2017-07-02 107 views
0

場景:不能傳遞實現參數接口

public interface IEntity { 
    int Id {get; set;}; 
} 

public interface IRepository<T> where T: class, IEntity { 
    //repo crud stuffs here 
} 

public class RepositoryBase<T> : IRepository<T> where T : class, IEntity { 
    //general EF crud implementation 
} 

public class MyEntity : IEntity { 
    //implementation and other properties 
} 

public interface IMyEntityRepository : IRepository<MyEntity> { 
    //any extending stuff here 
} 

public class MyEntityRepository : RepositoryBase<MyEntity>, IMyEntityRepository { 
    //implementation here 
} 

public interface IBusinessLogic<T> where T : class, IEntity { 
} 

public class BusinessLogicBase<T> : IBusinessLogic<T> where T : class, IEntity { 
    private readonly IRepository<T> _baseRepo; 
    private readonly IList<IRepository<IEntity> _repositories; 

    public BusinessLogicBase(IRepository<T> baseRepo, params IRepository<IEntity>[] repositories) { 
     _baseRepo = baseRepo; 
     _repositories = repositories; 
    } 

    //some standard business logic methods that resolve the repositories based on navigation property types and call 

    private IRepository<U> ResolveRepository<U>() where U: class, IEntity { 
     IRepository<U> found = null; 
     foreach (IRepository<IEntity> repository in _repositories) { 
      if (repository.IsFor(typeof(U))) { 
       found = repository as IRepository<U>; 
       break; 
      } 
     } 
     return found; 
    } 
} 

public interface IMyEntityBL : IBusinessLogic<MyEntity> { 
    //extended stuff here 
} 

public class SomeOtherEntity : IEntity { 
} 

public interface ISomeOtherEntityRepository : IRepository<SomeOtherEntity> { 
} 

public interface SomeOtherEntityRepository : RepositoryBase<SomeOtherEntity>, ISomeOtherEntityRepository { 
} 

public class MyEntityBL : BusinessLogicBase<MyEntity>, IMyEntityBL { 
    public MyEntityBL(IRepository<MyEntity> baseRepo, ISomeOtherEntityRepository someOtherEntityRepo) : base(baseRepo, someOtherEntityRepo) { 
     //ERROR IS HERE WHEN TRYING TO PASS ISomeOtherEntityRepository to base 
    } 
    //implementation etc here 
} 

爲什麼我不能通過ISomeOtherEntityRepository立足?我得到「不可分配的參數類型」錯誤。它實現了IRepository,SomeOtherEntity是一個IEntity,所以我不明白爲什麼我嘗試的是無效的。有人可以請指教嗎?

+0

爲什麼所有'class'和'interface'關鍵字都缺失?始終複製/粘貼工作代碼。 –

回答

1
public interface IRepository<out T> 
    where T : class, IEntity 

有關合作和反方差冗長的說明,請參見this answer

相關問題