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

时间:2019-05-26 19:11:33

标签: c# generics interface composition

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

我一直在尝试通过代码进行不同的技术和实现。有些已经可以编译,但是在运行时它们还没有得到解决。问题的一部分是-该方法可以完成吗,另一个是我没有正确解决此问题。

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)将我的第一个答案带到正确的轨道上时,我将对其进行投票并标记为答案。希望这会帮助其他人。非常感谢。

2 个答案:

答案 0 :(得分:0)

看起来您正在尝试做的事情是这样的:

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();

希望您想要的就是它。

答案 1 :(得分:0)

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

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();