多态性和依赖注入,如何选择子类而不实例化它

时间:2018-05-18 21:24:09

标签: c# design-patterns dependency-injection polymorphism factory

使用DI,我一直使用的方法是构造函数注入。使用DI,依赖类被注入到调用类中,不需要实例化依赖类。

考虑下面的代码,我需要确定使用哪个具体类。如何在不手动实例化类的情况下选择适当的具体类(因为使用DI,注入器会为您执行此操作)?代码都是手写的,原谅任何错字:)

public interface IMyInterface
    {
        void DoSomething();
    }

public class SuperClass : IMyInterface
{
    private DependencyClass _dependency;
    public SuperClass(DependencyClass dependency)
    {
         _dependency = dependency;
    }

    public abstract void DoSomething();

}

public class ChildClassCommon : SuperClass
{
    private DependencyClass _dependency;
    public ChildClassCommon(DependencyClass dependency)
    {
         _dependency = dependency;
    }

    public override void DoSomething()
    {
    }
}

public class ChildClassSpecial : SuperClass
{
    private DependencyClass _dependency;
    public ChildClassSpecial(DependencyClass dependency)
    {
         _dependency = dependency;
    }

    public override void DoSomething()
    {
    }
}


public class Main()
{
    private IMyInterface _myClass;

    public Main(IMyInterface myClass)
    {
        _myClass = myClass;
    }

    public void Selector(string recordType)
    {
        if(recordType == "Common")
        {
             // without DI, i would normally just do a new here to 
             // instantiate the concrete class, but with DI, I shouldnt be
             // instantiating a new instance, but how do I select which
             // concrete class to use?
            _myClass = new ChildClassCommon(DependencyClass dependency);
        }
        else if (recordType == "Special")
        {
            _myClass = new ChildClassSpecial(DependencyClass dependency);
        }

        _myClass.DoSomething();
    }
}

4 个答案:

答案 0 :(得分:1)

您可以注册所有已实现的类和工厂类。之后你可以解决工厂类。当您有工厂类时,可以使用某个生成规则生成所需的对象。

我在此示例中使用Autofac作为DI工具

class Program
{
    public class DependencyClass { }

    public interface IMyInterface
    {
        void DoSomething();
    }

    public abstract class SuperClass : IMyInterface
    {
        protected DependencyClass _dependency;
        public SuperClass(DependencyClass dependency)
        {
            _dependency = dependency;
        }

        abstract public void DoSomething();
    }

    public class ChildClassCommon : SuperClass
    {
        public ChildClassCommon(DependencyClass dependency) : base(dependency){}

        public override void DoSomething(){}
    }

    public class ChildClassSpecial : SuperClass
    {
        public ChildClassSpecial(DependencyClass dependency) : base(dependency){}

        public override void DoSomething(){}
    }

    public class MyInterfaceFactory
    {
        private IEnumerable<IMyInterface> _myInterfaces;
        public MyInterfaceFactory(IEnumerable<IMyInterface> myInterfaces)
        {
            _myInterfaces = myInterfaces;
        }

        public IMyInterface Generate(string rule)
        {
            IMyInterface myObject;
            if (rule == "a")
                myObject = _myInterfaces.First(x => x is ChildClassCommon);
            else
                myObject = _myInterfaces.First(x => x is ChildClassSpecial);
            return myObject;
        }
    }

    // Injection run in this
    static void Main(string[] args)
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<DependencyClass>().AsSelf();


        builder.RegisterType<MyInterfaceFactory>().AsSelf();

        var assembly = Assembly.GetExecutingAssembly();
        builder
            .RegisterAssemblyTypes(assembly)
            .AssignableTo<IMyInterface>()
            .AsImplementedInterfaces();

        var container = builder.Build();

        var factory = container.Resolve<MyInterfaceFactory>();
        IMyInterface myInterface = factory.Generate("a");

        Console.WriteLine(myInterface.GetType());

        Console.ReadKey();
    }
}

答案 1 :(得分:0)

我不确定我是否理解你,但如果你不想要实例化课程并想要使用它,也许可以尝试反思?你可以获得指定的类,然后调用它的方法。

答案 2 :(得分:0)

如果我已经正确地阅读了这个内容,那么typeof()可以选择引用您的具体类型,如果您当时不想实例化该对象。

public static Type Selector(string recordType)
{
        Type concreteType;

        switch (recordType)
        {
            case "Common":
                concreteType = typeof(ChildClassCommon);
                break;
            case "Special":
                concreteType = typeof(ChildClassSpecial);
                break;
            default:
                concreteType = null;
                break;
        }

        return concreteType;
}

但是要理解,在某些时候,您需要实例化该对象才能在您的应用程序中使用。因此,通过上面的示例,要实例化,您可以将Reflection与DI参数一起使用,如下所示:

var type = Selector("Common");
var myObject = (IMyInterface) Activator.CreateInstance(type, new object[] { dependency });

MSDN:

typeof()

Type

答案 3 :(得分:-1)

我是个白痴,我可以轻松地施展它。在看电视时回答了我自己的问题:D

if(recordType == "Common")
        {

            ((ChildClassCommon)_myClass).DoSomething();
        }
        else if (recordType == "Special")
        {
            ((ChildClassSpecial)_myClass).DoSomething();
        }