Unit of work-unit of work (2)

Source: Internet
Author: User

Review

In the previous article, we used ADO Based on the working unit principle. net, but when the project requirements change, for example, you need to introduce the ORM framework to be compatible with the current ADO. net implementation method, the previous implementation can not meet this requirement.

Let's just talk about it. Let's reconstruct the work unit based on the changes in current needs.

Rebuilding unitofwork

First, let's look at the interface extracted from the previously implemented work unit. The Code is as follows:

public interface IUnitOfWork{    void RegisterAdd(string sql, params IDataParameter[] parameters);                                 void RegisterSave(string sql, params IDataParameter[] parameters);                               void RegisterRemove(string sql, params IDataParameter[] parameters);                              void Comit();}

The requirement must be compatible with the orm and ADO. Net methods, and the above interfaces only support the ADO. Net method. Therefore, the interface needs to be changed, for example:

// Other codes omit void registeradd (Object entity );

Observe the above changes and you will find that if you want to meet your needs, you need to determine that it is ADO. net or ORM operations, there are two different responsibilities, which is obviously unreasonable, but if we put ADO. if net and ORM are implemented by corresponding data layer classes, they will be subject to a single responsibility. After the above analysis, we can further modify the above interfaces, the Code is as follows:

// Other codes omit void registeradd (Object entity, iunitofworkrepository repository );

According to this idea, the number of iunitofworkrepository methods will be basically the same as that of iunitofwork (without commit). One is registration, while the other is actual operations, therefore, the interface code will be the same as the first change to object. The Code is as follows:

public interface IUnitOfWorkRepository{    void ExecuteAdd(object entity);                                 void ExecuteSave(object entity);                               void ExecuteRemove(object entity);}

With the above changes, iunitofwork can be implemented. The code structure is similar to sqlunitofwork. The difference is that a list <sqlentity> is used to store all cud operations, but now you must differentiate different types of operations. Therefore, you need to have containers that store cud separately. The general code is as follows:

public class UnitOfWork : IUnitOfWork{    private Dictionary<object, IUnitOfWorkRepository> m_addList =         new Dictionary<object, IUnitOfWorkRepository>();    private Dictionary<object, IUnitOfWorkRepository> m_saveList =        new Dictionary<object, IUnitOfWorkRepository>();    private Dictionary<object, IUnitOfWorkRepository> m_removeList =         new Dictionary<object, IUnitOfWorkRepository>();    public void RegisterAdd(object entity, IUnitOfWorkRepository repository)    {        if (!this.m_addList.ContainsKey(entity))            this.m_addList.Add(entity, repository);    }    public void RegisterSave(object entity, IUnitOfWorkRepository repository)    {        if (!this.m_saveList.ContainsKey(entity))            this.m_saveList.Add(entity, repository);    }    public void RegisterRemove(object entity, IUnitOfWorkRepository repository)    {        if (!this.m_removeList.ContainsKey(entity))            this.m_removeList.Add(entity, repository);    }    public void Commit()    {        using (TransactionScope trans = new TransactionScope())        {            foreach (var entity in this.m_addList.Keys)            {                this.m_addList[entity].ExecuteAdd(entity);            }            foreach (var entity in this.m_saveList.Keys)            {                this.m_saveList[entity].ExecuteSave(entity);            }            foreach (var entity in this.m_removeList.Keys)            {                this.m_removeList[entity].ExecuteRemove(entity);            }            trans.Complete();        }    }}

Now we have completed the reconstruction of the work unit. Next we can derive the implementation based on ADO. NET and ORM Based on iunitofworkrepository.

Rebuilding schoolrepository

First, let's take a look at the restructured code:

class schoolrepository: irepository, iunitofworkrepository {private idbconnection m_connection = NULL; private iunitofwork m_uow = NULL; Public schoolrepository (idbconnection connection, iunitofwork uow) {This. m_connection = connection; this. m_uow = uow;} public void add (Object entity) {This. m_uow.registeradd (entity, this);} public void save (Object entity) {This. m_uow.registersave (entity, this);} public void remove (Object entity) {This. m_uow.registerremove (entity, this);} public void executeadd (Object entity) {school = entity as school; using (idbcommand cmd = This. m_connection.createcommand () {cmd. commandtype = commandtype. text; cmd. commandtext = "insert school values (@ ID, @ name)"; cmd. parameters. add (New sqlparameter ("@ ID", school. ID); cmd. parameters. add (New sqlparameter ("@ name", school. name); cmd. executenonquery () ;}} public void executesave (Object entity) {// Code omitted} public void executeremove (Object entity) {// Code omitted}

Irepository is the basic interface of the data layer. From the code, we can see that the cud method was split into the cud and executexxx methods. The cud method is responsible for calling the iunitofwork interface, while executexxx implements specific database operations

Iunitofworkrepository implementation based on nhib.pdf

Check the code first.

Class schoolrepository: irepository, iunitofworkrepository {private iunitofwork m_uow = NULL; Public schoolrepository (idbconnection connection, iunitofwork uow) {This. m_uow = uow;} public void add (Object entity) {This. m_uow.registeradd (entity, this);} public void save (Object entity) {This. m_uow.registersave (entity, this);} public void remove (Object entity) {This. m_uow.registerremove (entity, this);} public void executeadd (Object entity) {sessionfactory. currentsession. add (entity);} public void executesave (Object entity) {// Code omitted} public void executeremove (Object entity) {// Code omitted }}

From the implementation based on Nhibernate, we can see that all executexxx Methods call methods related to nhibernatesession.

End

At this data layer, only query is performed. Next time, we will share the query mode.

The article ends here. If you have any questions or errors, please leave a message. Thank you!

Unit of work-unit of work (2)

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.