1

I'm a little confused on how to approach EF development when looking at all these articles together, since I can't find a sample that addresses all of these practices in one place:

Question

  1. Should I call Dispose? Where should I call it to ensure proper lifetime of the context? ... In an MVC app this seems to be done by overriding the controller's dispose method.

  2. How should I dispose of the Azure cache linked above? ... Perhaps ObjectCache is the only object I should be concerned about.

  3. Should I use Using, or is using untrustworthy?

  4. Should Microsoft produce a sample that addresses all these issues? What would that sample look like? (if it's not this one) Most samples I see with EF + MVC have a varying and inconsistent implementation. I'm not sure who to imitate in my project.

Community
  • 1
  • 1
makerofthings7
  • 60,103
  • 53
  • 215
  • 448

3 Answers3

1

You have drawn conclusions unnecessarily.

The WCF issue is a desgn flaw. Microsoft screwed up. It happens sometimes.

I no longer have the reference, but I found it by searching, and you can too. There was a point during the design of WCF where the question came up, "should Dispose() just always call Close()". When the question came to Don Box (the Chief Architect of WCF or some such title), he "couldn't think of any reason why not". He missed a reason why not.

Dispose() must not throw exceptions. This is because of the following:

try
{
    var proxy = null;
    try
    {
        proxy = new ProxyClass();
        throw new Exception1();
    }
    finally
    {
        if (proxy != null) proxy.Dispose(); // What happens if this throws Exception2?
    }
}
catch (Exception ex)
{
    // Which exception do I see in here?
}

If Dispose() throws Exception2, then I will lose Exception1 along with the stack trace showing me what happened. The problem is that Mr. Box found no reason why Dispose() shouldn't just call Close() to do the job. The problem is that, with some bindings, Close() actually has to do some work. This is the case with wsHttpBinding, where there is a message exchange upon Close(). This means ther's a real chance of Close() throwing an exception, trashing my call stack.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I've been looking for this kind of explanation of the WCF issue for years (literally) . Thank you! Does that mean that this sample is the most complete and accurate one I should follow with regard to disposing an MVC object? ... and that the lifetime of the context is maintained appropriately? http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application – makerofthings7 Nov 19 '11 at 08:03
  • I see no relationship between what I wrote and any other kind of object that implements IDisposable. In general, if it implements IDisposable, and if you created it, then ensure that `Dispose()` is called before the object goes out of scope. The best way to do that, for most purposes, is to use a `using` block. WCF is one of the very few exceptions to that rule. – John Saunders Nov 19 '11 at 08:14
0

Start checking your facts - they are wrong. The article about using being unreliable does not say it is unreliable. WCF objects that are in a faulted state can not be disposed - they are already self-disposed. This is why another error occurs. It does not mean using does not call dispose. Given that this fact is wrong, I would not even tart commenting on your conclusions - because obviously your facts are wrong.

TomTom
  • 61,059
  • 10
  • 88
  • 148
  • I updated the text to "using has issues in WCF". Do those issues relate to my dealings with the EF context? Well I'm not qualified to answer. I'm just being thorough as I attempt to figure out who is the right person to listen to regarding EF. Seems like everyone has an opinion, I'm looking for the answer that is backed up by the most facts. I'll read your prior posts to see what I can learn from them. Thanks! – makerofthings7 Nov 19 '11 at 07:30
0

The basic idea is very simple. If you are using IDisposable instances you need to call the Dispose method. The problem is the scope of those instances and when you are going to dispose them. Better to dispose them as soon as you are done with them.

People implement the repository pattern in various ways depending on their requirements. So those implementations may not work for you. Work out whats best for you.

This post says my MVC applications should override Dispose() to get rid of the context

I never said [you] should over ride the method

Eranga
  • 32,181
  • 5
  • 97
  • 96
  • 1
    I updated the text, please don't take offense to the word "should", I'm just trying to understand when to implement the techniques you mention. Your expertise and guidance is greatly appreciated – makerofthings7 Nov 19 '11 at 07:26