9. Net business framework development practices 9 mapping attribute principles and implementation policies of verification rules
Previous discussions have been focusing onData acquisition and data mapping in Dal. In fact, the main role of a business framework is to simplify the compilation and development of business logic.
The topics in this article are as follows:
- References
- Comprehensive Consideration
SeriesArticleLink:
[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.References
The emergence of a framework is not that simple and requires a lot of problems.Richard considers:
- Avoid re-building wheels
- Draw on the existing mature framework ideas
In the development process,Richard has been using Visual Studio IDE for development. And every time with the release of the new version of Vs, it is always accompanied by the emergence of new technologies. In many cases, developers only focus on the use and learning of new technologies. However, there is another aspect that deserves attention for the new technology: the principle of implementation, and the reason for such implementation, that is, the idea. There is no doubt that the new technology is the result of thinking by some masters. It is of great benefit to learn from their ideas.
InDuring his learning, Richard was particularly concerned about the concept of Dependency Property, first WPF, and then when he learned WF, you can also see the re-use of the dependency attribute. He considered using the idea of dependency attributes to the business framework he was developing.
First, he analyzed the implementation methods of current dependency attributes.(Using WPF as an example ),
Code
Public Class Frameworkelement: uielement ,...
{
Public Static ReadonlyDependencyproperty marginproperty;
...
}
PublicThickness margin
{
Set{Setvalue (marginproperty, value );}
Get{Return(Thickness) getvalue (marginproperty );}
}
StaticFrameworkelement ()
{
Frameworkpropertymetadata metadata= NewFrameworkpropertymetadata (
NewThickness (), frameworkpropertymetadataoptions. affectsmeasure );
Marginproperty=Dependencyproperty. Register ("Margin",
Typeof(Thickness ),Typeof(Frameworkelement), metadata,
NewValidatevaluecallback (frameworkelement. ismarginvalid ));...
}
InRichard has always believed that the declaration of an attribute is very simple, and Richard has been using the following method for a long time:
Public Thickness margin
{
Get;Set;
}
In comparison, the biggest benefit of dependency properties is:More information is provided for common attributes, and more functions are provided: verification, triggering callback events. Of course, using common attributes can also achieve:
Public Thickness margin
{
Get{ReturnMargin ;}
Set
{
If(Margin<0)
...
...
}
}
In comparison, dependency attributes are more elegant and scalable.
Another important thing is that once an attribute is changed to a dependency attribute. NET Framework begins to manage this property, such as automatic verification, value change and tracking. In this way, the task is handed over to the framework, which makes it easier for developers to do things.
Richard remembered the problems encountered in the development business class: for example, the followingCode:
Public Class Product
{
Public StringProductname {Get;Set;}
Public DoublePrice {Get;Set;}
}
In many casesBecause of the logic, it is often necessary to judge that productname is not empty and the price must be greater than zero. Therefore, you need to write code to judge each time:
Public VoidAdd ()
{
If(String. Isnullorempty (This. Productname ){...}
If(This. Price<0){...}
}
There are more than these problems. If the same is required in other business classes and similar verification is required, only one line of similar code can be written. The best case isCopy some code.
Writing code in this way is really tiring. Richard also wants to improve it in some ways later, and uses the validation verification module in the Enterprise Library, so the code becomes like the following:
Public Class Product
{
[Notnullvalidator]
Public StringProductname {Get;Set;}
Public DoublePrice {Get;Set;}
}
Using declarative development,The idea of AOP, in fact, is indeed quite good than before. When saving business data, you only need to call the validate () method of the validation module. It is really convenient, but the problem is that every time you call the validate () method, all attributes of this service class will be checked (those attributes with verification tags added ), in this way, the performance is not good, and it cannot be verified separately for a certain attribute.
2.Comprehensive Consideration
Richard also considered another point: he had been solving the mapping problem before. In the end, he assigned the data obtained from the Dal to the attributes of the business class. In addition, you must create a query object based on the business class and resolve the query object to an SQL statement. Therefore, you must also save the correspondence between the business attribute and the data entity attribute in the Dal, that is, which business attribute corresponds to which data entity attribute (also a table field ).
Based on the above considerations,Richard decided to take advantage of the Dependency Property (automatic verification, data change tracking, and permission verification), and gave the Dependency Property more metadata information: save the mapping field information in the dependency attribute. Therefore, the attribute declaration is as follows:
Code
Public Static Readonly Propertyinfo < Int > Productidproperty = Registerproperty < Product > (
New Propertyinfo < Int > ( " Productid " , Typeof (M_product) " , " ID " ));
Public String Productid
{
Get{ReturnReadproperty (productidproperty );}
Set{Loadproperty (productidproperty, value );}
}
In the preceding attribute declaration, specify(For example, product.
Registerproperty stores the attribute information in a dictionary:
Dictionary <type, list <ipropertyinfo>
WherePropertyinfo followedAcceptedIpropertyinfo interface.
The final result is:The mapping attributes are stored in a Global static dictionary.
In addition, a Global static dictionary is used to save the verification rules corresponding to each attribute:
Dictionary <ipropertyinfo, list <icheckrule>
All verification rules are fromThe icheckrule interface inherits.
A relatively powerful property is generated. Of course, inThe verification in the mapping attribute is only basic verification, and more complex business verification will be placed elsewhere. The implementation method or similar to WPF: Adopt callback, such as newValidatevaluecallback(Frameworkelement. ismarginvalid ).
ThereforeThe mapping attribute solves three problems:
- Implementation of mapping and query objects
- Partial verification rule Declaration
- Manage business attributes
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
Next article:The first version of the. NET service framework is released.