简体   繁体   中英

ASP.net web forms where to dispose of entity context using UnitOfWork pattern

Throughout my web app I am using a UnitOfWork class to handle all my interactions with the data source. Below is the interface it inherits.

    public interface IUnitOfWork : IDisposable
{
    ActivityService ActivityService { get; }
    AmendmentService AmendmentService { get; }
    AspUserService AspUserService { get; }
    AttachmentService AttachmentService { get; }
    CampaignService CampaignService { get; }
    CommentService CommentService { get; }
    EventRegistrationService EventRegistrationService { get; }
    GroupService GroupService { get; }
    InstanceService InstanceService { get; }


    void Commit();
}

It inherits Idisposable so i dispose the entity context during the UnitOfWorks Dispose method. However when using this class in my web pages i am never sure whether i should create one UnitOfWork class as a variable of the page like so.

public partial class Members_Request : BasePage
{
 UnitOfWork uow = new UnitOfWork();

 protected void Page_Load(object sender, EventArgs e)
{
  Instance instance = uow.InstanceService.GetById(base.instanceId)
}


protected void Page_PreRender(object sender, EventArgs e)
{ 
//I don't think disposing here will work in all scenarios
uow.Dispose();
}

private void SomeOtherMethod(string name)
{ 
  Group newGroup = new Group{Name = name}

  uow.GroupService.Add(group))
  uow.Commit();
}

}

Doing it this way i need to be careful when i dispose for example, if i need the UnitOfWork in the Page_PreRender method but it never gets called becuase the request was a Callback, then my context will never get disposed. I am not sure if i should be disposing it during every method i use it then simply reinitialize it as a new UnitOfWork when i need it again to ensure it is always disposed.

An alternative way which ensures it gets dispose is to create a new UnitOfWork for every method i need it in so the above exmaple would become

  public partial class Members_Request : BasePage
{


 protected void Page_Load(object sender, EventArgs e)
{
  UnitOfWork uow = new UnitOfWork();

  Instance instance = uow.InstanceService.GetById(base.instanceId)

  uow.Dispose();
}

private void SomeOtherMethod(string name)
{ 
  UnitOfWork uow = new UnitOfWork();

  Group newGroup = new Group{Name = name}

  uow.GroupService.Add(group))
  uow.Commit();

  uow.Dispose();
}

}

So my question is how can i ensure that my UnitOfWork is always disposed when i share the same one each request, or should i just be creating many UnitOfWork objects whenever i need them and disposing them straight away. I think a solution by sharing the UnitOfWork would be better as it reducing code bloat.

Edit -- One alternative way i am thinking of doing this is to have this code in my base page

protected UnitOfWork uow = new UnitOfWork();

    protected override void OnUnload(EventArgs e)
    {
        uow.Dispose();
        base.OnUnload(e);
    }

Then i think i can just use the same instance of uow throughout the lifecycle and know it will always be disposed.

I use this: override dispose in every page

public override void Dispose()
    {
        if(uow != null)
        {
            uow.Dispose();
        }
        base.Dispose();
    }

uow is my UnitOfWork

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM