Practical unveiling: development .NET platform application system framework
Author: This article taken from SUN Ya-min: SEOUL
Microsoft's .NET platform provides a very good basic system platform for application development, how is it to build your own application on this system platform, and we need to build your own application framework for the characteristics of the application system (Framework ). In the process of applying the .NET development system, we combined with many years of development experience, also referring to J2EE architecture, designed a set of application system frameworks under .NET, and corresponding middleware and development tools, already in multiple projects. Application in neutralizing software products has achieved good results. Now introduce the overall solution for this framework, I hope to help you. We know that for a typical three-layer application system, we can usually divide the system into the following three levels: • Database layer · User interface layer · Application service layer For application system, in these three hierarchical, system main Features and business logic processing in the application service layer, for the system framework, the main process is also the architecture of this level. For the application service layer, in an object-oriented system, the following aspects of the following aspects must be processed: • Data representation, that is, the expression of the entity class, and the corresponding relationship with the database, that is, the so-called so-called OR MAP problem. · The data is accessible, that is, the persistence problem of the entity class, usually using the database to permanently store the data entity, which requires solving the interaction problem with the database. This part is to complete, that is to save the data entity into the database, or read data entities from the database. The same is related to this part is the use of data access objects. In the frame, we have made a layer of encapsulation in ADO.NET, making it easier, and also unifying the use of ADO.NET. · Organization of business logic. In an object-oriented system, business logic is implemented by messaging between objects. In this section, in order to ensure the correctness and reliability of logic processing, it is necessary to support transaction processing. · Provide way of business services. In order to ensure the flexibility and encapsulation of the system, the system must have a layer to encapsulate these business logic, serve the client, while as an interface between the functional calls between the system, to ensure high-polymerization and low coupling of the system. The customer refers to the user refers to the user who is called, but the interface, other programs, etc. The web layer (ASP.NET page) is usually only interacting with this part, not the functionality of the business logic layer or data entity. In order to solve these problems very well, we designed this framework. In the framework, for the above problems, we divide the application service layer into five levels: data entity layer, entity control layer, data access layer, business rules layer, and business appearance layer. The relationship between each level can be expressed as follows:
Hierarchical problem data entity layer data representation of physical control layer data access layer provides access to database access, encapsulation ADO.NET business rule layer business logic's Organizational business appearance layer business service
The structural diagrams of the entire system are as follows:
The arrows in the figure indicate that the relationship is divided into so many layers. It is the advantage that the system's architecture is more clear, so that each level is completed, the function is compared, the function is ruined, which means we Some tools can be developed to generate these code, thereby reducing the workload of code written, enabling developers to put more energy on the processing of business logic. It is based on this idea, we have developed development tools for this framework, and reduce the number of code in actual work, and the effect is very good. At the same time, in order to apply the service layer better, we have designed an application system middleware that supports this framework. (Now, many other companies have tried this middleware system.) Different from the EntityBean of J2EE, we have adopted data entities and entities to control separate design methods, which will bring a certain benefit. Below I will detail the design and strategy of each part as follows: Data entity layer We first need to solve the problem of data representation, which is the usual O-R Map problem. O-R MAP usual approach is to map classes in the program to one or more tables of the database, such as a simple Product class: public class product
{
String productId;
String productname;
Float Account;
}
A PRODUCT table may be corresponding in the database:
Field name Data Type Productidivarchar (40) ProductNameVarchar (50) Accountfloat
This is the most common practice, but this way will bring some problems. The first is that the data entity is different in the database and program. For some "coarse granular objects" involved in multiple tables, one entity class may reference multiple other entity classes, which means that the object is involved. There are some problems in the modeling of granularity; followed by interacting with the database, there is also a conversion problem, if an object involves the operation of multiple tables, the problem is greater; finally, when the system is inquiry, When multiple objects need to be returned, because the problem involves conversion, the efficiency is relatively low, and if it is used to return to the data set, although the efficiency, the problem is inconsistent with the inconsistency. Considering the above problems, we use another way in the performance of the data entity, which is to use the DataSet. DataSet is Microsoft's new data object in ADO.NET, which is different from ADO's Recordset, which can accommodate multiple records. DataSet is similar to a memory database consisting of multiple DataTable, while a DataTable has multiple column. Such a structure allows him to map with a database. At the same time, we have learned the advantages of CMP in the J2EE architecture to define the entity class structure in the J2EE architecture, using similar solutions. Therefore, in this respect, we are processed: 1) The core class library defines the EntityData class. This class inherits the DataSet, adds some methods to the framework of all entity classes, define each entity class. General structure, as for each entity class specific structure, determined by the following method: 2) The definition of the entity class is determined by the XML file, the XML file conforms to the JIXML object entity description language specification (Note: jixml is us Object-entity mapping language for determining the structure of the entity class. For example, a definition of the entity class of the order may be similar to the following structure: 3) The structure of the entity object is generated according to the XML of the above specification by a series of classes. These types of constructors implements the IClassBuilder interface. We predefine some standard Builder in the system core class, in general, directly using these standards of Builder. The design model of the class constructor used by the class constructor. If the user thinks that the standard Builder does not meet the requirements, you can extend the IClassBuilder interface, write your own class constructor, and then specify a variety of constructors in the system configuration file. Name. The definition of IclassBuilder is as follows: Public Interface IClassBuilder
{
EntityData BuildClass (String StrClassName); // Get class data structure
SQLStruct getsqlstruct (string strsqlname);
}
This part of the structure can be represented by class diagrams:
When the user needs an entity class, as long as the following statement is used:
EntityData Entity = EntityDataManager.GeteMptyentity ("Product");
The GetExtYentity method of EntityDataManager is implemented by calling the ClassBuilder's buildClass and implements the cache function of the object. The ClassBuilder's buildclass method is implemented as follows:
Public EntityData BuildClass (String strclassname) {
Iclassbuilder
Builder = ClassBuilderFactory.getClassBuilder (STRCLASSNAME);
Return Builder.BuildClass (STRCLASSNAME);
}
Here is a comprehensive use of Builder and Factory design patterns. The role of ClassBuilderFactory is based on the name of the entity class, the specific class name of the corresponding class fabric in the configuration file, and returns the specific class constructor. The structure of the configuration file ClassBuilders.xml is simple:
XML Version = "1.0" encoding = "GB2312"?>
BuilderName = "jobsinfo.singletableclassbuilder" /> Builders> If the specific Builder is not specified for a physical class, the system will call the default Builder to construct the structure of the entity object. The system also provides entity object cache services. The entity objects generated by the above-described manner can be cached so that when the object is called, it can be read from the cache without re-generating, thereby greatly improving the performance of the system. In the actual development process, we feel that the data entity layer uses this design mode with the following advantages: • The physical class definition XML file can be automatically generated by tools, mitigating development effort. • When performing query operations, both an entity is returned, or the manifestation of multiple entities, the manifestation of data is both ENTITYDATA, and there is no problem that the single object and data set as described above is not uniform. • When modifying the definition of the entity, if the modified part does not involve the processing of business logic, you can only modify the XML file, you do not have to modify other programs and recompile. · The physical object cache service provided by the system can greatly improve the performance of the system. · The design model of the class construction plant has greatly improved the flexibility of the system. Entity control layer solves and O-R MAP problems, it is necessary to consider the persistence problem of entity classes, that is, the interaction with the database. The physical control layer is used to control the basic operation of data, such as increasing, modifying, deleting, query, etc., while providing data services for the business rules layer. The class of the entity control layer implements the IentityDao interface. This interface defines the main necessary methods of implementing data manipulation, including adding, modifying, deleting, and looking up. The definition of IAentityDao is as follows: Public Interface IntiTyDao: IDisposable { Void insertity (entityData entity); Void UpdateEntity (EntityData Entity); Void deletentity; EntityData FindByPrimaryKey (Object strkeyvalue); } It can be seen that this interface is very similar to the interface of EntityBean in J2EE. In fact, we also refer to EntityBean solutions. Below is an example of a Product's DAO class: Public Class ProductivityDao: IENTITYDAO { Private dbcommon db; // This is the class access by the database Public product DiDao () { DB = New dbcommon (); db.open (); } Public productTiesDao (dbcommon cdb) { THIS.DB = CDB; } // Insert an entity Public void insertentity (entityData Entity) { Checkdata (Entity); db.begintrans (); Try { Foreach (DataRow Row in Entity.Tables ["Product"]. ROWS) Db.exesql (Row, SqlManager.getsqlstruct ("Product", "InsertProduct")) db.committrans (); } Catch (Exception E) { db.rollbacktrans (); Throw e; } } / / Modify a physical class Public void UpdateEntity (entityData Entity) { Checkdata (Entity); db.begintrans (); Try { Foreach (DataRow Row in Entity.Tables ["Product"]. ROWS) IF (Row.RowState! = DATAROWSTATE.UNCHANGED) Db.exesql (Row, SqlManager.getsqlstruct ("Product", "UpdateProduct"))) db.committrans (); } Catch (Exception E) { db.rollbacktrans (); Throw e; } } // Delete a entity class Public void deletentity (entityData entity) { Checkdata (Entity); db.begintrans (); Try { Foreach (DataRow Row in Entity.Tables ["Product"]. ROWS) Db.exesql (Row, Sqlmanager.getsqlstruct ("Product", "DeleteProduct"))); db.committrans (); } Catch (Exception E) { db.rollbacktrans (); Throw e; } } / / Find the entity class Public EntityData FindByPrimaryKey (Object KeyValue) { EntityData Entity = New EntityData ("Product"); Sqlstruct SqlProduct = SQLManager.getsqlstruct ("Product", "SELECTBYIDPRODUCT"); DB.Filletity (SqlProduct.sqlstring, Sqlproduct.Paramslist [0], KeyValue, Entity, "Product"); Return Entity; } Public EntityData FindAllProduct () { EntityData Entity = New EntityData ("Product"); Sqlstruct SqlProduct = Sqlmanager.getsqlstruct ("Product", "FindAllProduct"); DB.Fillentity (SqlProduct.Sqlstring, Null, Null, Entity, "Product"); Return Entity; } / / Validity Void Checkdata (EntityData Entity) { IF (Entity.Tables ["Product"]. Rows [0] ["ProductID"]. TOSTRING (). Length> 40) Throw New ErrorClassPropertyException ("Property Productid Should Be Less Than 40 Characters"; } Public void dispose () { Dispose (TRUE); Gc.suppressFinalize (TRUE); } Protected Virtual Void Dispose (BOOL Disposing) { IF (! Disposing) Return; // We're Being Collected, So let thegc take care of this object db.close (); } } Combined with the data entity layer, these two parts realize the application service layer interaction with the database. These two parts are combined to complete the functionality similar to the EntityBean in J2EE. Using data entities and entities control separate design methods, with the following advantages: Avoid defects that manipulate the EntityBean system resource consumption and low efficiency in the J2EE system. · Resolved the overhead of using EntityBean transmission data in the J2EE system, and low-efficiency defects. · You can modify the entity structure and manipulation of the entity data, so that the system is more flexible and the data entity's XML definition file and entity control layer can be automatically generated, and the development workload can be reduced. The data access layer provides services to the database operations for the entity control layer, we design this part. This level usually does the following: • Connect the database · Execute database operation · Query database, return results · Maintain Database Connection Cache · Database Transaction Call To unify the data of data, we are designing, in the framework library The data access service contains the commonly used database operations, which can access different types of databases, so that when the specific software system is developed, it is possible to replace the application with the database. During the database, you don't have to modify the original code, greatly simplify the development and deployment work. Data Access Service also maintains database connection cache, improving system performance, and services to database transactions. Data Access Service is primarily a service called to data access functionality in the core class library. The use of dbcommon can see one or two in ProductItemDao above. More can look at the use of DEMO projects. The functionality of the business rules level business rules is the implementation of various business rules and logic. Business rules complete this task such as the verification of customer accounts and book orders. This is the most complicated part of the entire application system, without too many laws. However, after we finish the above work, we can have a simplified job for this part of the development. This can be seen from the example below. The design of the business rules usually needs a good modeling. Modeling of business rules, generally using UML. You can use UML sequence diagrams, state diagrams, activity diagrams, etc. to model business rules. This part of the work is usually done through interactions between a series of classes. Business rules usually require the system to support transaction. In this place, .net provides a means of calling Windows Transaction Server. About this part, you read MSDN very clearly, and you will not do a detailed introduction. For example, in a stock system in a stock system, in addition to the need to save the storage list, before this, the number of products involved in the library must be modified, and the code is usually as follows (using transaction processing) : Public Void StoreIntoWarehouse (EntityData Indepotform) { DataTable tbl = indepotform.tables ["indepotformdetail"]; Try { ProductivityDao PED = New ProductTentIn (); For (int i = 0; i { DataRow formdetail = tbl.rows [i]; String productId = formdetail ["productID"]. TOSTRING (); Decimal Incount = (decimal) FormDetail ["incount"]; EntityData product = ped.findbyprimarykey (ProductID); DATAROW ProductRow = Product.getRecord ("Product"); ProductRow ["currentcount"] = (decimal) Productrow ["currentcount"] incount; Ped.UpdateEntity (Product); } Ped.dispose (); IndepotFormentityDao Indepotform = New IndepotFormentityDao (); Indepotform.insertity (IndEpotform); Indepotform.dispose (); Contextutil.setComplete (); } Catch (Exception EE) { Contextutil.Setabort (); Throw EE; } } The business appearance layer business appearance layer provides the web layer to provide processing, browsing, and operation interfaces. The business appearance layer is used as an isolation layer that isolates the user interface to the implementation of various business functions. The business appearance layer is just a system function that has been completed, and the business rules are high-level package according to the needs of each module. The framework is not specified in the implementation of the business appearance layer, but it is recommended to use Web Service to provide services. Using IIS as a web server, it can be easily deployed Web Service. • The Web layer web layer provides access to the app for the client. The Web layer consists of an ASP.NET web form and code hidden file. The web form is just a user operation with HTML, and the code hidden file implements event processing of various controls. Typically, for the ASP.NET web form and control event processing code for the data maintenance type, we provide tools to generate and reduce development efforts. In addition to the above six logic layers, the system typically includes a system configuration item, providing an application configuration and tracking class. The design strategy of framework services In order to support the system architecture described above, we need a core class library to achieve support for applications that construct it. Thus, when the development of each application system, many basic work can be omitted to improve the efficiency of development. In this regard, we designed the following core classes and interfaces: • EntityData: Universal Structure of the entity class · iClassBuilder: Defines the structure of the physical class structure. We predefined several standard classes implemented according to this interface: AbstractClassBuilder, SingletableClassBuilder, ThickClassBuilder, StandardClassBuilder. These Builders are managed by ClassBuilderFactory. · IENTITYDAO: Defines the interface of the entity control class. Through these core classes and interfaces, the framework provides the following services for the application: · Or map: Object-Relational Database Mapping Services This section completes the mapping of entity objects in the application, mainly providing data for data entity . In this section, a Jixml entity-object mapping language is defined. This is a specific language we developed using XML to describe mappings between object-entities. Developers can use it to describe object-entity mapping relationships. Developers can also directly extend the codebuilder interface, manually complete the code of the object - entity mapping relationship. The system is modified according to the setting of the configuration file at runtime, and the structure of the entity object is dynamically constructed. · Database Access: Database Access Service This section provides services to database access. The application software system built on this framework is not directly manipulated, but is performed by the data access service provided by the class library. Database Access Service As an intermediaries between the application with the database, it is possible to effectively prevent the insecure operation of the database. Database Access Services also provides a calling method for database library transaction processing, developers can easily call the transaction function of the database through the Database Access Service. · DML Search: Data Manipulation Schedule Query Service In the system architecture, the SQL statement that operates on the database is not embodied in the program, but is described in the XML file as the physical class structure of the data entity layer, which conforms to the JIXML specification. The basic part of these manipulation schemments, such as data insertion, deletion, modification, query, etc., can be generated by our own development. In this way, it can be greatly improved in the facility and flexibility of the system. In this way, the system must provide query services for these data manipulation. The core class library provides services to find these data manipulation schematic and related parameters in an XML file. · Entity Buffer & Search: Entity Objects in the Entity Object Cache & Find Service System After the first creation, the system is cached, when the system needs to access the object, do not need to create this object again, but only needs Take it from the cache. This is the entity object cache service provided by the framework. Interacting with this service is the search service of entity objects, which is the service of the corresponding entity object from these cached entity objects. · Transaction: Transaction Service We make full use of the powerful features of the Windows COM transaction mechanism, enabling the application to fully use the transaction processing, and ensure the stability and reliability of the application system. When a class requires a transaction function, first inherit the class inheritance of the ServicComponent class under the System.EnterpriseServices namespace, and then use the transaction type used as follows: [Transaction (TransactionOption.Required)]. When the system is called for the first time, the system is automatically registered in the COM service, so that the application can use the COM transaction function. The system supports the following transaction type: Member Name Description Disabled ignores any transactions in the current context. NotSupported creates components in context using non-controlled transactions. Required If the transaction is shared, create a new transaction if necessary. RequiresNew uses a new transaction to create components and is independent of the status of the current context. Supported If the transaction exists, shared this transaction. At the same time, in order to simplify the development, we also designed a development tool for this framework and integrated into the development environment of the plugin to the VS.NET, which can greatly reduce the development of the code to write workload. In this way, through the above, we have achieved the following objectives: • With a very clear system architecture · Because there is a core class library to serve the application system, we can reduce a lot during the rear development process Basic work · With its own targeted development tool, it can reduce a lot of repeating coding work. For the reader to better understand the structure of the system, attach a DEMO project (download Demo project). This is an example of a simple warehouse inventory that basically shows the structure of this framework and the use of the core class library. I have to see the actual content: Use the .NET framework development application system introduction: Sun Yam, graduated from Nanjing University in 1998, is now the technical director of Suzhou Di Xun Software Development Co., Ltd., familiar with J2EE architecture, .net and C # language.