Architecture mode: Data er)

Source: Internet
Author: User
Tags in domain to domain

Architecture mode: Data er)
: The data relational database is used to store data and relationships, and objects can process business logic. Therefore, to combine data and business logic into one object, we must either use activity records, you can also separate the two and associate them with each other through a data er. The data er is an intermediate software layer that separates memory objects from databases. The following sequence diagram describes the concept of the intermediate software layer: image in this sequence diagram, we also see a concept, the er must be able to obtain the domain object (in this example, a Person is a domain object ). However, for changes in data (or changes in domain objects), The er must also be aware of these changes. At this time, we need the work unit model (discussed later ). From this, we can see that the data CER is quite simple. The complicated part is that we need to process table queries and domain object inheritance. Fields of domain objects may come from multiple tables in the database. In this case, we must have the data er do more. Yes, as we have mentioned above, the data er must be able to implement two complex parts: 1: perceiving changes; 2: assigning values to domain objects through the results of table join queries; to detect changes and maintain consistency with database objects, You need to map IDS (schema objects and relational schema patterns: Identity fields )), this usually requires a registry with ID ing or an ID ing for each search method. The following code is the latter: void Main () {SqlHelper. connectionString = "Data Source = xxx; Initial Catalog = xxx; Integrated Security = False; User ID = sa; Password = xxx; Connect Timeout = 15; Encrypt = False; trustServerCertificate = False "; var user1 = User. findUser ("6f7ff44435f34 12cada61898bcf0df6c "); var user2 = User. findUser ("6f7ff44435f3412cada61898bcf0df6c"); (user1 = user2 ). dump (); "END ". dump ();} public abstract class BaseMode {public string Id {get; set;} public string Name {get; set ;}} public class User: baseMode {static UserMap map = new UserMap (); public static User FindUser (string id) {var user = map. find (id); return user;} public class UserMap: Abstr ActMapper <User> {public User Find (string id) {return (User) AbstractFind (id);} protected override User AbstractFind (string id) {var user = base. abstractFind (id); if (user = null) {"is Null ". dump (); string SQL = "SELECT * FROM [EL_Organization]. [User] where id = @ Id "; var pms = new SqlParameter [] {new SqlParameter (" @ Id ", id)}; var ds = SqlHelper. executeDataset (CommandType. text, SQL, pms); use R = DataTableHelper. toList <User> (ds. tables [0]). firstOrDefault (); if (user = null) {return null;} user = Load (user); return user ;} public List <User> FindList (string name) {// SELECT * from user where name like name List <User> users = null; return LoadAll (users );} public void Update (User user) {// update user set ....}} public abstract class AbstractMapper <T> where T: BaseMod E {// The problem here is that as the object disappears, loadedMap will be recycled protected Dictionary <string, T> loadedMap = new Dictionary <string, T> (); protected T Load (T t) {if (loadedMap. containsKey (t. id) {return loadedMap [t. id];} else {loadedMap. add (t. id, t); return t ;}} protected List <T> LoadAll (List <T> ts) {for (int I = 0; I <ts. count; I ++) {ts [I] = Load (ts [I]);} return ts;} protected virtual T AbstractFind (string id) {if (loa DedMap. ContainsKey (id) {return loadedMap [id];} else {return null ;}} is a simple er with the id ing function. Because of the Identity ing, the result of running this code is: image regression is the essence of this question. The question is: what is "Data ing", in fact, this question is critical, through the Find method, UserMap converts database records into a User object, which is called "Data ing". However, the core role of UserMap is user = ableablehelper. toList <User> (ds. tables [0]). firstOrDefault (); this line of code. Furthermore, the ableablehelper. ToList <T> method completes the data ing function. The DataTableHelper. ToList <T> method actually retrieves the field value of the DataTable Based on the attribute name. This is a simple method, or in many scenarios where the business is not complex, this may be a good method. However, because the business is often complex, in actual situations, there are not many cases in which we use this method. In most cases, we need to encode it like this to complete the ing: someone. name = Convert. toString (row ["Name"]) do not doubt. The above line of code is called data ing. Any concept on the top is actually the code you have written many times. 1.1 data ing in EntityFramework this is a typical EF data ing class. public class CourseMap: EntityTypeConfiguration <Course> {public CourseMap () {// Primary Key this. hasKey (t => t. courseID); // Properties this. property (t => t. courseID ). hasDatabaseGeneratedOption (DatabaseGeneratedOption. none); this. property (t => t. title ). isRequired (). hasMaxLength (100); // Table & Column Mappings this. toTable ("Course"); this. propert Y (t => t. courseID ). hasColumnName ("CourseID"); this. property (t => t. title ). hasColumnName ("Title"); this. property (t => t. credits ). hasColumnName ("Credits"); this. property (t => t. specified mentid ). hasColumnName ("DepartmentID"); // Relationships this. hasMany (t => t. people ). withiterator (t => t. courses ). map (m => {m. toTable ("CourseInstructor"); m. mapLeftKey ("CourseID"); m. mapRightKey ("PersonID") ;}); this. hasR Equired (t => t. department ). withiterator (t => t. courses ). hasForeignKey (d => d. departmentID) ;}} we can see that EF data ing is a real data ing. Basically, it does nothing inside: Which field is the database and which attribute is the corresponding memory object. Eventually, it generates a domain model through an object factory. The principle is roughly as follows: internal static Course BuildCourse (IDataReader reader) {Course course = new Course (reader [FieldNames. courseId]); contract. title = reader [FieldNames. title]. toString ();... Return contract;} 2: Is it too heavy for the concept of a Data CER in the repository UserMap? Because it does ing and persistence, it even has to hold a unit of work. So, if we can, like EF, do only map things and separate the other things? Yes. This part is called a warehouse. 3. Let's talk about DataTableHelper. ToList <T>. The simplified data er is actually a DataTable To List. If you are using a framework such as EF or NHibernate, you should use the mappers they provide (strictly speaking, you are not using their mappers. Because these frameworks are using their own mappers, we are only configuring the data and relationships required by the mappers. Sometimes, these configurations are in the configuration file, sometimes adding Attribute to a field or Attribute, or sometimes a simple but huge single-line code ). Of course, we can also create our own standard Mapper, which Tim McCarthy has implemented in "Domain-driven design C #2008 implementation. However, EF and nhib.pdf are good, but many times we still have to use handwritten SQL, because: 1: EF and nhib.pdf require learning costs, which indicates that the training costs of the team are high, and error-prone; 2: the efficiency of handwritten SQL should not be abandoned.

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.