[EntLib] Microsoft Enterprise Library 6 based on data Access Application Block repository

Source: Internet
Author: User

The name is a little exaggerated, it's actually implementing the DAL base class and constraints for the data Access application Block

First of all, there is nothing to describe the repository, if you do not understand the direct Baidu or Google-related content, directly on the specific code

Note that this section does not have a way to write bulk queries (such as FindAll, which is related to the base class setting later)

    <summary>//DataAccess Repository//</summary>/<typeparam name= "T1" ></typepa  ram>//<typeparam name= "T2" ></typeparam> public interface Irepository<t1, t2> {//        <summary>/////Based on primary key to obtain the corresponding entity object///</summary>//<param name= "key" ></param>        <returns></returns> T1 Getentitybykey (T2 key);        <summary>///single new///</summary>/<param name= "entity" ></param>        <returns></returns> BOOL Insert (T1 entity);        <summary>///individual editor///</summary>/<param name= "entity" ></param>        <returns></returns> BOOL Update (T1 entity); <summary>///single deletion///</summary>/<param name= "key" ></param>/ <returns></returns>        BOOL Delete (T2 key); }
Then there is the Dal abstract base class, which implements read-write separation on the design of the base class, and each subclass should have only one database connection, so that each DAL class should only perform its own basic functions:care only about database interactions, not on business logic
    <summary>//DAL base class, based on EntLib, read/write separation///</summary> public abstract class Dataaccessbase {        Private Database _readdb;        Private Database _writedb; <summary>///The Read Database configuration section to be used, if configured as NULL or NULL invokes the default configuration section///</summary> protected abstract str        ing readdbname {get;} <summary>////The Write Database configuration section to be used, if configured to NULL or NULL calls the default configuration section///</summary> protected abstract str        ing writedbname {get;}            <summary>///reading library///</summary> protected database Readdb {get {if (THIS._READDB = = null) {THIS._READDB = this. Getdatabase (This._writedb, this.                Readdbname);            } return THIS._READDB; }} Private database Getdatabase (Database db, String dbName) {if (this. Readdbname = = this.    Writedbname && db! = NULL)        {return db; } else {return this.            CreateDatabase (DbName);             }}///<summary>//write library///</summary> protected database Writedb {  get {if (this._writedb = = null) {This._writedb = This. Getdatabase (THIS._READDB, this.                Writedbname);            } return this._writedb;  }} Private Database CreateDatabase (string dbName) {Databaseproviderfactory factory = new            Databaseproviderfactory (); if (string. Isnullorwhitespace (DbName)) {return factory.            Createdefault (); } else {return factory.            Create (DbName); }        }    }
Finally is the IRepository interface and the combination of dataaccessbase implementation singledataaccessbase, why not dataaccessbase when the implementation of IRepository it? Because the design of dataaccessbase is can be used in multi-table operation and single-table operation, multi-table operation, IRepository does not have any meaning, only a single table operation, IRepository has meaning, And Singledataaccessbase is the single-table Dal base class.

    <summary>//////single-table DataAccess base class, all single table dataaccess should inherit this class, it is recommended that the non-common part also implement interface//multi-table but single-database operation DataAccess cannot inherit this class, but should inherit dat Aaccessbase//</summary>//<typeparam name= "T1" ></typeparam>//<typeparam name= "T2" ;</typeparam> public abstract class Singledataaccessbase<t1, t2>: Dataaccessbase, Irepository<t1, T2&gt    ; {#region irepository<t1,t2> members///<summary>//////&LT;/SUMMARY&G        T <param name= "key" ></param>///<returns></returns> public abstract T1 Getentityb        YKey (T2 key);        <summary>///single new///</summary>/<param name= "entity" ></param>        <returns></returns> public abstract bool Insert (T1 entity);        <summary>///individual editor///</summary>/<param name= "entity" ></param> <returns&Gt;</returns> public abstract bool Update (T1 entity); <summary>///single deletion///</summary>/<param name= "key" ></param>/        <returns></returns> public abstract bool Delete (T2 key); #endregion}
Here Singledataaccessbase actually did not specifically implement the irepository (I lied to you), just a constraint specification, because the data Access application block is actually not an ORM, if you use this class library, In fact, there is a large probability to determine the performance of the system is high, of course, you can also through the reflection of the implementation of real repository (if your Poco and relational database field can correspond)

Many people may feel that it is not necessary or meaningless to differentiate between the Dal and the single-table operation of a multi-table operation, indeed, there is no difference between writing in a function and writing in more than one, and if the system is larger and more developers It is likely that some developers want to use some of the fields of a table, but because the system is large and there are no related constraints, so that the developer does not know where to find out if the method already exists, for the sake of simplicity, the most common practice is to write this part of the code directly where you need it, and the end result is, The distribution of code is becoming more and more chaotic, and this is especially a disaster when the table structure changes, because if you do not look up the table name, you simply cannot foresee where the table is used! The distinction between single-table and multi-table operation, is to solve such a problem, when the table structure changes, it is only necessary to find the table corresponding to the single-table dal and the database corresponding to the multi-table Dal two places

[EntLib] Microsoft Enterprise Library 6 based on data Access Application Block repository

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.