Preparation Specification for Common Codes (4) --- Saveness and Update of Objects

xiaoxiao2021-03-06  50

Code for common code (4) --- Sales and Update of Object December 10, 2004

(4) Sales and updates of objects

How to store and update the object, which is troubled by many people, usually we will save the object's assignment with objects and update on different two layers: interface layer, data access layer. In the interface layer, by unified methods such as: Save, the data access layer is distinguished in INSERT or UPDATE, for this, we can design a simple class level, such as cobject, other physical classes inherit from this object, and implement corresponding INSERT and Update method can simplify our programming.

Public Class COBject

{

Private bool misnew;

Private guid mobjectId;

Public COBJECT ()

{

MOBJECTID = Guid.newguid ();

Misnew = true;

}

Public COBJECT (GUID GuidObject)

{

THIS.MOBJECTID = GuidObject;

THIS.LOAD ();

Misnew = false;

}

Public void save ()

{

IF (Misnew)

{

INSERT ();

Misnew = false;

}

Else

Update ();

}

Protected virutal void insert ();

protected virutal void update ();

Protected Virual Void Load ();

}

Each subclass inherits from COBJECT to achieve corresponding methods, such as CPERSON

Public Class CPERSON: COBJECT

{

protected override void insert ()

{

String ssql = "Insert INTO TPERSON";

DBHELPER.EXECUTENORTURN (SSQL);

}

}

After designing the above level, we can complete our needs in the interface code, in accordance with the common code preparation specification (3) - to write assignment and save methods in the preservation of the object, you can complete our needs.

If we use the E / R MAPPING tool, generally these tool classes will do the above work for us, and do not need to realize the INSERT and UPDATE methods in subclasses, which can be more convenient to complete our business. In addition, some E / R MAPPING tools also have a cache mechanism that can further improve performance. At this stage, the use of SQL batch acquisition data and use the E / R mapping tool for entity access and saving is a relatively high efficiency.

Currently preferred E / R mapping tools,: Hibernate, NHibernate, Grove, PDO, and more.

转载请注明原文地址:https://www.9cbs.com/read-82436.html

New Post(0)