Entityframework usage (4) repository and unitofwork

Source: Internet
Author: User

Based on the code generated by codefirst in the previous article, we will continue to explore how to use it.

The repository mode is introduced to define the simplest irepository interface, which only contains the addition, deletion, modification, and Query Interfaces,

1   public interface IRepository<T>2     where T : class3   {4     IQueryable<T> Query();5     void Insert(T entity);6     void Update(T entity);7     void Delete(T entity);8   }

The unitofwork mode is introduced. Because entityframework is responsible for failure rollback, only the submission method is defined here.

1   public interface IUnitOfWork2   {3     void Commit();4   }

Implement the irepository interface,

 1   public class Repository<T> : IRepository<T> where T : class 2   { 3     private readonly IObjectSetFactory _objectSetFactory; 4     private readonly IObjectSet<T> _objectSet; 5  6     public Repository(IObjectSetFactory objectSetFactory) 7     { 8       _objectSetFactory = objectSetFactory; 9       _objectSet = objectSetFactory.CreateObjectSet<T>();10     }11 12     #region IRepository<T> Members13 14     public IQueryable<T> Query()15     {16       return _objectSet;17     }18 19     public void Insert(T entity)20     {21       _objectSet.AddObject(entity);22     }23 24     public void Update(T entity)25     {26       _objectSet.Attach(entity);27       _objectSetFactory.ChangeObjectState(entity, EntityState.Modified);28     }29 30     public void Delete(T entity)31     {32       _objectSet.DeleteObject(entity);33     }34 35     #endregion36   }

Implement the iunitofwork interface,

 1   public class UnitOfWork : IUnitOfWork, IDisposable 2   { 3     private readonly IObjectContext _objectContext; 4  5     public UnitOfWork(IObjectContext objectContext) 6     { 7       _objectContext = objectContext; 8     } 9 10     #region IUnitOfWork Members11 12     public void Commit()13     {14       _objectContext.SaveChanges();15     }16 17     #endregion18 19     #region IDisposable Members20 21     public void Dispose()22     {23       if (_objectContext != null)24       {25         _objectContext.Dispose();26       }27 28       GC.SuppressFinalize(this);29     }30 31     #endregion32   }

The implementation of the customerrepository class requires some configuration,

 1     public CustomerRepository() 2     { 3       Mapper.CreateMap<DomainModels.Customer, Customer>(); 4       Mapper.CreateMap<Customer, DomainModels.Customer>(); 5  6       DbContext context = new RETAILContext(); 7       DbContextAdapter contextAdaptor = new DbContextAdapter(context); 8  9       IObjectSetFactory objectSetFactory = contextAdaptor;10       _repository = new Repository<Customer>(objectSetFactory);11 12       IObjectContext objectContext = contextAdaptor;13       _uow = new UnitOfWork(objectContext);14     }

Then the logic implementation for adding, deleting, modifying, and querying is as follows,

 1     public void InsertCustomer(DomainModels.Customer customer) 2     { 3       Customer entity = Mapper.Map<DomainModels.Customer, Customer>(customer); 4  5       _repository.Insert(entity); 6       _uow.Commit(); 7  8       customer.Id = entity.Id; 9     }10 11     public void UpdateCustomer(DomainModels.Customer customer)12     {13       Customer entity = _repository.Query().Single(c => c.Id == customer.Id);14 15       entity.Name = customer.Name;16       entity.Address = customer.Address;17       entity.Phone = customer.Phone;18 19       _repository.Update(entity);20 21       _uow.Commit();22     }

It can still work in the same example,

1 icustomerrepository customerrepository = new customerrepository (); 2 3 // ================================ 4 console. foregroundcolor = consolecolor. darkred; 5 6 domainmodels. customer customer1 = new domainmodels. customer () 7 {8 name = "Dennis GAO", 9 address = "Beijing", 10 phone = "18888888888", 11}; 12 customerrepository. insertcustomer (customer1); 13 console. writeline (customer1 );

At the same time, unitofwork can ensure that related business operations are in the same transaction,

Complete code and Indexing

How to Use entityframework

  • (1) databasefirst
  • (2) codefirst
  • (3) codefirst smooth API
  • (4) repository and unitofwork
  • (5) Introduce unity
  • (6) static repository
  • (7) thread security practices
  • (8) Transaction Processing

Download complete code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.