将具体的类实例转换为使用泛型的接口

问题描述

| 这是我从抽象基类派生的具体存储库
public class DepartmentRepository : RepositoryBase<Department>,IDepartmentRepository

public abstract class RepositoryBase<T> : IRepository<T> where T : class,IPersistantBusinessObject
这是我的界面
public interface IDepartmentRepository : IRepository<Department>

public interface IRepository<T> where T : IPersistantBusinessObject

public interface IPersistantBusinessObject { ... }
这是我的实体
public class Department : BusinessObjectBase

public abstract class BusinessObjectBase : IPersistantBusinessObject
如何将DepartmentRepository转换为通用IRepository?
DepartmentRepository rep = new DepartmentRepository();
(IRepository<IPersistentBusinessObject>)rep; // this doesn\'t work
有想法吗?如果您想知道,这是一个使用存储库模式的asp.net MVC3应用程序。我是否过度设计了接口?我现在应该如何使用通用存储库? 谢谢。 编辑: 这是我的IRepository接口。当被问及为什么需要强制转换为IRepository时,实际上只是这样,所以我可以使用\“ SaveChanges \”方法,因为我想从数据库中拦截任何异常并以一种通用方式进行处理。
public interface IRepository<T> where T: IPersistantBusinessObject
{
    IQueryable<T> Retrieve();
    T Retrieve(int id);
    IQueryable<T> RetrieveActiveAndInActive();
    T RetrieveActiveAndInActive(int id);
    void Add(T domainObject);
    void SaveChanges();
    void Delete(T domainObject);
    void Delete(int id)
 }
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)