C# 我怎样才能摆脱一个空接口
我知道空接口很可能是一种代码味道。我发现自己正在使用一个,我无法找到设计缺陷是什么 我有一个DBServer抽象类,名为C# 我怎样才能摆脱一个空接口,c#,dependency-injection,interface,C#,Dependency Injection,Interface,我知道空接口很可能是一种代码味道。我发现自己正在使用一个,我无法找到设计缺陷是什么 我有一个DBServer抽象类,名为Server public abstract class Server { protected ICustomerRepository CustomerRepository; public string HostName{ get; } protected Server(string HostName)
Server
public abstract class Server
{
protected ICustomerRepository CustomerRepository;
public string HostName{ get; }
protected Server(string HostName)
{
this.HostName = HostName;
}
protected GetCustomers()
{
return CustomerRepository.GetCustomers();
}
}
因为我使用了两个DB引擎,比如A和B,所以我有两个含义
A的实现需要A的icCustomerRepository
,例如ACustomerRepository
但是,为了能够对实现进行单元测试,我将使用接口IACustomerRepository
并注入依赖项
问题是这个接口是空的
public interface ICustomerRepository
{
string HostName { get; set; }
IEnumerable<ICustomer> GetCustomers();
}
public interface IACustomerRepository:ICustomerRepository
{
}
统一配置:
container.RegisterType<IACustomerRepository, ACustomerRepository>();
container.RegisterType();
我读过“标记接口”,但我不确定它是否适用于这里
您能帮我确定潜在的问题,以便我可以修复此代码气味吗?我不确定我是否理解。究竟是什么实现了
IACustomerRepository
?@maccettura我的错,我忘了把它放在问题中。我已经添加了实现。如果IACustomerRepository
只是icCustomerRepository
为什么要IACustomerRepository
?它只是为了依赖注入吗?在这种情况下,除非您的IOC容器支持每上下文注册,否则您实际上没有其他选择。我假设marker接口的“需要”是为了您能够配置IOC容器。您的容器可能支持container.Register(“注入到服务器时”)——所以只有一个接口就足够了。您使用哪个IoC容器?看起来像是未完成的代码,用于将来的扩展。也许有人在遵循一种模式。为什么它需要修理?
public class ServerA: Server
{
public ServerA (
string HostName,
IACustomerRepository ACustomerRepository):
base(HostName)
{
CustomerRepository = ACustomerRepository;
ACustomerRepository.HostName = HostName;
}
}
container.RegisterType<IACustomerRepository, ACustomerRepository>();