Flexibly utilizes the CRUD operation logic in the design mode and model.

Source: Internet
Author: User

Now, a lot of our work is actually crud, our Aspx. the CS page is filled with a lot of crud logic. when modification is required, it takes a lot of time to search for and modify, and each page needs to be modified, which requires a lot of work. why? We have not encapsulated the CRUD operation logic. This problem can be solved through the template method and other design patterns.
  
I. How can I find this problem?
After a lot of projects, I found that most of my work involves the following steps when I write basic CRUD operations:
1. Update needs to be initialized from the data object entity to the page.
2. Check the data entered on the page.
3. Create a data object after checking that there is no wrong input.
4. Write the data object to the database.
5. Read and delete are both very simple.
  
I have done these things many times, but why have I done so many times? I think they can be encapsulated. when I saw the template method of <in-depth introduction to design patterns>, I came to the inspiration. I only need to find the crud algorithm to encapsulate them. in fact, I have found this algorithm. the above are the steps of this algorithm, but not every step is executed.

Ii. Design
After knowing the requirements, the following is the design.

    

Here I only need to introduce the abstractadd class. it is an abstract class that encapsulates the crud algorithm. at the beginning, I only encapsulate one of my pages, but I think other pages with the same operation can be simultaneously performed. however, because different pages may operate on different value type objects. so I also need to abstract the types. the model can meet such requirements.

Abstractadd
Public abstract class abstractadd <t, webpage>
Where webpage: Page
Where T: New ()
{
Private ientitycontrol <t, webpage> entitycontrol;
Private icache <t, webpage> operatecache;
Private icruddao <t, string> cruddao;

Public ientitycontrol <t, webpage> entitycontrol
{
Set {This. entitycontrol = value ;}
Get {return this. entitycontrol ;}
}

Public icache <t, webpage> operatecache
{
Set {This. operatecache = value ;}
Get {return this. operatecache ;}
}

Public icruddao <t, string> cruddao
{
Set {This. cruddao = value ;}
Get {return this. cruddao ;}
}

Public void add (webpage page)
{
T entity = createentity (PAGE );
If (entity! = NULL)
{
If (cruddao. Create (entity) = 0)
Operatecache. savecache (cruddao. findall (), page );
}
Else
Throw new exception ("entity is null ");
}

Private t createentity (webpage page)
{
If (entitycontrol. Check (page ))
Return entitycontrol. createentityfromconytrol (PAGE );
Else throw new exception ("Check Control unsucessful .");
}

Public void BIND (webpage page)
{
If (isbind ())
{
Entitycontrol. bindcontrol (operatecache. getcache (PAGE), page );
}
}

Public bool isbind ()
{
Return true;
}

Public abstract void Init ();

// Public abstract bool check ();
// Public abstract t initentityfromconytrol ();
// Public abstract void bindcontrol (ilist <t> T );

// Public abstract int create (t );
// Public abstract ilist <t> findall ();

// Public abstract void savecache (ilist <t> T );
// Public abstract ilist <t> getcache ();


}

The annotated part is actually the prototype of the template method mode. Later I abstracted them into interfaces.

Abstractadd abstract classes depend on these three interfaces, which are:
The ientitycontrol interface abstracts the conversion between the page control and the data type entity.
The ioperatecache interface abstracts the actions related to cache operations, such as writing the set read from the database to the cache and reading the set from the cache.
The icruddao interface is a common crud interface that allows BLL and interface to be decrypted.

Iii. Talking about the learning Design Model

There is no interface in the template method mode. I have extended it. the steps of the basic algorithm in abstract are decomposed and abstracted into interfaces. In this way, the algorithm is only dependent on the abstract algorithm steps, and the algorithm is targeted at abstract programming, instead of programming.
The abstracted interface can be expanded at will. For example, the icruddao interface can be implemented for business objects generated by BLL to complete data access operations.
Different BLL objects have different policies for implementing the icruddao interface. In fact, this is a policy mode.

Learning the design pattern for such a long time, the biggest feeling is that you cannot simply remember how to write the code of each pattern. What's important is to understand what they mean and what changes they encapsulate, what design principles are used. in fact, in my opinion, some models are essentially the same, which is why you find that the structure of the models is very similar. they only encapsulate different changes. For example, the factory mode encapsulates the changes caused by the creation of different objects, the iterator mode encapsulates the changes of different iterations due to the use of different sets, but they are essentially the same.

Iv. Description
The practices described in this article are only suitable for non-associated crud operations. If your domain mode is well established and has many associations, you need to use Orm, currently, I only implement a single CRUD operation. but where is the real domain model now! The associated fields are all set as attributes! This is also usable. The project we are working on is like this, and I also have the impulse to do this thing.

The sample code can be downloaded here.
  

 

Related Article

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.