. NetPractical application of business framework development-selection strategy for ing at the Business Layer
In the previous articleArticleMentioned in mapping, it feels like re-implementing nHibernate. In fact, the article is intended to reflectRichard made some choices when thinking: using existing ones, or finally using other methods. If you say anything as soon as possible, it is too arbitrary and one-sided. The series of articles repeatedly emphasize that technology has its applicable scenarios and there is no perfect technology. Many friends say that this series has developed an ORM in a similar way. In fact, it is not: Orm refers to converting database tables into data entities, but this article uses converted data entities. Is to give the data of the Data entity to the business class.
In addition, this article discussesMapping, that is, the way data is obtained. Of course, the design of business classes is far more than that.
We hope to reach a consensus on the following two points before the start:
1.BestDo not expose the data entities of the Dal (generated by LINQ or Entity Framework), or native datatable to the UI side (unless necessary, or for special reasons ).
2.UIThe bll class is used (or message-based scheme format ).
Today's topics are as follows:
1.The second mapping method.
2.The third mapping method.
Links to articles:
[Original]. Net distributed architecture development practices
[Original]. Net distributed architecture development practice Draft II Design
[Original]. Net distributed architecture development practice III. In-depth thoughts on data access
[Original]. Net distributed architecture development practices 4 build a bridge between ideal and implementation (previous article)
[Original]. Net distributed architecture development practice 5 framework Improvement
[Original]. Net business framework development practices six Dal Reconstruction
[Original]. Net business framework development practices-a preliminary idea of the seven business Layers
[Original]. Net business framework development practices-eight business layer ing selection policies
[Original]. Net business framework development practices 9 mapping attribute principles and verification rules implementation strategies
[Original]. Net business framework development practices in the first phase of the 10 Summary, a simple introduction, the water goes by the way (previous article)
[Original]. Net business framework development practices in the first phase of the 10 Summary, a simple and easy to understand (later)
1. The second mapping method.
RichardI thought about the configuration file method. It is true that the configuration file is flexible, but flexibility is also costly, because the framework was used by the company's developers at last, excessive configuration and high learning costs make the framework meaningless.
RichardI started to think about it and thought of another simplest mapping method: directly assigning values one by one, for example:
Code
Public Class Productbl
{
Public String Productname { Get ; Set ;}
Public Decimal Price { Get ; Set ;}
Public String Description { Get ; Set ;}
Public Void Mapping (m_product productentity)
{
This . Productname = Productentity. Name;
This . Price = Productentity. price;
This . Description = Productentity. description;
}
}
Obviously, this process is very simple but tedious.
Compared with the previous configuration file:
Advantages: 1. Easy to use and understand
2.Easy to debug
Disadvantages: 1. Tight coupling with data entities (in fact, this is not a disadvantage, which is considered a disadvantage in comparison with the previous configuration file method ). The aboveCodeIs used directly.M_product.(You can refer to the previous section.An articleAdvantages and disadvantages of using configuration files)
2.The compilation process is cumbersome. All are manual mapping.
Another key point is: how can a query object generate a final SQL statement?
For example, the following code:
Icriteria Condition = Criteriafactory. Create ( Typeof (Productbl). Where ( " Productname " , Operation. Equal, " Book " );
if you use the mapping method of the configuration file, it is clear that in the configuration file, productbl productname corresponding m_produc T entity Name field, that is, the corresponding database table m_product Name field (because in Bll, the name field is used through LINQ or Entity Framework generated m_product entity ). The above query object finally generates a file similar to select * From m_product where name = 'book ' statement.
RichardThinkNHibernateImplementation: InNHibernateThere are also query objects, inNHibernateThe implementation of the query object in is also dependentNHibernateTheMapping configuration file.
It does not mean that no query object is required.This can also be implemented by using the LINQ and entity frameworks. However, the data layer does not have the effect of "always changing", and developers need to master various data access technologies such as ADO. NET and LINQ. (See. NetDistributed architecture development practices 3 In-depth thoughts on data accessArticle).
NowRichard faces the following problems:
1.No configuration file requiredMapping, so that the query object is not easy to implement.
2.Manually typed in the Code mapping to repeat the work.
RichardThink about whether it is better to solve the above problems. So the third method is generated.
3.The third mapping method.
The third mapping method is to combine the advantages of the previous two mapping methods and avoid their shortcomings.
RichardThe solution to manual mapping is to generate graphical code instead of hand-written code. In addition, you need to save some information about the database fields.
Coincidentally, the field information in the Entities generated by LINQ and EF reflects the field information of the data table. This can be exploited. The sketch below is drawn Using Visio, which represents Richard's idea. In fact, Richard did not develop the following tools, and everything is still in the design stage.
RichardAn Automatic Code Generation Tool (tool developmentRichardAfter thinking about it, you can adopt the simplest implementation method: OneWindowsProgram. I also thought about usingDSLTool development,DSL).
Note: Although it is a code generation toolRichardIt is also easy to think: it is an operation to write text.
On the interface above, select the data entity class to be joined.Mapping, You can selectMappingname. Then click"Properties"Button, the following interface is displayed:
This is a configurationMappingPage: click"Add"Button to add a business class attribute, and then use"Mappingto"To set the data of this attribute from the field of the data entity class. When selecting a data entity field, you can also save the field information of the selected data entity for later query objects.
Basic IdeasRichardYes. The problem now is where to save the information of the selected data field, and it must correspond to the attributes of the business class, for example,IDCorresponding business classProductOfProductid,Instead of other attributes.
InMappingUsually, define an attribute in the business class, and then assign the value:
Public String Productid { Get ; Set ;}
This . Productname = Productentity. ID;
To save the data entity field information, the attribute Declaration of the business class is changed to the following:
Code
Public Static Readonly Propertyinfo < Int > Productidproperty = Registerproperty (
Typeof (Product ),
New Propertyinfo < Int > ( " Productid " , Typeof (M_product) " , " ID " ));
Public StringProductid
{Get{ReturnReadproperty (productidproperty );}
Set{Loadproperty (productidproperty, value );}
}
The code above is easy to generate, and the attribute Declaration above has more usage. The first thought is similar to the dependency attribute in WPF. The idea is also from the reference of WPF. This is short for "Mapping attributes ".
I wrote it here today. I can't help you because this article is quite cool and has not been written yet. The next article describes the implementation principles and causes of the mapping attribute, which is why the productidproperty declaration method is used.
The copyright is owned by Xiaoyang and the blog Park. You are welcome to reprint the copyright. repost the copyright to indicate the source to the author.
Http://www.cnblogs.com/yanyangtian