gpt4 book ai didi

c# - 两种类型通用接口(interface)的依赖注入(inject)

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:22 24 4
gpt4 key购买 nike

我正在使用两种类型的通用接口(interface),称为 IRepository<Entity, DTO> .如何使用依赖注入(inject)在 RepositoryFactory 中进行初始化。

我尝试初始化的

接口(interface)

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; }
}

管理员库

public class AdministratorRepository : Repository<AdministratorEntity, AdministratorDTO>
{
public AdministratorRepository(string ConnectionString) : base(ConnectionString)
{

}

// Implemented functions for Repository base class
}

Repository 基本存储库类

public abstract class Repository<Entity, DTO> : IRepository<Entity, DTO> 
where Entity : BaseEntity
where DTO : BaseDTO
{
// Implemented functions for IRepository Interface
}

存储库工厂

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;
}
}

最佳答案

您提到的场景是“您试图分配比接口(interface)的通用类型参数更派生的类型对象”。要允许分配,您应该定义带有参数类型的通用接口(interface)。阅读本文了解variance在 C# 中。

您可以使用 out 关键字声明泛型类型参数协变。

所以定义IRepository接口(interface)为

public interface IRepository<out Entity, out DTO> where Entity : BaseEntity where DTO : BaseDTO
{
//......
}

完整代码(示例应用 - 检查 this live fiddle)

using System;

public abstract class BaseEntity
{
public int ID { get; protected set; }
}

public class BaseDTO
{
public int ID { get; protected set; }
}

public interface IRepository<out Entity, out DTO> where Entity : BaseEntity where DTO : BaseDTO
{
string GetTestString();
}

public abstract class Repository<Entity, DTO> : IRepository<Entity, DTO>
where Entity : BaseEntity
where DTO : BaseDTO
{

// Implemented functions for IRepository Interface
public Repository(string connectionString)
{

}

public abstract string GetTestString();
}

public class AdministratorEntity : BaseEntity
{

}

public class AdministratorDTO : BaseDTO
{

}

public class AdministratorRepository : Repository<AdministratorEntity, AdministratorDTO>
{
public AdministratorRepository(string ConnectionString) : base(ConnectionString)
{

}

public override string GetTestString()
{
return "TestString";
}

// Implemented functions for Repository base class
}


public class Program
{
public static void Main()
{

IRepository<BaseEntity, BaseDTO> repo = new AdministratorRepository("connectionString");
Console.WriteLine(repo.GetTestString());
Console.WriteLine("Hello World");
}
}

关于c# - 两种类型通用接口(interface)的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50758946/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com