在所有生成的代码中包含合同/假设的计划是什么(例如实体框架) [英] What are the plans for including contracts / assumptions in all generated code (e.g. Entity Framework)

查看:75
本文介绍了在所有生成的代码中包含合同/假设的计划是什么(例如实体框架)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前EDMX生成的代码产生了很多......  CodeContracts:可能在空引用上调用方法




解决方案

不确定团队的计划是什么,但如果您使用的话,我可以建议在当前版本中处理这种情况的方法.NET 4。


对于ObjectContext,创建一个使用IObjectSet< TEntity>的接口。公开每个实体集合。为实现此接口的ObjectContext创建一个分部类。然后,您可以签订接口合同。例如,如果
我有一个EDMX,它有一个User实体和一个Users ObjectSet,那么在界面中我会有:

 IObjectSet< User> ;用户{get; } 

生成的代码将实现该接口,因为它在partial类中被引用,并且签名与将生成的代码匹配。然后,您可以向界面添加合同。在这种情况下,我会将以下内容添加到接口
合同中:

 IObjectSet< User> IMyContext.Users 
{
get
{
Contract.Ensures(Contract.Result< IObjectSet< User>>()!= null);
返回默认值(IObjectSet< User>);
}
}

此时你在ObjectContext上有一些合约......你已经可以使用一个接口代替类。


您也可以直接收缩生成的类和代码。为此,您可以创建合同规定的POCO类*或*您可以控制代码生成。您可以创建自己的T4(.tt)文件,以便从EDMX
生成各种支持类。您可以先修改它们提供的默认模板,使其包含您实体公开的各种属性的合同。 (包括导航属性)。如果您不想使用接口方法,可能以
方式直接收缩ObjectContext。 EDMX格式允许您包含自己的注释,然后可以将其作为代码生成过程的一部分进行选取和使用。如果你走这条路线,T4编辑器(例如有形的那个)
可以成为真正的救星。


签约实体有一个考虑因素。由于EF必须能够使用默认构造函数创建类并初始化属性,因此在编写契约时必须考虑这一点。也就是说,它运作得很好。另一种方法
工作(即使不使用部分类处理代码也可以完成)是将ValidateContracts()方法添加到实体类,然后验证实体中的值是否符合特定合同。我发现它有用
有时会在类上生成静态'helper'方法,例如IsValidName(string)来包装验证的详细信息。这样就可以调用Contract.Requires(IsValidName(value))。


 希望这能为您提供一些关于如何将代码契约与实体框架结合起来的想法。


Currently the EDMX generated code produces lots of ... CodeContracts: Possibly calling a method on a null reference


解决方案

Not sure what the teams plans are for this, but I can suggest ways to possibly deal with this situation in the current version if you are using .NET 4.

For your ObjectContext, create an interface which uses IObjectSet<TEntity> to expose each of the entity collections. Create a partial class for the ObjectContext which implements this interface. You can then contract the interface. For example, if I have an EDMX which has a User entity and a Users ObjectSet, then in the interface I will have:

IObjectSet<User> Users { get; }

The generated code will implement that interface since it is referenced in the partial class and the signature matches the code that will be generated. You can then add a contract to the interface. In this case, I would add the following into the interface contract:

IObjectSet<User> IMyContext.Users
{
  get
  {
    Contract.Ensures(Contract.Result<IObjectSet<User>>() != null);
    return default(IObjectSet<User>);
  }
}

At this point you have some contracts on the ObjectContext ... and you've made it possible to use an interface instead of the class.

You can also directly contract the generated classes and code. For this, you can either create contracted POCO classes *or* you can take control of the code generation. You can create your own T4 (.tt) file to generated the various supporting classes from the EDMX. You can start by modifying the default template they provide so that it includes contracts on the various properties exposed by your entities (including the navigation properties). It is possibly to directly contract the ObjectContext in this manner if you don't want to use the interface approach. The EDMX format allows you to include your own annotations which can then be picked up and used as part of the code generation process. If you go this route, T4 editors (such as the one made by Tangible) can be a real lifesaver.

There is one consideration in contracting the entities. Since EF must be able to create the class with the default constructor and initialize the properties, you have to consider this when writing the contracts. That said, it works quite well. Another approach which works (and which can be done even without handling code generation by using partial classes) is to add a ValidateContracts() method to the entity class which then validates that the values in the entity honor specific contracts. I have found it helpful to sometimes generate static 'helper' methods on the class such as IsValidName(string) to wrapper the details of the validation. This makes it possible to also call Contract.Requires(IsValidName(value)).

 Hope this gives you some ideas of ways that you can combine Code Contracts with the Entity Framework.


这篇关于在所有生成的代码中包含合同/假设的计划是什么(例如实体框架)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆