How to determine the order of Property Loading in CodeFirst?

倾然丶 夕夏残阳落幕 提交于 2019-12-25 07:23:42

问题


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).


回答1:


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.




回答2:


Then you can just declare PropertList as read only.

public Class
{
 public string Propert1{get;set;}
 public string Propert2{get;set;}
 public List<String> PropertList{
       get{ 
          return genaratePropertyListFromProperty1();
       }
 }
}

Here prpertyList will be populated only when it is accessed.



来源:https://stackoverflow.com/questions/14191288/how-to-determine-the-order-of-property-loading-in-codefirst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!