In the previous articleArticleThat encapsulation makes development quite cool. Well, I admit it is a database-oriented programming thinking. In a class, almost all operations on a single table are encapsulated. In my opinion, because ORM encapsulates all operations that are dealing with databases, it is the data exchange layer in the project,Because this generic class encapsulates operations on a single object, we have independent basic operations on all objects to achieve the highest level of reuse, this enables us to focus on Object relationship processing and complex business logic.. (Hope experienced people can give me guidance or discuss with each other)
1. Support for returning encapsulation to normal multi-database
Public class basedao <t> where T: entity {public dbsession dB; Public basedao (dbsession _ dB) {DB = _ dB;} public basedao (): This (dbsession. default) {}# region "query" /// <summary> /// obtain the object through the condition /// </Summary> /// <Param name = "where"> </ param> // <returns> if your condition is to obtain a single object, list T [0] is the object </returns> Public Virtual list <t> get_entity_bywhere (whereclip where, orderbyclip orderby, Params field [] fields) {return dB. from <t> (). where (where ). select (fields ). orderby (orderby ). tolist ();} /// <summary> /// obtain the able Data Set /// </Summary> /// <Param name = "where"> </param> /// <Param name = "orderby"> </param> // <Param name = "fields"> </param> // <returns> </returns> Public Virtual datatable get_entity_bywhere_totable (whereclip where, orderbyclip orderby, Params field [] fields) {return dB. from <t> (). where (where ). select (fields ). orderby (orderby ). totable ();} /// <summary> /// obtain a single object by using conditions /// </Summary> /// <Param name = "where"> </param> /// <param name = "fields"> </param> // <returns> </returns> Public Virtual t get_singleentity_bywhere (whereclip where, params field [] fields) {return dB. from <t> (). where (where ). select (fields ). tosingle ();} /// <summary> /// obtain all the data /// </Summary> /// <returns> datatable </returns> Public Virtual datatable get_alldata_table () {return dB. from <t> (). totable ();} /// <summary> /// obtain all the data /// </Summary> /// <returns> List <t> </returns> Public Virtual list <t> get_alldata_list () {return dB. from <t> (). tolist ();} /// <summary> /// obtain the top data records /// </Summary> /// <Param name = "TOP"> </param> /// <Param name = "where"> </param> /// <Param name = "orderby"> </param> /// <returns> </returns> Public Virtual list <t> get_entitys_bytop (INT top, whereclip where, orderbyclip orderby, Params field [] fields) {If (Top = 0) throw new exception ("Top value cannot be 0"); Return dB. from <t> (). where (where ). select (fields ). gettop (top ). orderby (orderby ). tolist ();} # endregion # region "Update" /// <summary> /// update a record /// </Summary> /// <Param name = "entity"> </Param >/// <returns> </returns> Public Virtual bool update_entity (T entity) {entity. attach (); Return dB. save <t> (entity)> 0 ;} /// <summary> /// update an object using multiple conditions /// </Summary> /// <Param name = "fields"> </param> /// <Param name = "values"> </param> /// <Param name = "where"> </param> /// <returns> </returns> Public Virtual bool update_entity_bywhere (field [] fields, object [] values, whereclip where) {return dB. update <t> (fields, values, where)> 0 ;} /// <summary> /// update an object using a single condition /// </Summary> /// <Param name = "fields"> </param> // <param name = "values"> </param> // <Param name = "where"> </param> // <returns> </returns> Public Virtual bool update_entity_bywhere (field filed, object value, whereclip where) {return dB. update <t> (filed, value, where)> 0 ;} # endregion # region "delete" // <summary> // delete a record from an object /// </Summary> /// <Param name = "entity"> </ param> // <returns> </returns> Public Virtual bool delete_entity (T entity) {return dB. delete <t> (entity)> 0 ;} /// <summary> /// delete from the primary key ID set /// </Summary> /// <Param name = "idlist"> </param> // <returns> </returns> Public Virtual int delete_entitys (list <string> idlist) {return delete_entitys (idlist. toarray ());} /// <summary> /// delete from the primary key ID set /// </Summary> /// <Param name = "idlist"> </param> // <returns> </returns> Public Virtual int delete_entitys (string [] idlist) {return dB. delete <t> (idlist) ;}/// <summary> /// you can specify the where = t condition when deleting objects in batches by using the condition. id. in (OBJ []) /// </Summary> /// <Param name = "where"> </param> /// <returns> </returns> Public Virtual bool delete_entity (whereclip where) {return dB. delete <t> (where)> 0 ;}# endregion # region "added" /// <summary> // Add an new record, /// </Summary> /// <Param name = "entity"> </param> /// <returns> </returns> Public Virtual bool add_entity (T entity) {return dB. save <t> (entity)> 0 ;} # endregion # region "pagination" /// <summary> // The total number of records required for displaying data by PAGE /// </Summary> /// <Param name = "currentpageindex"> current page number </param> /// <Param name = "pagesize"> pagesize </param> /// <Param name = "where"> whereclient </param> // /<Param name = "orderby"> orderbyclip </param> // <Param name = "record"> total number of records </param> // <returns> </ returns> Public Virtual list <t> get_entity_bypage (INT currentpageindex, int pagesize, whereclip where, orderbyclip orderby, out int record, Params field [] fields) {record = get_entity_record (where); Return get_entity_bypage (currentpageindex, pagesize, where, orderby, fields );} /// <summary> /// return able by PAGE /// </Summary> /// <Param name = "currentpageindex"> </param> /// <Param name = "pagesize"> </param> // <Param name = "where"> </param> // <Param name = "orderby"> </param>/ // <Param name = "record"> total records </param> /// <Param name = "fields"> </param> /// <returns> </returns> Public Virtual datatable get_entity_bypage_totable (INT currentpageindex, int pagesize, whereclip where, orderbyclip orderby, out int record, Params field [] fields) {record = get_entity_record (where); Return partition (currentpageindex, pagesize, where, orderby, fields );} /// <summary> /// obtain the number of records in the table using the conditions /// </Summary> /// <Param name = "where"> </param> // /<returns> </returns> Public Virtual int get_entity_record (whereclip where) {return dB. from <t> (). where (where ). count ();} /// <summary> /// the total number of records that do not need to be recorded on the page. // </Summary> /// <Param name = "currentpageindex"> current page number </Param> /// <Param name = "pagesize"> pagesize </param> /// <Param name = "where"> whereclient </param> /// <Param name = "orderby"> orderbyclip </param> // <returns> </returns> Public Virtual list <t> get_entity_bypage (INT currentpageindex, int pagesize, whereclip where, orderbyclip orderby, Params field [] fields) {return dB. from <t> (). where (where ). select (fields ). orderby (orderby ). getpage (pagesize ). tolist (currentpageindex );} /// <summary> /// return able by PAGE /// </Summary> /// <Param name = "currentpageindex"> </param> /// <Param name = "pagesize"> </param> // <Param name = "where"> </param> // <Param name = "orderby"> </param>/ // <Param name = "fields"> </param> // <returns> </returns> Public Virtual datatable get_entity_bypage_totable (INT currentpageindex, int pagesize, whereclip where, orderbyclip orderby, Params field [] fields) {return dB. from <t> (). where (where ). select (fields ). orderby (orderby ). getpage (pagesize ). totable (currentpageindex );} # endregion # region "batch processing" /// <summary> /// batch add with things /// </Summary> /// <Param name = "entitylist"> </param> // <returns> </returns> Public Virtual bool batchadd_entitys (list <t> entitylist) {// insert using (dbtrans trans = dB. begintrans () {try {dbbatch batch = trans. beginbatch (entitylist. count); entitylist. foreach (item => {item. detach (); batch. save (item) ;}); batch. process (); Trans. commit (); Return true;} catch {TRANS. rollback (); Return false ;}}} /// <summary> /// batch update /// </Summary> /// <Param name = "entitylist"> </param> /// <returns> </returns> Public Virtual bool batchupdate_entitys (list <t> entitylist) {// update using (dbtrans trans = dB. begintrans () {try {dbbatch batch = trans. beginbatch (entitylist. count); entitylist. foreach (item => {item. attach (); batch. save (item) ;}); batch. process (); Trans. commit (); Return true;} catch {TRANS. rollback (); Return false ;}}# endregion}
1. because mysoft can dynamically pass in dbsession objects to support database operations that are not supported, a constructor with dbsession is added to the basedao class. all methods are changed to virtual methods, so that the inheritance class can override its own implementation. in the business logic operation class of data, modify as follows:
Namespace ECOM. businesslogic. eshopmanagement {public partial class e_shopservice: basedao <e_shop> {# region "ctor" // The operation status of multiple databases is displayed. input the corresponding database operation object public e_shopservice (): base (dataacess. defaultsession) {}# endregion }}
The daacess class is as follows: Because my project has operations on multiple databases, the configuration is as follows:
Public static class dataacess {// <summary> // instantiate dbsession through the configuration section /// </Summary> Public static readonly defaultsession = new defaultsession (); /// <summary> /// API basic database /// </Summary> Public static readonly dbsession topsession = new dbsession ("topdatabasecon "); /// <summary> /// trace the base database /// </Summary> Public static readonly dbsession tracksession = new dbsession ("trackingcon");} public class defaultsession: dbsession {public defaultsession (): Base ("applicationservices ")
{This. registersqllogger (log =>{// output debugging information comlib. Debug. debugview. printdebug (log );});}}
Ii. Project Planning
Taking the eshop module as an example, the e_shop file is an entity class generated by mysoft, The e_shopservice is a generated general link processing business logic class (relational query), and The e_shoppartial is a special business processing class written by the user, these two classes are all partial classifications, so that you can isolate what you write from what you generate. Modifying the table structure or setting does not affect the existingCode. The naming rules here are as follows. All namespaces under the eshop module are
ECOM. businesslogic. eshopmanagement enables the processing logic to focus on the processing of modules.
Why do I need to separate commonlibraries from a folder? For my personal reasons, I wrote something. I hope to directly follow the source code to modify or add functions to the source code bug during debugging, if it is developed by a teammate, all the DLL libraries compiled by these class libraries will be put into dependencies. The dependencies folder aims to make the project-dependent DLL clear.
3. Use IOC and singletonprovider to manage our business logic
1. Why do I introduce IOC?
Logic A = new logic (); in this way, a new instance is required each time a service is called, or a global variable of the class is simply written in each called class, it is okay to make global calls. However, I think the class that does not need to save the status should be managed collectively, and you do not need to get an instance through new every time, my practice is as follows:
The source of IOC class is: commonlibrary. net
IOC Module
2. Why is singletonprovider introduced? For all classes limited to single-threaded operations, you do not need to write the same code in each class to get the single-column code.
Public class pageservicebase: Page {# region "get type instance via container" Public e_shopservice myeshopservice {get {return IOC. getObject <e_shopservice> ("e_shopservice") ;}} public e_ispservice myispservice {get {return IOC. getObject <e_ispservice> ("e_ispservice ");}} # endregion # region singerinstance if the class is in Single-Column mode, use the common singletonprovider to obtain the instance // public e_shopservice myinstanceshopservice {// get {// return singletonprovider <e_ispservice>. instance; //} # endregion}
Okay, now all the pages that inherit this page class need to be written to call the business logic class
This. myispservice. XXX (); OK, refreshing, convenient, centralized management ....
Do not forget to register a business class in the Global File.
Protected void application_start (Object sender, eventargs e) {IOC. init (New ioccontainerinmemory (); IOC. addobject ("e_shopservice", new e_shopservice (); IOC. addobject ("e_ispservice", new e_ispservice ());}