I'm building my own generic solution to Entity Framework update in disconnected scenario. Many different approaches could be taken but I decided to decorate ICollection properties inside my entities with a custom attribute so that I can check the state of each entity inside those collections. Here's a sample entity with a navigation property:
public class{
public int TaskId{get;set;}
public string Instruction {get;set;}
[EntityNavigationProperty]
public virtual ICollection<TaskExecutor> Executors {get;set;}
}
public class{
public int TaskExecutorId{get;set;}
public int TaskId{get;set;}
public virtual Task Task{get;set;}
}
public class EntityNavigationProperty:Attribute{}
I have a generic Update method that I'm planning to use to update any type of entity which will ensure that the related entities also get updated properly.
1 public void Update (TEntity entity){
2 PropertyInfo[] properties=entity.GetType().GetProperties();
3 foreach (PropertyInfo pi in properties)
4 {
5 if(Attribute.IsDefined(pi,typeof(EntityCollectionPropertyAttribute))){
6 foreach (//here I need to iterate through the ICollection object){
7
8
9 }
10 }
11 }
12}
Now, assume I'm sending an instnce of Task to the above update method.In line 3, when iterator comes to the Executors
property, the condition in line 5 resolves to true. Now I need to iterate through the Executors property and do appropriate tasks. For this particular case, in line 6 I can say:
foreach (var item in (pi.GetValue(entity) as ICollection<TaskExecutor>))
But how can I determine what to type instead of T in ICollection?
Aucun commentaire:
Enregistrer un commentaire