5

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.

Wim Coenen
67k14 gold badges162 silver badges253 bronze badges
asked Jan 3, 2011 at 10:25

2 Answers 2

5

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.

answered Jan 3, 2011 at 10:58
Sign up to request clarification or add additional context in comments.

3 Comments

There's a heck of a lot to think about, but I get the general concept of RRR. I've looked into castle windsor. Is it the component lifestyle that resolves this issue then?
Yes, but indirectly most of the times. For Windsor specifics, see: kozmic.pl/2010/08/27/…
Very informative thanks. I'm going to resist digging too far into this and just except that some "magic happens".
2

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.

answered Jan 3, 2011 at 10:42

1 Comment

This is the better answer - the resource being injected needs to be disposed and IDisposable implemented on the container. No need for the RRR pattern for a simple case like this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.