简体   繁体   English

如何使用统一和对象工厂

[英]How to use unity and object factory

Have a code: 有一个代码:

class MyClass : IMyInterface
{
    public void DoSomething()
    {
        using(var dependency = new Dependency())
        { 
           // some code
        }

        // some code
    }

    public void DoSomething2()
    {
        using(var dependency = new Dependency())
        { 
           // some code
        }

        // some code
    }
}

So, Dependency is a class inherited from IDisposable. 因此,Dependency是从IDisposable继承的类。

I want to rewrite code using Unity container: 我想使用Unity容器重写代码:

class MyClass : IMyInterface
{
    private readonly IDependency _dependency;

    public MyClass(IDependency dependency)
    {
        _dependency = dependency;
    }

    public void DoSomething()
    {
        using(_dependecy)
        { 
           // some code
        }

        // some code
    }

    public void DoSomething2()
    {
        using(_dependecy)
        { 
           // some code
        }

        // some code
    }
}

This solution isn't work because new instance of Dependency should be created for each "using". 该解决方案不起作用,因为应该为每个“使用”创建新的依赖关系实例。 Ok, I should inject something like factory: 好吧,我应该注入像工厂这样的东西:

class MyClass : IMyInterface
{
    private readonly IDependencyFactory _dependencyFactory;

    public MyClass(IDependencyFactory dependencyFactory)
    {
        _dependencyFactory = dependencyFactory;
    }

    public void DoSomething()
    {
        using(var dependency = _dependecyFactory.Create())
        { 
           // some code
        }

        // some code
    }

    public void DoSomething2()
    {
        using(var dependency = _dependecyFactory.Create())
        { 
           // some code
        }

        // some code
    }
}

Is this the solution? 这是解决方案吗? I'm in doubt, because: 我有疑问,因为:

  1. Such factories complicate code. 这样的工厂使代码复杂化。
  2. How should I instantiate an instance of Dependency in DependencyFactory method Create? 我应该如何在DependencyFactory方法Create中实例化Dependency的实例? Only use the New keyword isn't good because in this case I lose Unity interception logic (logging, caching, etc.). 仅使用New关键字是不好的,因为在这种情况下,我会丢失Unity拦截逻辑(记录,缓存等)。

I think here the factory approach is the solution . 我认为这里的工厂方法是解决方案 I've been in same situation, and my solution was also factory. 我一直处在同样的情况下,我的解决方案也在工厂中。 It fits your requirementfor providing abstract way of creating instances of types. 它适合您提供创建类型实例的抽象方式的要求。

About the interception logic (cache, logg .. ). 关于拦截逻辑(缓存,logg ..)。 Your factory still implements interface, so you can use the interceptors for the .Create method or any other which is party of the interface. 您的工厂仍然实现接口,因此可以将拦截器用于.Create方法或作为接口参与方的任何其他方法。

If methods of your disposable object also needs to be intercepted i can this of this approach: 如果您的一次性对象的方法也需要拦截,我可以使用以下方法:

You can inject the container in the factory and use the container withing the factory to create instances of the disposable object, this way you will keep all interception logic. 您可以在工厂中注入容器,并与工厂一起使用该容器来创建一次性对象的实例,这样您将保留所有拦截逻辑。 But you should be carefull, abused usage of container is leading to really bad written code. 但是您应该小心, 滥用容器的使用会导致非常糟糕的书面代码。 Limiting the injection of the container to the Factories is still acceptable from my point of view. 从我的角度来看,将容器的注入限制为工厂仍然是可以接受的。

I hope i helped ;] 希望我能帮上忙;]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM