Single-piece mode and business logic service layer Encapsulation

Source: Internet
Author: User

At present, the idea of software layering has been popularized and has also achieved good results in projects I have done. However, there are also obvious disadvantages. When dealing with changes from the bottom up, it is often necessary to perform cascade modifications, especially changes in the database structure. In addition, if an ORM platform such as nhib.pdf is used, this is a bit better.
In the complex business logic layer, objects are usually very small in granularity, which is not convenient to use at the presentation layer, and duplicate code (such as regular initialization and number of data shards) will be generated ), this increases the learning difficulty and Development workload of the presentation layer developers. In this case, the service layer (Packaging layer) is often added to the business logic layer to reduce repetitive code and unnecessary complexity.
The disadvantage of adding a service layer is also obvious: reduced flexibility.
In this example, the service layer wraps the business logic, simplifying the development of the presentation layer. The service layer packaging class requires database connection parameters. Therefore, the single-piece mode is used, static attributes are used to further encapsulate the service layer objects, and multithreading is taken into account. In this case, you only need to modify the service layer packaging class for future changes to the service layer interface.

// Service layer packaging
Public class MyUserService
{
Public MyUserService (string strConn)
{
//......
}
//......
}
......
Public class BusinewwWapper
{
Private static MyUserService m_User;
Private static string m_StrConn;

Static BusinewwWapper ()
{
// Necessary Initialization
M_StrConn = ......;
}

Private static void SetUser ()
{
Lock (typeof (SmWFM ))
{
If (m_User = null)
M_User = new MyUserService (m_StrConn );
}
}

Public static MyUserService User
{
Get
{
If (m_User = null)
SetUser ();
Return m_User;
}
}
//......
}

In fact, with a framework like Spring. NET, this is unnecessary.

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.