ExtJS4.1 + MVC3 + Spring. NET1.3 + EF5 integration 5: data access layer

Source: Internet
Author: User
This article describes how to write the data access interface and implementation-layer code. The data access layer is generally atomic, and there are few data tables involved due to small projects. I will first implement the data access interface and interface in the previous figure, which is just a few classes mentioned in this Article. Other classes will be implemented later. 1. Data Access... SyntaxHighlighter. all (); this article describes how to write the data access interface and implementation-layer code. The data access layer is generally atomic, and there are few data tables involved due to small projects. I will first implement the data access interface and interface in the previous figure, which is just a few classes mentioned in this Article. Other classes will be implemented later. 1. Data access interface I abstracted the parent interface based on the actual situation. For other IDao interfaces, I can directly inherit the interface. The main consideration is that the project is relatively simple, so that the design can simplify the code. But there is a problem. Although Dao depends on the persistence layer framework (EF or nhib.pdf), it should not be expressed by the IDao interface, otherwise, the Service layer will also rely on the persistence layer framework. Obviously, this design is not a good design (of course, if you have to do so, it is not impossible ). Therefore, IDao should not be coupled with the EF framework. This is also the reason why I set all the interface parameters in the IDao layer to be of the string, int, and other types, but it is not designed as an Expression. > Type. 1.1 The Query method of the IDao interface is mainly the implementation of data paging: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; namespace MESE. IDao {public interface IDao Where T: class {IList Query (string SQL, int pageIndex, int pageSize, out int recordCount); IList QueryAll (); bool Exists (string code); T Read (string code); bool Add (T entity); bool Update (T entity); bool Delete (T entity ); bool Delete (string code); int DeleteByKeys (IList Keys) ;}} in this way, other access layer interfaces can be inherited directly, and the code is directly used: 1.2 IAdminDao interface [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; namespace MESE. IDao {public interface IAdminDao: IDao {} 1.3 IRoleDao interface: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; namespace MESE. IDao {public interface IRoleDao: IDao {} 1.4 The ICategoryDao interface expands a method based on actual needs: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; namespace MESE. IDao {public interface ICategoryDao: IDao {IList QueryByParent (string parent) ;}} 2 the Data Access Implementation class is defined by the above interface. Now we start to implement these interfaces. Similar to the IDao design, we also write a DaoBase class, to encapsulate common atomic operations. Note that, because the EF5 persistence layer framework is used and combined with the DbContext management class compiled in the previous article, the DbContext obtained and created in Dao must be obtained through the DbContextFactory class. 2.1 DaoBase class [csharp] using System; using System. collections. generic; using System. linq; using System. linq. expressions; using System. text; using System. data. entity; using Simple. web. entityFramework5; namespace MESE. dao {public class DaoBase Where T: class {public DbContext {get {return DbContextFactory. GetContext () ;}} public virtual IList Query (string SQL, int pageIndex, int pageSize, out int recordCount) {var list = this. DbContext. Set (). SqlQuery (SQL); recordCount = list. Count (); return list. Skip (pageIndex * pageSize). Take (pageSize). ToList ();} public virtual IList QueryAll () {return this. DbContext. Set (). AsNoTracking (). ToList ();} public virtual IList QueryItems (Expression > Predicate) {return this. DbContext. Set (). AsNoTracking (). Where (predicate). ToList ();} protected virtual bool Exists (Expression > Predicate) {return this. DbContext. Set (). AsNoTracking (). Where (predicate). Any ();} protected virtual T Read (Expression > Predicate) {return this. DbContext. Set (). AsNoTracking (). Single (predicate);} public virtual bool Add (T entity) {this. DbContext. Set (). Add (entity); return this. DbContext. SaveChanges ()> 0;} public virtual bool Update (T entity) {this. DbContext. Set (). Attach (entity); this. DbContext. Entry (Entity). State = System. Data. EntityState. Modified; return this. DbContext. SaveChanges ()> 0;} public virtual bool Delete (T entity) {this. DbContext. Set (). Remove (entity); return this. DbContext. SaveChanges ()> 0;} protected virtual bool Delete (Expression > Predicate) {T entity = this. DbContext. Set (). Single (predicate); if (entity = null) return false; this. DbContext. Set (). Remove (entity); return this. DbContext. SaveChanges ()> 0;} public virtual int DeleteByKeys (IList Keys) {foreach (var key in keys) {var entity = this. DbContext. Set (). Find (key); if (entity! = Null) {this. DbContext. Set (). Remove (entity) ;}} return this. dbContext. saveChanges () ;}}2.2 AdminDao implementation class [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; using MESE. IDao; namespace MESE. dao {public class AdminDao: DaoBase, IAdminDao {public bool Exists (string code) {return base. exists (x => x. code = code);} public Admin Read (string code) {return base. read (x => x. code = code);} public bool Delete (string code) {return base. delete (x => x. code = code) ;}} 2.3 RoleDao implementation class is similar to AdminDao: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; using MESE. IDao; namespace MESE. dao {public class RoleDao: DaoBase , IRoleDao {public bool Exists (string code) {return base. exists (x => x. code = code);} public Role Read (string code) {return base. read (x => x. code = code);} public bool Delete (string code) {return base. delete (x => x. code = code) ;}}2.4 CategoryDao implementation class requires the QueryByParent method: [csharp] using System; using System. collections. generic; using System. linq; using System. text; using MESE. EF5; using MESE. IDao; namespace MESE. dao {public class CategoryDao: DaoBase , ICategoryDao {public bool Exists (string code) {return base. exists (x => x. code = code);} public Category Read (string code) {return base. read (x => x. code = code);} public bool Delete (string code) {return base. delete (x => x. code = code);} public IList QueryByParent (string parent) {return string. IsNullOrEmpty (parent )? Base. queryItems (x => x. parentCode = null | x. parentCode = ""). orderBy (x => x. sort ). toList (): base. queryItems (x => x. parentCode = parent ). orderBy (x => x. sort ). toList () ;}} so far, the data access layers Admin, Role, and Category have been completed. Later, we will compile the Service layer, Web layer code, and various types of MVC in ExtJS, in this way, we can build a basic ExtJS framework.

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.