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 define data access layer in asp.net core

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to define the data access layer in asp.net core, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

1. Data entity

In general, the fields in the data entity of a project are not completely irregular. Typically, there must be a primary key. In some cases, it is required to increase the last modification time and creation time in the data table, as well as the primary key of the creator and modifier.

So, we can create a generic parent class to help us define these public fields:

Using System

Namespace Data.Infrastructure

{

Public class BaseEntity

{

Public T Id {get; set;}

Public string ModifyUserId {get; set;}

Public DateTime? ModifyTime {get; set;}

Public string CreatorId {get; set;}

Public DateTime? CreateTime {get; set;}

}

}

In the above code, the namespace is not in Data, but in Data.Infrastructure. This namespace Infrastructure is used to store some of the project's schema classes or interfaces, as well as other classes.

So, add some potentially useful methods to this class:

Public void Create (object userId)

{

CreatorId = userId.ToString ()

CreateTime = DateTime.Now

}

Public void Create (object userId, DateTime createTime)

{

CreatorId = userId.ToString ()

CreateTime = createTime

}

Public void Modify (object userId)

{

ModifyUserId = userId.ToString ()

ModifyTime = DateTime.Now

}

Public void Modify (object userId, DateTime modifyTime)

{

ModifyUserId = userId.ToString ()

ModifyTime = modifyTime

}

Here to save the user ID fields, I use a string to save, is to borrow the string type to save data can hold more data types.

two。 Common data operation interface

In normal development, a complete data manipulation interface will have many categories, but many times we need to add, delete and query these two operations separately. For the database, the view and some data tables are not allowed to change, so it is necessary for us to open the query interface only to the caller, but not to modify the interface.

Therefore, there should be the following two interfaces under Domain:

Using System

Using System.Collections.Generic

Using System.Linq.Expressions

Namespace Domain.Infrastructure

{

/ / /

/ / modify the API

/ / /

/ / /

Public interface IModifyRepository

{

/ / /

/ / insert data

/ / /

/ / /

/ / /

T Insert (T entity)

/ / /

/ / insert data

/ / /

/ / /

Void Insert (params T [] entities)

/ / /

/ / insert data

/ / /

/ / /

Void Insert (IEnumerable entities)

/ / /

/ / Save submitted changes

/ / /

/ / /

Void Update (T entity)

/ / /

/ / Save submitted changes

/ / /

/ / /

Void Update (params T [] entities)

/ / /

/ Update data

/ / /

/ / /

/ / /

Void Update (Expression predicate, Expression updator)

/ / /

/ / Delete

/ / /

/ / /

Void Delete (T entity)

/ / /

/ / Delete data

/ / /

/ / /

Void Delete (params T [] entities)

/ / /

/ / Delete data according to conditions

/ / /

/ / /

Void Delete (Expression predicate)

/ / /

/ / Delete the data corresponding to the primary key

/ / /

/ / /

Void DeleteByKey (object key)

/ / /

/ / Delete the data corresponding to the primary key

/ / /

/ / /

Void DeleteByKeys (params object [] keys)

}

}

The above is the update interface, so let's go back to write the query interface, there are many ways to query the interface. Let's first create an interface file:

Using System

Using System.Linq.Expressions

Namespace Domain.Infrastructure

{

/ / /

/ / query API

/ / /

/ / /

Public interface ISearchRepository

{

}

}

A query interface should include the following methods:

Get a single data

/ / /

/ / obtain data according to the primary key

/ / /

/ / /

/ / /

T Get (object key)

/ / /

/ / query

/ / /

/ / /

/ / /

T Get (Expression predicate)

Statistics:

/ / /

/ / return the data entry in the database

/ / /

/ / /

Int Count ()

/ / /

/ / return the data entry in the database with the type Long

/ / /

/ / /

Long LongCount ()

/ / /

/ / return the number of eligible data

/ / /

/ / /

/ / /

Int Count (Expression predicate)

/ / /

/ / returns the qualified number of long shaping

/ / /

/ / /

/ / /

Long LongCount (Expression predicate)

Existence judgment

/ / /

/ / whether there is data that meets the conditions

/ / /

/ / /

/ / /

Bool IsExists (Expression predicate)

Query

/ /

/ / return all records in the database

/ / /

/ / /

List Search ()

/ / /

/ / return all eligible data

/ / /

/ / /

/ / /

List Search (Expression predicate)

/ / /

/ / return an object that delays the query

/ / /

/ / /

IEnumerable Query ()

/ / /

/ / return an object that delays the query, and preset a query condition

/ / /

/ / /

/ / /

IEnumerable Query (Expression predicate)

Sort

/ / /

/ / sort query, default ascending order

/ / /

/ / /

/ / /

/ / /

/ / /

List Search

(Expression predicate, Expression order)

/ / /

/ / sort lookup, specifying whether to sort in descending order

/ / /

/ / /

/ / /

/ / /

/ / /

/ / /

List Search

(Expression predicate, Expression order, bool isDesc)

Pagination

In fact, the paging interface definition model requires the assistance of two classes, without which the definition of the interface will become very complex, which is not conducive to the readability of the code:

Using System

Using System.Collections.Generic

Using System.Linq.Expressions

Namespace Data.Infrastructure

{

/ / /

/ / paging condition model

/ / /

/ / /

Public class PageCondition

{

/ / /

/ / query conditions

/ / /

/ / /

Public Expression Predicate {get; set;}

/ / /

/ / sort field

/ / /

/ / /

Public string OrderProperty {get; set;}

/ / /

/ / sort in ascending or descending order, asc or empty in ascending order and desc in descending order

/ / /

/ / /

Public string Sort {get;set;}

/ / /

/ / the maximum capacity of big data per page

/ / /

/ / /

Public int PerpageSize {get; set;}

/ / /

/ current page

/ / /

/ / /

Public int CurrentPage {get; set;}

}

/ / /

/ / paging result

/ / /

/ / /

Public class PageModel

{

/ / /

/ / data

/ / /

/ / /

Public List Items {get; set;}

/ / /

/ / current page number

/ / /

/ / /

Public int CurrentPage {get; set;}

/ / /

/ / the maximum capacity of big data per page

/ / /

/ / /

Public int PerpageSize {get; set;}

/ / /

/ / Total number of queried data

/ / /

/ / /

Public long TotalCount {get; set;}

/ / /

/ / Total page number

/ / /

/ / /

Public int TotalPages {get; set;}

}

}

These are two helper classes, and you can simply see that if these parameters are passed directly to the method without encapsulation, the method's parameter list can be expected to be particularly long, which is a disaster for readability and maintainability. I once took over the maintenance of a project, the last developer wrote nearly 15 parameters in one method, and there are a large number of optional parameters, well, it's a headache. Therefore, I do not recommend that you write like this. I suggest encapsulating a method with more than 4 parameters.

So, take a look at the declaration of the method:

/ / /

/ / query paging according to the paging parameter settings

/ / /

/ / /

/ / /

PageModel Search (PageCondition condition)

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report