[英]How to not repeat data access rules in .NET MVC3 using EF (Repository pattern)?
I have 2 classes with one-to-many like this: 我有2对这样的一对多的课程:
public class Parent
{
public virtual List<Child> Children {get; set;}
}
I am also using repository classes for each model, ie: 我还在为每个模型使用存储库类,即:
Parent has it's own repo class that has a Get() function that can gets an ordered list of Parents. Parent有自己的repo类,该类具有Get()函数,该函数可以获取有序的Parents列表。
Child also has it's own repo class tha thas a Get() function that can gets an ordered list of Children that have a status of Active. Child还具有自己的repo类,可以通过Get()函数获取状态为Active的Children的有序列表。
The problem is, the way EF works, since Parent has a reference to children, EF loads Children automatically when the parent list is loaded, but it gets active and inactive Children (also, the children list is not ordered). 问题是,EF的工作方式是,由于Parent引用了子代,因此EF在加载父列表时会自动加载子代,但它会变为活动和非活动子代(也不会对子代列表进行排序)。 The check for active status is only in the Child repo. 活动状态的检查仅在子仓库中。
How can I limit my repetition of "get" rules to be used wherever Children are accessed. 如何限制访问“儿童”的地方重复使用“获取”规则。
I know I can just modify the Parent repo to also check for Child status=Active, but then I am repeating my logic in every class that has children... 我知道我可以修改Parent仓库以检查Child status = Active,但是随后我会在每个有孩子的课程中重复我的逻辑...
Edit: I have a Repository class for Child that returns an Ordered list of Children: 编辑:我有一个儿童的存储库类,返回儿童的有序列表:
public List<Children> GetList()
{
IQueryable<Children> query = context.Set<Children>();
return query.OrderBy(a=>a.Seq).ToList();
}
That works fine when I just need to access the Children directly. 当我只需要直接访问“儿童”时,该方法就很好。 But when I need to get a Parent class, I use the repo function below: 但是,当我需要获取Parent类时,可以使用以下repo函数:
ParentRepo GetByID: ParentRepo GetByID:
public Parent GetByID(object id)
{
IQueryable<Parent> query = context.Set<Parent>();
Parent parentModel = context.Parents.Where(a => a.ParentId == (int)id)
.Select(a => new
{
Parent = a,
Children = a.Children.OrderBy(b => b.Seq)
}).ToList()
.Select(q => q.Parent)
.Single();
return parentModel;
}
As you can see, I had to order the Children here as well (by Seq). 如您所见,我还必须在此订购孩子们(通过Seq)。
This is the only way I can think to make this work, but it doesn't seem right. 这是我可以想到的唯一方法,但这似乎并不正确。
public Parent GetByID(object id)
{
IQueryable<Parent> query = context.Set<Parent>();
Parent parentModel = context.Parents.Single(a => a.ParentId == (int)id);
parentModel.Children = childRepo.GetList(id);
return parentModel;
}
The repository pattern abstracts the data access layer, so that the rest of the application doesn't depend on DAL implementation details. 存储库模式抽象了数据访问层,因此应用程序的其余部分不依赖于DAL实现细节。 The repository should be constructed according to the application needs . 应根据应用程序的需求来构建存储库 。 Basically you should design the repository interface from the application point of view and the repository should return domain/business objects. 基本上,您应该从应用程序的角度设计存储库接口,并且存储库应返回域/业务对象。
If I understand correctly what you're doing, you're trying to build repositories around EF which gives you this kind of problems. 如果我正确理解您在做什么,则您正在尝试围绕EF建立存储库,这会给您带来这类问题。
I might've misunderstood and it might be simply a EF related problem but it really seems to me that the problem is the wrong design of the repository. 我可能会误解了,这可能只是与EF相关的问题,但在我看来,问题确实是存储库的设计错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.