如何确定CodeFirst中的物料装载顺序? [英] How to determine the order of Property Loading in CodeFirst?

查看:129
本文介绍了如何确定CodeFirst中的物料装载顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在想,有什么办法可以确定CodeFirst中的物业装载顺序,例如我有一个如下所示的类:

  public class 
{
public string Propert1 {get; set;}
public string Propert2 {get; set;}
public List< string> PropertList {get; set;}
}

我需要让EF加载ProprtyList之前的property1!(因为我操纵ProprtyList值在Property1_Changed)。

解决方案

属性应该是:属性。即你得到或设置它,没有别的,没有副作用。我知道即使是一些违反这个规则的.Net课程,但它仍然是一个非常健康的原则。另一个原则是在对象的哪个顺序属性设置中不重要。这是因为设置任何单个属性应该使对象处于有效状态。



所以考虑你的 EF实现的方式是有效的。然后可以开始修改。如果要以同时更改多个属性的方式修改对象,则应通过调用具有描述性名称的方法来实现,而不是通过设置一个属性并静默更改其他属性。



如果您希望基于 PropertList 呈现不同内容的列表,请制作只读(未映射)属性或 GetXyz ()生成更改的内容的方法(不修改 PropertList !)并返回。


I was thinking that is there any way to determine the order of property loading in CodeFirst,for example i have a class like below:

public Class
{
 public string Propert1{get;set;}
 public string Propert2{get;set;}
 public List<string> PropertList{get;set;}
}

And i need to make EF to load ProprtyList before property1!(Because i manipulate ProprtyList Values On Property1_Changed).

解决方案

A property should be just that: a property. I.e. you get or set it and nothing else, no side effects. I know that even some .Net classes that violate this rule, but still it is a very healthy principle. Another principle is that it should not matter in which order properties of an object are being set. That is because setting any individual property should leave an object in a valid state.

So consider your Class object, the way EF materializes it, to be valid. Then you can start modifying it. If you want to modify an object in a way that multiple properties are changed simultaneously, you should do this by calling a method with a descriptive name, not by setting one property and silently changing others.

If you want a list that presents a different content based on PropertList, make a read-only (unmapped) property or a GetXyz() method in which the changed content is generated (without modifying PropertList!) and returned.

这篇关于如何确定CodeFirst中的物料装载顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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