Build a service layer

Source: Internet
Author: User
Build a service layer

Reading directory

  • Toddler
  • Cohesion
  • ① Dbcontact. CS
  • ② Idbcontact. CS
  • Key Points
  • Create a service layer
  • ③ Iuserinforservice. CS
  • ④ Userinforservice. CS
  • ⑤ Ibaseservice. CS
  • ⑥ Baseservice. CS
  • Next article
Back to Top

Although I was an unknown cainiao, I also had a dream to become a small architect from a background programmer. I knew that architects didn't just think about it.

I have been working for a while, and I want to get the tens of thousand salary after a year of graduation. Do not spray!

 

We are growing in a different environment. Some people may cultivate it with one hand. maybe only one person can work hard. Whatever the case,

We need to learn how to build wheels first, but I will only build four high-quality wheels. Let my car run steadily!

I am well aware that to achieve these plans, I must make hard effort!

 

These things will be nothing to mention, and they will not be tall.

We hope to bring readers new insights and knowledge.

Thank you for your support. Finally, I will upload the source code + database for the viewer.

Updated one after another. Update until you will !!

Back to Top

Article 2 creates a unique dbcontact that inherits idbcontact in a thread through a factory.

Create a unique modelcontainer in a thread that inherits the objextcontext and write it as the attribute of dbcontact.

And encapsulate the context savechange () into dbcontact.

Let the execution SQL control logic in the service layer. Of course, the savechange () of modelcontainer at the storage layer is removed ()

And write each warehouse as the corresponding attribute.

Purpose: The business logic (service layer) Only accesses the data layer through this class (warehousing Layer ).

Back to Top ① dbcontact. CS
Using lkbs. communicationplatform. idal; using system. data. objects; // context namespace using lkbs. communicationplatform. factory; using system. data. sqlclient; namespace lkbs. communicationplatform. dal {public class dbcontact: idbcontact {public objectcontext currentefcontext {Get; set;} isingleefcntextfactory singleefcntextfactory = new singleefcntextfactory (); // singleefcntextfactory implements a single dbcontact public dbconta CT () {currentefcontext = singleefcntextfactory. getcurrentefcontext (); //} public int excutesql (string plain text, sqlparameter [] parammeters) {return currentefcontext. executestorecommand (plain text, parammeters);} // In the warehouse layer, baserespository does not need savechang, so that it can control and submit the database at the service layer. // In the service layer, this is the business logic, based on a scenario in the business logic layer, multiple tables may be used in a method. Finally, save the file. Perform one interaction with the database: Public int savechange () {return this. currentefcontext. savechanges ();} public iuserinforrespository userinforresponsity {Get; Set ;}}}
Back to Top ② idbcontact. CS
Using system. data. objects; using system. data. sqlclient; // namespace lkbs in which the context is located. communicationplatform. idal {public interface idbcontact {// The attribute in the interface must be {} objectcontext currentefcontext {Get; set;} int excutesql (string plain text, sqlparameter [] parammeters); int savechange (); iuserinforrespository userinforresponsity {Get; set ;}//... store multiple other tables. Add OK in the T4 template }}
Go back to the top and check

 

⑥ Obtain the logic of the warehouse layer instance in baseservice. CS.

To obtain an instance, we write an abstract method in baseservice to obtain the instance,

In order to inherit from the self-class and ensure that this method must be implemented in the sub-class ④ userinforservice, we have written baseservice as an abstract class. Must be written!

When will it be executed? When the subclass is instantiated, the base class constructor is called. Therefore, we call this abstract method in the base class constructor to obtain the instance.

 

Okay, so much of this is actually the practical use of abstract classes and abstract methods. In addition, the base class constructor is called when subclass instantiation is used!

Finally, this chase is a loop. If you understand it, you will feel this way. This method achievesSubclass drop base class adjustment subclassCycle!

 

Go back to the creation of the top service layer

In the process of building this layer, I have a lot of loopholes. The wood has public or wood has Assembly reference or wood has using, and the interface object cannot fall out of the methods already in the instance interface, always warning.

You cannot directly assign values to non-static variables, attributes, or methods. In the end, they are all resolved. The final program does not have a warning. All pass.

The framework is almost built. In fact, I have already built the database, but it was not released.

In the next article, I will start to set up the front-end.

This process is a process of growth. I want to record it. There will be fewer such errors in the future. Even if an error occurs, I will immediately correct it.

Return to the top ③ iuserinforservice. CS
using LKBS.CommunicationPlatform.Model;namespace LKBS.CommunicationPlatform.IBll{ public interface IUserInforService : IBaseService<UserInfor>  {  }}
Back to Top ④ userinforservice. CS
Using lkbs. communicationplatform. ibll; using lkbs. communicationplatform. model; using lkbs. communicationplatform. idal; using lkbs. communicationplatform. dal; namespace lkbs. communicationplatform. bll {public class userinforservice: baseservice <userinfor>, iuserinforservice {public override void setcurrentrespository () {// at this time, dbcontact of the parent class cannot be found because this field does not have public this. currentrespository = This. dbcontact. userinforresponsity ;}}}
Back to Top ⑤ ibaseservice. CS
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace LKBS.CommunicationPlatform.IBll{ public interface IBaseService<T>  {  bool AddEntity(T entity);  bool DeleteEntity(T entity);  bool UpdateEntity(T entity);  T SelectOneEntity(Func<T, bool> wherelamda);  IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda);  IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount);  }}
Back to Top 6 baseservice. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using lkbs. communicationplatform. model; using lkbs. communicationplatform. idal; using lkbs. communicationplatform. dal; using lkbs. communicationplatform. factory; using system. data. entity; using system. runtime. remoting. messaging; namespace lkbs. communicationplatform. bll {public abstract class baseservice <t> where T: Class, new () {// No Assembly added: Using; The isingledbcontactfactory singledbcontactfactory = new singledbcontactfactory (); Public idbcontact dbcontact; Public baseservice () {dbcontact = singledbcontactfactory. createdbcotactuseef (); // obtain the key logic of the current Warehouse instance !!!!! This class is written as an abstract class and must be implemented as a subclass. // When the subclass is instantiated, the base class dog function is called, and the constructor executes the base class virtual method. The base class virtual method has the method to call the subclass implementation. Then continue to execute the code below the base class // This logic is the true understanding of object-oriented and the essence of the use of abstract classes .!! Setcurrentrespository ();} public abstract void setcurrentrespository (); // Error 2 type "T" must be a reference type before it can be used as a generic type or method // forget to add where T: Class, new () to the class () public ibaserespository <t> currentrespository; Public bool addentity (T entity) {currentrespository. addentity (entity); If (dbcontact. savechange ()> 0) // savechange cannot be found. This method is not available in the interface, but the subclass has 0.0 {return true;} else {return false ;}} public bool deleteentity (T entity) {currentrespository. deleteentity (entity); If (dbcontact. savechange ()> 0) {return true;} else {return false;} public bool updateentity (T entity) {currentrespository. updateentity (entity); If (dbcontact. savechange ()> 0) {return true;} else {return false ;}} /// <summary> /// individual entity /// </Summary> /// <Param name = "wherelamda"> </param> /// <returns> </returns> Public t selectoneentity (func <t, bool> wherelamda) {return currentrespository. selectoneentity (wherelamda );} /// <summary> /// query the entire table or multiple entities /// </Summary> /// <returns> </returns> Public iqueryable <t> selectallentitylist (func <t, bool> wherelamda) {return currentrespository. selectallentitylist (wherelamda);} int rowcount = 0; // The generic method <S> is the type of the constraint parameter. The parameter "automatic identification" <S> is used to specify the parameter. <S> is required for definition. Calling this method can be omitted: Because the passed parameter is determined, the type of <S> Public iqueryable <t> selectpageentitylist <S> (func <t, s> orderlamda, func <t, bool> wherelamda, int pageindex, int pagesize, bool isasc, out int rowcount) {// non-page pagecount, which is the total number of rows return currentrespository. selectpageentitylist <S> (orderlamda, wherelamda, pageindex, pagesize, isasc, out rowcount );}}}
Back to Top next article

I have already built a database. Of course, there is still a need to improve the database. After all, the demand analysis needs to change.

In the end, I will send it up. I will not send it now, because the architecture cannot use the database. At last, I will devote myself to the manual debugging. This is a small "dry goods. Self-Encouragement

The use of the T4 template, the generation of database tables, and the full ing of database tables.

In the next article, I started to set up the front-end. This is estimated to take several days.

 

Build a service layer

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.