【发布时间】:2013-03-23 04:46:27
【问题描述】:
有没有办法创建一个实现以下两个通用接口的基类?然后,这个基类将被“其他”类继承,这些类可以从两个接口中的任何一个调用方法。
public interface IGenericRepositoryOne<E> where E : Entity
{
void Save(E entity);
void Save(List<E> entityList);
E Load(Guid entityId);
}
public interface IGenericRepositoryTwo<D, E> where E : Entity
{
void Save(D dto);
void Save(List<D> entityList);
D Load(Guid entityId);
}
现在我们有两个独立的存储库,分别实现每个接口:
public abstract class RepositoryOne<D, E> : IGenericRepositoryOne<D, E> where E : Entity {...}
public abstract class RepositoryTWO<E> : IGenericRepositoryTwo<E> where E : Entity {...}
还有一些类需要继承RepositoryOne 或RepositoryTwo。正是在这些课程中,我希望做一些分解,例如:
public class MessageDataTypeRepository : RepositoryTwo<MyEntityType>
{
// here when I call the method Load() I want it for RepositoryOne implementation.
}
public class MessageDataTypeRepository : RepositoryOne<MyDTOType, MyEntityType>
{
// here when I call the method Load() I want it for the RepositoryTwo implementation.
}
【问题讨论】:
-
你确定第二个的
吗?我在成员列表中没有看到 E。 -
除此之外,您在尝试实现这两个接口时遇到了什么错误?它给你带来了哪些麻烦?
-
@AnthonyPegram:是的,我还需要
E,因为还有其他操作使用它,为简洁起见,上面省略了。我更新了我的问题,以进一步阐明我想要做什么。
标签: c# architecture