views:

44

answers:

2

Hi All,

In my application I am making use of Spring.Net for IoC. The service objects are called from the ASP.Net files to perform CRUD operations using these service object. For example, I have CustomerService to do all CRUD operations on Customer table. I use entity framework and the entities are injected .. my question is where do I call the dispose method?

As far as I understood from the API documentations, unless I call Dispose() there is no guaranty it will be garbage collected! So where and how do I do it?

Example Service Class:

 public class CustomerService
{
    public ecommEntities entities = {get; set;}

    public bool AddCustomer(Customer customer)
    {
        try
        {
            entities.AddToCustomer(customer);
            entities.SaveChanges();
            return true;
        }
        catch (Exception e)
        {
            log.Error("Error occured during creation of new customer: " + e.Message + e.StackTrace);
            return false;
        }            
    }

    public bool UpdateCustomer(Customer customer)
    {
        entities.SaveChanges();
        return true;
    }

public bool DeleteCustomer(Customer customer)
.
.
.   

And I just create an object of CustomerService at the ASP partial class and call the necessary methods.

Thanks in advance for the best practice and ideas..

Regards,

Abdel Raoof

+1  A: 

I have HttpModule in my application, which takes care of creating and disposing of context:

public class MyEntitiesHttpModule : IHttpModule
{
    public void Init(HttpApplication application)
    {
        application.BeginRequest += ApplicationBeginRequest;
        application.EndRequest += ApplicationEndRequest;
    }

    private void ApplicationEndRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Items[@"MyEntities"] != null)
            ((MyEntities)HttpContext.Current.Items[@"MyEntities"]).Dispose();
    }

    private static void ApplicationBeginRequest(Object source, EventArgs e)
    {
        //TextWriter logFile = File.CreateText(HttpContext.Current.Server.MapPath("~/sqllogfile.txt"));
        var context = new MyEntities();
        //context.Log = logFile;
        HttpContext.Current.Items[@"MyEntities"] = context;
    }
}

It creates it at the beginning of request and disposes at the end. Context is taken from HttpContext.Current.Items by Ninject and injected into my service object. I don't know how exactly it works with Spring.Net and what is your approach to creating ObjectContext, but I hope this answer will help. If Sprint.Net supports per request lifetime and doesn't need any modules, it propably takes care of disposing too. In other case, you can do it like here.

LukLed
Thanks for your suggestions.. lets see what others have to say
Abdel Olakara
+1  A: 

@LukLed is generally correct (+1) that the HTTP request is the correct scope. However, I very much doubt explicit code is required to call Dispose() with either NInject or Spring.Net as both of these should already be disposing instances they manage. HttpContext.Current is as good a place to store the reference as anything.

However, you (@Abdel) are wrong to say:

As far as I understood from the API documentations, unless I call Dispose() there is no guaranty it will be garbage collected!

This is just not right. Dispose() makes the collection somewhat more optimal and provides for deterministic release of unmanaged resources (e.g., your DB connection). But everything will eventually be collected, with or without calling Dispose().

Craig Stuntz