Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to abstract CRUD and how to apply T4 template

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to abstract CRUD and how to apply T4 template" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to abstract CRUD and how to apply T4 template" article can help you solve the problem.

Create a Cnblogs.Rdst.IDAO assembly

Series Overview: the full series will introduce in detail the construction of the three-tier abstract factory, as well as EF advanced applications and ASP.NET MVC3.0 simple applications, including Ef, Lambda, Linq, Interface, T4 and so on.

As there are so many technical concepts involved online, the basic concepts will no longer be described in this project.

1.1 first create an Interface folder in the solution to hold the abstract interfaces

1.2 add an assembly named Cnblogs.Rdst.IDAO to the Interface folder

1.3 add references to Domain assemblies and System.Data.Entity assemblies created in Series 1

2. The base interface of the abstract data access layer

2.1 create an IBaseDao interface in the Cnblogs.Rdst.IDAO assembly you just created

2.2 define common CRUD methods in IBaseDao

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cnblogs.Rdst.IDAO {public interface IBaseDao where T:class, new () / / constraint T type must be able to instantiate {/ / get entity object collection IQueryable LoadEntites (Func whereLambda) according to conditions. / / obtain entity object collection paging IQueryable LoadEntites (Func whereLambda, int pageIndex, int pageSize,out int totalCount) according to conditions; / / add T AddEntity (T entity); / / update T UpdateEntity (T entity); / / delete bool DelEntity (T entity) / / delete bool DelEntityByWhere (Func whereLambda) according to conditions;}}

At this point, the CRUD method in the base interface is defined. Next we need to use the T4 template to generate all the entity class interfaces and implement the IBaseDao interface.

Generate all entity class interfaces

3.1 add a T4 text template named IDaoExt

3.2 paste the following code into the template, where the comments need to be changed according to the respective project

Using System; using System.Collections.Generic; using System.Linq; using System.Text; using Cnblogs.Rdst.Domain;// refers to Domain's namespace namespace Cnblogs.Rdst.IDAO / / the namespace where the entity class interface resides {e.Name) / / facilitates entity objects mapped in the edmx model {# > public interface IDao:IBaseDao / / generate entity object interface {}}

3.3After editing the T4 template, save the Ctrl+s, prompt whether to run it or not, and click OK. At this point, it automatically generates all the entity class interfaces for us, and implements the IBaseDao interface, which also has the CRUD method definition.

This is the end of the introduction of "how to abstract CRUD and how to apply T4 template". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report