Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object disposal with dependency injection

I have created a repository class that I want to use in a code behind page. I'm using constructor injection in the code behind page to instantiate the repository.

Repository class:

BritanniaPremierEntities PBEntities = new BritanniaPremierEntities();

public IQueryable<TradeRoutes> GetRoutes()
{
    var routes = PBEntities.TradeRoutes.OrderBy(c => c.ConsignmentDate);        

    return routes;
}

public IQueryable<TradeRoutes> GetExpiredRoutes()
{
    var routes = PBEntities.TradeRoutes.Where(
        c => c.ConsignmentDate <= System.DateTime.Now);

    return routes;
}

Code behind page

private IRepository repos;

public Admin_TradeRoutesAdmin()
    : this(new Repository()) 
{
}

public Admin_TradeRoutesAdmin(IRepository repos)
{
    this.repos = repos;
}

public IQueryable GetTradeRoutes()
{        
    // call repository method
    return repos.GetRoutes();
}

Here is where I get a little confused. How should I ensure the repository is disposed correctly? For instance, I'm unable to wrap the repository calls in using statements in the code behind page, thus making use of the dispose method in the repository.

like image 981
hoakey Avatar asked Jan 03 '11 10:01

hoakey


People also ask

Does dependency injection dispose?

Disposal of services The container is responsible for cleanup of types it creates, and calls Dispose on IDisposable instances. Services resolved from the container should never be disposed by the developer. If a type or factory is registered as a singleton, the container disposes the singleton automatically.

What are the three types of dependency injection?

There are three main styles of dependency injection, according to Fowler: Constructor Injection (also known as Type 3), Setter Injection (also known as Type 2), and Interface Injection (also known as Type 1).

What is the main purpose of using dependency injection?

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

How do dependency injection containers work?

A DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.


2 Answers

You should employ the Register Resolve Release pattern.

Specifically you should remember to always Release what you Resolve. It's the responsibility of the Composer to keep track of whether or not the dependency should be disposed. This is not trivial as it depends on different factors:

  • Does the dependency implement IDisposable?
  • Does the dependency's lifetime indicate that it should be disposed now or later?

This is such a complex task that you should use a proper DI Container for the job.

However, keep in mind that this ultimately depends on whether or not your DI Container supports decommissioning. For example, Castle Windsor does while StructureMap doesn't.

like image 78
Mark Seemann Avatar answered Sep 30 '22 12:09

Mark Seemann


Well, the idea generally is that whatever gets created by a DI container gets disposed, provided it is an IDisposable. The only issue is when that happens. I suspect there might be differences between different containers, but my take on this would be to implement Dispose() in the object being created, and explicitly calling Dispose() on the object being injected.

like image 43
Dmitri Nesteruk Avatar answered Sep 30 '22 12:09

Dmitri Nesteruk