2

我已经为多个域实现了 IRepository 接口,但我不知道如何调用特定的方法。

下面是代码:

public interface IRepository<T> where T : class
{
    IEnumerable<T> GetAll();
    void Add(T entity);
    void Update(T entity);
}


public interface IProjectRepository : IRepository<ProjectType>, IRepository<Project>, IRepository<ProjectDetail>
{
}

public class ProjectRepository : IProjectRepository
{
// implemenation

}

现在,当我创建 ProjectRepository 类的对象时,它显示了接口的所有方法,但只需要 projecttype 参数。

我不知道,这是正确的实现方式吗?还是有另一种方法来实现类似的事情?

4

2 回答 2

4

通用存储库IRepository<T>不是一个好的解决方案 IMO,看看http://www.sapiensworks.com/blog/post/2012/03/05/The-Generic-Repository-Is-An-Anti-Pattern.aspx

我认为最好在没有通用参数和常用方法的情况下单独指定每个存储库(您确定在所有存储库中总是需要Add, Update,GetAll吗?)

于 2013-11-15T09:03:35.743 回答
0

我认为使用工作单元模式创建通用存储库会更好。
看看
http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns- in-an-asp-net-mvc-application
这是关于使用工作单元实现通用存储库的好教程,
位于 http://contosontiermvc.codeplex.com/SourceControl/changeset/view/13936#ContosoUniveristy/ContosoUniveristy/Controllers/CourseController .cs
.this 是代码示例

于 2013-11-15T09:36:53.357 回答