简体   繁体   中英

Entity Framework, UnitofWork pattern with dispose method

A sample of uow:

using System;
using ContosoUniversity.Models;

namespace ContosoUniversity.DAL
{
    public class UnitOfWork : IDisposable
    {
        private SchoolContext context = new SchoolContext();
        private GenericRepository<Department> departmentRepository;
        private GenericRepository<Course> courseRepository;

        public GenericRepository<Department> DepartmentRepository
        {
            get
            {

                if (this.departmentRepository == null)
                {
                    this.departmentRepository = new GenericRepository<Department>(context);
                }
                return departmentRepository;
            }
        }

        public GenericRepository<Course> CourseRepository
        {
            get
            {

                if (this.courseRepository == null)
                {
                    this.courseRepository = new GenericRepository<Course>(context);
                }
                return courseRepository;
            }
        }

        public void Save()
        {
            context.SaveChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}

As you see uow includes dispose method and inside it disposes dbContex object. Why should we explicitly dispose the dbContext object. Since it is a member of uow, outside the scope it will disposed by garbace collector automaticly. So, why are we doing this manually? As an example:

using(Uow uowObject = new Uow())
{
      //there is a dbcontext
}
  //it will be disposed automaticly by gc

Outside the scope the variable is no longer reachable but that doesn't mean disposed. As a rule of thumb, every class that implements IDisposable should be disposed. In the case of EF, it will clear the cache, the graph that tracks object changes and rollback any uncommitted transactions as well.

With GC, you don't know when GC will be started. Even the variable is out of scope, it doesn't mean it has been garbage collected. With dispose pattern, you can free the memory right away.

From MSDN :In determining when to schedule garbage collection, the runtime takes into account how much managed memory is allocated. If a small managed object allocates a large amount of unmanaged memory, the runtime takes into account only the managed memory, and thus underestimates the urgency of scheduling garbage collection.

So for the managed objects which hold native resource, you should call dispose to free the native resource.

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