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

What is the implementation template of Service layer in asp.net core

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

Share

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

In this issue, the editor will bring you about the implementation template of the Service layer in asp.net core. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Data sheet

In general, we put the user table and the login information table in two tables. Why is it designed in this way? For the following considerations:

To divide the function, user information management is user management, login is login.

Increase security and reduce queries for irrelevant information, for example, accessing the login interface will not retrieve users' common information, and login information will not be brought here when managing user information.

Wait

Don't say much nonsense, just go to the code:

Namespace Data.Enums

{

/ / /

/ / Login type

/ / /

Public enum LoginType: byte

{

/ token login

Token

/ / user name and password

Password

}

/ / /

/ gender

/ / /

Public enum SexEnum

{

/ / male

Male

/ / female

Female

/ / privacy

None

}

}

SysUserAuthEntity.cs

Using Data.Enums

Using Data.Infrastructure

Namespace Data.Models

{

Public class SysUserAuthEntity: BaseEntity

{

Public string UserName {get; set;}

Public string Password {get; set;}

Public LoginType LoginType {get; set;}

}

}

SysUserInfoEntity.cs

Using System

Using Data.Enums

Using Data.Infrastructure

Namespace Data.Models

{

Public class SysUserInfoEntity: BaseEntity

{

Public string NickName {get; set;}

Public string ImageUrl {get; set;}

Public SexEnum Sex {get; set;}

Public DateTime? BirthDay {get; set;}

Public int SysUserAuthId {get; set;}

Public virtual SysUserAuthEntity SysUserAuth {get; set;}

}

}

Instead of using the database Sql statement as the database description, we use the Entity class as the description, because there is still a layer of transformation between the database and entity classes, and for developers, contact is more about entity classes than data tables.

two。 Generate Repository correlation

There are many ways to use tool code. Here I recommend adding a test class to the Test project as follows:

Using NUnit.Framework

Using System

Using System.Collections.Generic

Using System.Text

Using Utils.Develop

Namespace Test

{

Public class DevelopTest

{

[Test]

Public void TetDevelop ()

{

Var d = Develop.CurrentDirect

Console.WriteLine (d)

Assert.IsTrue (d.Contains ("template"))

Var entities = Develop.LoadEntities ()

Foreach (var item in entities)

{

Console.WriteLine (item.FullName)

}

}

[Test]

Public void TestCreateDevelop ()

{

Var entities = Develop.LoadEntities ()

Foreach (var item in entities)

{

Develop.CreateRepositoryInterface (item)

Develop.CreateRepositoryImplement (item)

Develop.CreateEntityTypeConfig (item)

}

Assert.Pass ()

}

}

}

Specific command line execution is cumbersome, and all test units are executed:

Cd Test/

Dotnet test

Of course, IDE supports the execution of a single test unit, so I won't go into too much detail here.

3. Service interface and implementation class

Usually the Service interface provides a Service interface for a simple Crud, and then other business interfaces inherit that interface. This reduces code duplication, because duplicate code is a very annoying situation in the development process, because once a change occurs in one place, it is possible for others to change as well. Therefore, when you encounter duplicate code, you will generally do a certain degree of encapsulation:

Using System

Using System.Collections.Generic

Using System.Linq.Expressions

Using Data.Infrastructure

Namespace Service.Insfrastructure

{

Public interface BaseService

{

T Get (object key)

T Get (Expression predicate)

PageModel SearchPage (PageCondition condition)

Void Delete (Expression predicate)

Void Update (T entity)

List Search (Expression predicate)

}

}

For the time being, these most common request methods are provided.

In the Service.Implements project:

Using System

Using System.Collections.Generic

Using System.Linq.Expressions

Using Data.Infrastructure

Using Domain.Insfrastructure

Using Service.Insfrastructure

Namespace Service.Implements.Insfrastructure

{

Public abstract class BaseServiceImpl: BaseService

{

Private IRepository LocalRepository {get;}

Protected BaseServiceImpl (IRepository repository)

{

LocalRepository = repository

}

Public T Get (object key)

{

Return LocalRepository.Get (key)

}

Public T Get (Expression predicate)

{

Return LocalRepository.Get (predicate)

}

Public PageModel SearchPage (PageCondition condition)

{

Return LocalRepository.Search (condition)

}

Public void Delete (Expression predicate)

{

LocalRepository.Delete (predicate)

}

Public void Update (T entity)

{

LocalRepository.Update (entity)

}

Public List Search (Expression predicate)

{

Return LocalRepository.Search (predicate)

}

}

}

This class is set as an abstract class.

4. User managed interface

First, two simple demonstration interfaces are created:

Using Data.Models

Using Service.Insfrastructure

Namespace Service

{

Public interface ISysUserService: BaseService

{

Void Register (SysUserAuthEntity auth, SysUserInfoEntity info)

Void ChangePassword (int userId, string oldPwd, string newPwd)

}

}

Implementation class:

Using System

Using Data.Models

Using Domain.Repository

Using Service.Implements.Insfrastructure

Namespace Service.Implements

{

Public class SysUserServiceImpl: BaseServiceImpl, ISysUserService

{

Protected ISysUserAuthRepository AuthRepository {get;}

Protected ISysUserInfoRepository InfoRepository {get;}

Public SysUserServiceImpl (ISysUserAuthRepository authRepository, ISysUserInfoRepository infoRepository): base (

InfoRepository)

{

AuthRepository = authRepository

InfoRepository = infoRepository

}

Public void Register (SysUserAuthEntity auth, SysUserInfoEntity info)

{

Var authItem = AuthRepository.Get (p = > p.LoginType = = auth.LoginType & & p.UserName = = auth.UserName)

If (authItem! = null)

{

Throw new Exception ("user information already exists")

}

Info.SysUserAuth = auth

InfoRepository.Insert (info)

}

Public void ChangePassword (int userId, string oldPwd, string newPwd)

{

Var info = InfoRepository.Get (userId)

Var auth = AuthRepository.Get (info.SysUserAuthId)

If (oldPwd = = null | | oldPwd! = auth?.Password)

{

Throw new Exception ("original password error")

}

Auth.Password = newPwd

}

}

}

The password is not encrypted here, so plaintext is used directly. This is not allowed in formal development, and passwords cannot be saved in clear text.

The above is the implementation template of the Service layer in asp.net core shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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