2017-07-02 1 views
0

Szenario implementiert:kann nicht Parameter übergeben, die Schnittstelle

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 
} 

Warum kann ich nicht ISomeOtherEntityRepository passieren zu stützen? Ich bekomme den Fehler "ist nicht zuweisbare Parameter Typ". Es implementiert IRepository und SomeOtherEntity ist eine IEntity, also sehe ich nicht, warum das, was ich versuche, ungültig ist. Kann jemand bitte beraten?

+0

Warum fehlen die Schlüsselwörter 'class' und' interface'? Arbeitscode immer kopieren/einfügen. –

Antwort

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

Siehe this answer für eine längere Erklärung über Co-und Contravarianz.

Verwandte Themen