Application Framework Combat 20: Mapping layer Hyper-type

Source: Internet
Author: User

The previous article introduces the encapsulation evolution process of the Work cell layer super type, this article introduces the encapsulation of the Entity Framework Map layer Super type.

Using the Entity Framework generally requires mapping three types of objects, that is, entities, aggregates, value objects.

The main difference between aggregation and entity mapping is that the aggregation map single attribute identity ID and the need to map optimistic offline lock version, while the entity's identity often needs to be mapped into composite properties, so that entities in the aggregation are easily physically deleted. The Entity Framework uses entitytypeconfiguration to map entities.

Value objects are mapped in embedded value mode, which requires the use of complextypeconfiguration.

Encapsulating the mapping configuration is not required, but the encapsulation can be achieved with the following benefits.

1. Secondary memory .

If you're as bad as I am, you can't remember the top two class names, so you can remember by encapsulating a custom type. Once the package is complete, you can throw the system or third-party APIs aside.

2. Divide the logical structure .

Put all the mapping code into one method, not easy to read, I divided them into different methods, you can get a clearer structure.

3. Reduce code redundancy .

For aggregations, the ID identification and version optimistic offline locks can be encapsulated into a layer-super-type, reducing code redundancy.

mapping layer Hyper-type implementationEntity mapping base class Entitymapbase

Entitymapbase inherit from entitytypeconfiguration, the generic parameter tentity uses the IEntity interface constraint, which constructs a logical separation of the mapping configuration into 4 methods, that is, the mapping table, mapping identity, mapping properties, mapping navigation properties.

Calling virtual methods in a construction method can sometimes lead to unexpected errors, and this situation occurs when the code of the subclass constructor relies on some virtual methods, and because the order of the calls can cause failures, this is a rare situation, and if you run into the above problem, throw away the map base class decisively. Derive directly from Entitytypeconfiguration.

Entitymapbase is used to map entities, as shown in the code below.

usingSystem.Data.Entity.ModelConfiguration;usingUtil.domains;namespaceUtil.Datas.Ef {/// <summary>    ///Entity Mappings/// </summary>    /// <typeparam name= "TEntity" >entity Type</typeparam>     Public Abstract classEntitymapbase<tentity>: entitytypeconfiguration<tentity>whereTEntity:class, ientity {/// <summary>        ///Initializing Mappings/// </summary>        protectedentitymapbase () {maptable ();            MapId ();            Mapproperties ();        Mapassociations (); }        /// <summary>        ///Mapping Table/// </summary>        protected Abstract voidmaptable (); /// <summary>        ///Mapping Identities/// </summary>        protected Abstract voidMapId (); /// <summary>        ///Mapping Properties/// </summary>        protected Virtual voidmapproperties () {}/// <summary>        ///Map Navigation Properties/// </summary>        protected Virtual voidmapassociations () {} }}
Aggregation Map base class Aggregatemapbase

Aggregatemapbase inherits from Entitymapbase and overrides MapID and mapproperties to map identity IDs and optimistic locks.

In addition, there are two generic versions of aggregatemapbase that provide aggregatemapbase<tentity> to make the aggregation map easier to use because most of my aggregations use the GUID type, which saves a parameter.

Aggregatemapbase is used to map aggregations, as shown in the code below.

usingSystem;usingSystem.ComponentModel.DataAnnotations.Schema;usingUtil.domains;namespaceUtil.Datas.Ef {/// <summary>    ///Aggregate Root Mappings/// </summary>    /// <typeparam name= "TEntity" >Aggregate Root Type</typeparam>    /// <typeparam name= "TKey" >Entity Identity Type</typeparam>     Public Abstract classAggregatemapbase<tentity, tkey>: entitymapbase<tentity>whereTentity:aggregateroot<tkey> {        /// <summary>        ///Mapping Identities/// </summary>        protected Override voidMapId () {Haskey (t=t.id); }        /// <summary>        ///Mapping Properties/// </summary>        protected Override voidmapproperties () {Property (T= t.version). Hascolumnname ("Version" ). Isrowversion (). Hasdatabasegeneratedoption (databasegeneratedoption.computed).        Isoptional (); }    }    /// <summary>    ///Aggregate Root Mappings/// </summary>    /// <typeparam name= "TEntity" >Aggregate Root Type</typeparam>     Public Abstract classAggregatemapbase<tentity>: Aggregatemapbase<tentity, guid>whereTentity:aggregateroot<guid> {    }}
Value Object Map base class Valueobjectmapbase

Valueobjectmapbase inherits from Complextypeconfiguration, the only thing it needs is the mapping attribute, there is only one reason to create this class--to help you remember.

The valueobjectmapbase is used to map value objects, as follows.

usingSystem.Data.Entity.ModelConfiguration;namespaceUtil.Datas.Ef {/// <summary>    ///Value Object Mappings/// </summary>    /// <typeparam name= "Tvalueobject" >Value Object Type</typeparam>     Public Abstract classValueobjectmapbase<tvalueobject>: complextypeconfiguration<tvalueobject>whereTvalueobject:class {        /// <summary>        ///Initialize value object Mappings/// </summary>        protectedvalueobjectmapbase () {mapproperties (); }        /// <summary>        ///Mapping Properties/// </summary>        protected Abstract voidmapproperties (); }}

The reason that the mapping base class is not necessary is that the mapping configuration is typically created by the code generator, so the benefits that can be obtained from the base class are not very obvious. In addition, many people will feel that this leads to over encapsulation. The creation of these classes is largely a matter of my personal habits, and the purpose of introducing them is to tell you that if you do not want to use your memory, you can encapsulate a layer.

. NET Application Framework Exchange QQ Group: 386092459, welcome interested friends to join the discussion.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/xiadao521/

: Http://files.cnblogs.com/xiadao521/Util.2014.12.8.1.rar  

Application Framework Combat 20: Mapping layer Hyper-type

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.