具有组合接口和多个具体实现的通用接口
Generic Interface with composed interfaces and multiple concrete implementations
我想使用一个通用接口,它可以将其他接口作为属性,使用具体实现。如果可能,如何创建模式,以及如何解决服务中的实现 collection。这将与工作单元和存储库模式一起使用。
我一直在通过代码尝试不同的技术和实现。有些已经能够编译,但在运行时它们无法得到解析。问题的一部分是 - 该方法是否可以完成,另一个问题是我没有正确解决这个问题。
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>();
}
}
期望的结果是它应该首先服务于目的,其次在服务 collection / 调用时得到解决。
更新
感谢您的帮助。因此,我认为我在试图实现的目标之间陷入了两种想法。我真正想要的只是一个支持 class,它通过两个可以组合成一个通用接口的接口公开。我认为以下作品。
通用接口采用两个或多个接口。
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; }
}
带有 class 的接口可能与上述接口一起使用。
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()");
}
}
正在注册服务 collection。我认为我之前没有正确注册接口/实现,这可能是个问题。
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();
希望是你想要的。
如果您想要一个实现两个接口的 class 实例,您也可以这样做:
public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo
{
}
public interface IInterfaceOne
{
void DoOne();
void DoTwo();
}
public interface IInterfaceTwo
{
void DoThree();
void DoFour();
}
你的具体class:
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();
我想使用一个通用接口,它可以将其他接口作为属性,使用具体实现。如果可能,如何创建模式,以及如何解决服务中的实现 collection。这将与工作单元和存储库模式一起使用。
我一直在通过代码尝试不同的技术和实现。有些已经能够编译,但在运行时它们无法得到解析。问题的一部分是 - 该方法是否可以完成,另一个问题是我没有正确解决这个问题。
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>();
}
}
期望的结果是它应该首先服务于目的,其次在服务 collection / 调用时得到解决。
更新
感谢您的帮助。因此,我认为我在试图实现的目标之间陷入了两种想法。我真正想要的只是一个支持 class,它通过两个可以组合成一个通用接口的接口公开。我认为以下作品。
通用接口采用两个或多个接口。
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; }
}
带有 class 的接口可能与上述接口一起使用。
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()");
}
}
正在注册服务 collection。我认为我之前没有正确注册接口/实现,这可能是个问题。
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();
希望是你想要的。
如果您想要一个实现两个接口的 class 实例,您也可以这样做:
public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo
{
}
public interface IInterfaceOne
{
void DoOne();
void DoTwo();
}
public interface IInterfaceTwo
{
void DoThree();
void DoFour();
}
你的具体class:
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();