如何解决这个通用的存储库模式问题?

发布于 2024-08-05 23:55:29 字数 1002 浏览 2 评论 0原文

我从上一个问题中得到了这段代码,但它没有编译:

    public interface IEntity
{     
// Common to all Data Objects
}
public interface ICustomer : IEntity
{
     // Specific data for a customer
}
public interface IRepository<T, TID> : IDisposable where T : IEntity
{
     T Get(TID key);
     IList<T> GetAll();
     void Save (T entity);
     T Update (T entity);
     // Common data will be added here
}
public class Repository<T, TID> : IRepository
{
     // Implementation of the generic repository
}
public interface ICustomerRepository
{
     // Specific operations for the customers repository
}
public class CustomerRepository : Repository<ICustomer>, ICustomerRepository
{
     // Implementation of the specific customers repository
}

但是在这两行中:

1- public class Repository : IRepository

2- public class CustomerRepository : Repository, ICustomerRepository

它给了我这个错误: 使用通用类型 'TestApplication1.IRepository '需要'2'类型参数

你能帮我解决吗?

I had this code from a previous question, but its not compiling:

    public interface IEntity
{     
// Common to all Data Objects
}
public interface ICustomer : IEntity
{
     // Specific data for a customer
}
public interface IRepository<T, TID> : IDisposable where T : IEntity
{
     T Get(TID key);
     IList<T> GetAll();
     void Save (T entity);
     T Update (T entity);
     // Common data will be added here
}
public class Repository<T, TID> : IRepository
{
     // Implementation of the generic repository
}
public interface ICustomerRepository
{
     // Specific operations for the customers repository
}
public class CustomerRepository : Repository<ICustomer>, ICustomerRepository
{
     // Implementation of the specific customers repository
}

But in these 2 lines :

1- public class Repository : IRepository

2- public class CustomerRepository : Repository, ICustomerRepository

It give me this error: Using the generic type 'TestApplication1.IRepository' requires '2' type arguments

can you help me solve?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-08-12 23:55:29

从 Repository/IRepository 继承时需要使用两个类型参数,因为它们采用两个类型参数。也就是说,当您从 IRepository 继承时,您需要指定类似:

public class Repository<T, TID> : IRepository<T,TID> where T:IEntity

public class CustomerRepository : Repository<ICustomer,int>,ICustomerRepository

Edited 以在 Repository 的实现上添加类型约束

You need to use two type arguments when inheriting from Repository/IRepository because they take take two type arguments. Namely, when you inherit from IRepository, you need to specify something like:

public class Repository<T, TID> : IRepository<T,TID> where T:IEntity

and

public class CustomerRepository : Repository<ICustomer,int>,ICustomerRepository

Edited to add type constraint on implementation of Reposistory

摇划花蜜的午后 2024-08-12 23:55:29

当您实现通用接口时,您还需要提供通用接口类型规范。将这两行更改为:

public class Repository<T, TID> : IRepository<T, TID>
     where T : IEntity
{  
    // ...

public class CustomerRepository : Repository<ICustomer, int /*TID type*/>, ICustomerRepository
{
     // ...

When you are implementing a generic interface, you need to provide the generic interface type specifications as well. Change those two lines to:

public class Repository<T, TID> : IRepository<T, TID>
     where T : IEntity
{  
    // ...

and

public class CustomerRepository : Repository<ICustomer, int /*TID type*/>, ICustomerRepository
{
     // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文