具有组合接口和多个具体实现的通用接口



我想使用一个通用接口,它可以将其他接口作为属性,使用具体的实现。如果可能,如何创建模式,以及如何解析服务集合中的实现。这将与工作单元和存储库模式一起使用。

我一直在通过代码尝试不同的技术和实现。有些已经能够编译,但在运行时它们无法解决。问题的一部分是 - 这种方法可以做到吗,另一个我没有正确解决这个问题。

public class GenericInterface<T, U> : IGenericInterface<T, U>
{
public T InterfaceOne { get; set; }     // IInterfaceOne
public U InterfaceTwo { get; set; }     // IInterfaceTwo
}
public interface IInterfaceOne
{
void DoOne();
void DoTwo();
}
public interface IInterfaceTwo
{
void DoThree();
void DoFour();
}
public class ConcreteImplementation : IInterfaceOne, IInterfaceTwo
{
public void DoOne()
{
throw new System.NotImplementedException();
}
public void DoTwo()
{
throw new System.NotImplementedException();
}
public void DoThree()
{
throw new System.NotImplementedException();
}
public void DoFour()
{
throw new System.NotImplementedException();
}
}
public class Resolver { 
public void Resolve() {
// Should be able to use the interfaces and reference the ConcreteImplementation....!?
// How to resolve in a service collection?
var genericComposedInterfaces = new GenericInterface<IInterfaceOne, IInterfaceTwo>();
}
}

期望的结果是,它应该首先服务于目的,其次在服务集合中/调用时得到解析。

更新

感谢您的帮助。所以我想我被夹在我试图实现的目标的两种思想之间。我真正想要的只是一个通过两个接口公开的支持类,这两个接口可以组合成一个通用接口。我认为以下有效。

通用接口需要两个或多个接口。

public interface ITestGenericInterface<T, U>
{
T InterfaceOne { get; }
U InterfaceTwo { get; }
}
public class TestGenericInterface<T, U> : ITestGenericInterface<T, U>
{
public TestGenericInterface(T interfaceOne, U interfaceTwo)
{
InterfaceOne = interfaceOne;
InterfaceTwo = interfaceTwo;
}
public T InterfaceOne { get; }
public U InterfaceTwo { get; }
}

具有可能与上述接口一起使用的类的接口。

public interface IInterfaceOne
{
void DoOne();
void DoTwo();
}
public interface IInterfaceTwo
{
void DoThree();
void DoFour();
}
public class ConcreteClass : IInterfaceOne, IInterfaceTwo
{
public void DoOne()
{
Console.WriteLine("DoOne()");
}
public void DoTwo()
{
Console.WriteLine("DoTwo()");
}
public void DoThree()
{
Console.WriteLine("DoDoThreeOne()");
}
public void DoFour()
{
Console.WriteLine("DoFour()");
}
}

在服务集合中注册。我认为我之前没有正确注册接口/实现,这可能是一个问题。

services.AddScoped<IInterfaceOne, ConcreteClass>();
services.AddScoped<IInterfaceTwo, ConcreteClass>();
services.AddScoped(typeof(ITestGenericInterface<,>), typeof(TestGenericInterface<,>));

尝试一下

public interface ITestRunner
{
void Start();
}
public class TestRunner : ITestRunner
{
private readonly ITestGenericInterface<IInterfaceOne, IInterfaceTwo> _genericTest;
public TestRunner(
ITestGenericInterface<IInterfaceOne, IInterfaceTwo> genericTest
)
{
_genericTest = genericTest;
}
public void Start()
{
// Interface One 
_genericTest.InterfaceOne.DoOne();
_genericTest.InterfaceOne.DoTwo();
// Interface Two
_genericTest.InterfaceTwo.DoThree();
_genericTest.InterfaceTwo.DoFour();
}
}

我很欣赏这可能比我最初的问题听起来更简单。当弗拉维奥·弗朗西斯科(Flavio Francisco)用他的第一个答案让我走上正轨时,我将投票并标记为答案。希望这会帮助其他人。非常感谢。

看起来您要做的是这样的:

public interface IGeneric<T, U>
{
T InterfaceOne { get; }
U InterfaceTwo { get; }
}
public class GenericClass : IGeneric<IInterfaceOne, IInterfaceTwo>
{
private readonly IInterfaceOne interfaceOne;
private readonly IInterfaceTwo interfaceTwo;
public GenericClass(IInterfaceOne interfaceOne, IInterfaceTwo interfaceTwo)
{
this.interfaceOne = interfaceOne;
this.interfaceTwo = interfaceTwo;
}
public IInterfaceOne InterfaceOne { get { return this.interfaceOne; } }
public IInterfaceTwo InterfaceTwo { get { return this.interfaceTwo; } }
}
public class ClassOne : IInterfaceOne
{
public void DoOne()
{
throw new NotImplementedException();
}
public void DoTwo()
{
throw new NotImplementedException();
}
}
public class ClassTwo : IInterfaceTwo
{
public void DoFour()
{
throw new NotImplementedException();
}
public void DoThree()
{
throw new NotImplementedException();
}
}

解析 器:

IGeneric<IInterfaceOne, IInterfaceTwo> genericClass = new GenericClass(new ClassOne(), new ClassTwo());
genericClass.InterfaceOne.DoOne();

希望你想要的就是它。

如果你想要一个实现两个接口的类的实例,你也可以这样做:

public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo
{
}
public interface IInterfaceOne
{
void DoOne();
void DoTwo();
}
public interface IInterfaceTwo
{
void DoThree();
void DoFour();
}

您的具体课程:

public class ConcreteClass : IInterfaceOneAndTwo
{
public void DoFour()
{
throw new NotImplementedException();
}
public void DoOne()
{
throw new NotImplementedException();
}
public void DoThree()
{
throw new NotImplementedException();
}
public void DoTwo()
{
throw new NotImplementedException();
}
}

和您的解析器:

IInterfaceOneAndTwo concreteClass = new ConcreteClass();
concreteClass.DoOne();

最新更新