1

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.).
2
  • 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? Commented Feb 15, 2017 at 12:55
  • 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. Commented Feb 15, 2017 at 13:40

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.