Я использую универсальный интерфейс двух типов, который называется IRepository<Entity, DTO>
.Как я могу инициализировать в RepositoryFactory с помощью Dependency Injection.
Интерфейс , который я пытаюсь инициализировать.
public interface IRepository<Entity, DTO> where Entity : BaseEntity where DTO : BaseDTO
{
bool Save(Entity Entity);
bool Save(Entity Entity, out int OutID);
bool Update(Entity Entity);
bool Update(Entity Entity, out int OutID);
DTO GetRecord(Entity ID);
DTO GetRecord(Expression<Func<Entity, bool>> predicate);
List<DTO> GetList();
List<DTO> GetByQuery(Expression<Func<Entity, bool>> predicate);
}
BaseEntity, BaseDTO
public abstract class BaseEntity
{
public int ID { get; protected set; }
}
public class BaseDTO
{
public int ID { get; protected set; }
}
AdministratorRepository
public class AdministratorRepository : Repository<AdministratorEntity, AdministratorDTO>
{
public AdministratorRepository(string ConnectionString) : base(ConnectionString)
{
}
// Implemented functions for Repository base class
}
Репозиторий базовый класс репозитория
public abstract class Repository<Entity, DTO> : IRepository<Entity, DTO>
where Entity : BaseEntity
where DTO : BaseDTO
{
// Implemented functions for IRepository Interface
}
RepositoryFactory
public class RepositoryFactory<Entity, DTO> where Entity : BaseEntity where DTO : BaseDTO
{
private static string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static IRepository<Entity, DTO> Create(Repositories repository)
{
IRepository<Entity, DTO> iRepo = null;
switch (repository)
{
// If I do this, program wants me to cast
case Repositories.Administrator:
// Casting error occured in this line.
// AdministratorRepository derives from Repository.
/*
Cannot implicitly convert type
'BusinessRules.Repositories.AdministratorRepository' to
'BusinessRules.Repositories.IRepositories.IRepository<Entity,DTO>'.
An explicit conversion exists (are you missing a cast?)
*/
iRepo = new AdministratorRepository(CONNECTION_STRING);
break;
// If I do this, program throws an exception
// Exception: TypeInitializationException
// Exception Description: The type initializer for 'BusinessRules.Repositories.RepositoryFactory`2' threw an exception.
case Repo.Administrator:
iRepo = (IRepository<Entity, DTO>)(new AdministratorRepository(CONNECTION_STRING));
break;
}
return iRepo;
}
}