1
votes

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.

I want to rewrite code using Unity container:

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? Only use the New keyword isn't good because in this case I lose Unity interception logic (logging, caching, etc.).
1
Can you rewrite your example code to show the actual code and class named in play here? What is Dependency for type and why do you need to dispose it at the end of each method? - Steven
This is a generic example frequently encountered in my project. Lifetime of MyClass much longer than the Dependency. IDisposable-objects (unmanaged resources) must be disposed as soon as possible and used only in blocks of code where they are needed. - Hopeless

1 Answers

1
votes

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 .. ). Your factory still implements interface, so you can use the interceptors for the .Create method or any other which is party of the interface.

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 ;]