4

I'm creating an EntityFramework object in the default constructor of my Controller.

To free the memory after calling action method inside any controller, I want to make the controller disposable. Is this a good idea?

public somethingController : Controller , IDisposable 
{
    // implement the dispose method here 
    public void Dispose ()
    {
        EntityFrameWorkObject.Dispose();
    }
}

what do you think?

3 Answers 3

2

I recommend IHttpModule implementation for dispose datacontext object. My actual code working with Microsoft unity.

public void Init(HttpApplication application)
{
    application.EndRequest += new EventHandler(this.Application_EndRequest);
}

private void Application_EndRequest(object source, EventArgs e)
{
    IoCWorker.Resolve<IRepositoryContext>().Terminate();
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot you remined me in very important point . I'm still beginner in this kind of patterns like Microsoft unity . do you have any other simple solutions because I don't want to recode the whole app , and by the way any useful links , books for unity ??!! ;) thanks
0

Yes, that is good idea. Actually it's recommended pattern and is generally used. If you want to have class wide object, and want to free its resources after class is freed, you do it in Dispose()

Comments

0

Yes, that is right.

public somethingController : Controller 
{
    // implement the dispose method here 
    public void Dispose ()
    {
        EntityFrameWorkObject.Dispose();
    }
}

You don't need to add IDisposable because the controller call already implements it.

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.