asp.net core 中Service层的实现样板是怎样的

技术asp.net core 中Service层的实现样板是怎样的这期内容当中小编将会给大家带来有关asp.net core 中Service层的实现样板是怎样的,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文

本期,边肖将为大家带来ASP.NET芯服务层的实现模式。文章内容丰富,从专业角度进行分析和描述。希望你看完这篇文章能有所收获。

00-1010通常,我们会将用户表和登录信息表放在两个表中。为什么要这样设计?原因如下:

划分功能,用户信息管理是用户管理,登录是登录。

增加安全性,减少无关信息的查询。比如访问登录界面不会联合检索用户的常用信息,在管理用户信息时也不会带来登录信息。

等等

废话不多说,直接上代码:

命名空间数据。枚举

{

///摘要

///登录类型

////摘要

公共枚举LoginType :字节

{

///令牌登录

令牌,

///用户名和密码

密码

}

///摘要

///性别

////摘要

公共枚举

{

///男

男性,

///女性

女性,

///隐私

没有人

}

}

SysUserAuthEntity.cs

使用数据。枚举;

使用数据。基础设施;

命名空间数据。模型

{

公共类SysUserauthentity : baseEntityint

{

公共字符串UserName { get设置;}

公共字符串Password { get设置;}

public LoginType LoginType { get设置;}

}

}

SysUserInfoEntity.cs

使用系统;

使用数据。枚举;

使用数据。基础设施;

命名空间数据。模型

{

公共类SysUserInfoentity : baseEntityint

{

公共字符串昵称{ get设置;}

公共字符串ImageUrl { get设置;}

public SexEnum Sex { get设置;}

公共日期时间?生日{ get设置;}

public int SysUserAuthId { get设置;}

p

ublic virtual SysUserAuthEntity SysUserAuth { get; set; }
   }
}

 

这里并没有使用数据库Sql语句作为数据库描述,而是使用了Entity类作为描述,这是因为数据库到实体类之间还是有一层转换,对于开发而言接触更多的是实体类,而不是数据表。

 

2. 生成 Repository相关

使用工具代码的方式有很多,我在这里推荐一种, Test项目中,添加一个测试类,具体代码如下:

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();
       }
   }
}

 

具体的命令行执行比较麻烦,会执行所有的测试单元:

cd Test/
dotnet test

 

当然了,IDE支持单个测试单元的执行,具体操作这里就不做过多的介绍了。

 

3. Service 接口和实现类

通常Service接口会提供一个简单Crud的Service接口,然后其他业务接口继承该接口。这样可以减少代码的重复,因为重复的代码在开发过程中是非常讨厌的一种情况,因为一旦一处发生变更,其他的也有可能发生变更。所以遇到重复代码一般都会进行一定程度的封装:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Data.Infrastructure;

namespace Service.Insfrastructure
{
   public interface BaseService<T>
   {
       T Get(object key);
       T Get(Expression<Func<T, bool>> predicate);

       PageModel<T> SearchPage(PageCondition<T> condition);

       void Delete(Expression<Func<T, bool>> predicate);

       void Update(T entity);

       List<T> Search(Expression<Func<T, bool>> predicate);

   }
}

 

暂时就提供了这些最常见的请求方法。

在Service.Implements项目中:

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<T> : BaseService<T>
   {
       private IRepository<T> LocalRepository { get; }

       protected BaseServiceImpl(IRepository<T> repository)
       {
           LocalRepository = repository;
       }


       public T Get(object key)
       {
           return LocalRepository.Get(key);
       }

       public T Get(Expression<Func<T, bool>> predicate)
       {
           return LocalRepository.Get(predicate);
       }

       public PageModel<T> SearchPage(PageCondition<T> condition)
       {
           return LocalRepository.Search(condition);
       }

       public void Delete(Expression<Func<T, bool>> predicate)
       {
           LocalRepository.Delete(predicate);
       }

       public void Update(T entity)
       {
           LocalRepository.Update(entity);
       }

       public List<T> Search(Expression<Func<T, bool>> predicate)
       {
           return LocalRepository.Search(predicate);
       }
   }
}

 

这个类设置为抽象类。

 

4. 用户管理的接口

先创建了两个简单的示范接口:

using Data.Models;
using Service.Insfrastructure;

namespace Service
{
   public interface ISysUserService : BaseService<SysUserInfoEntity>
   {
       void Register(SysUserAuthEntity auth, SysUserInfoEntity info);

       void ChangePassword(int userId, string oldPwd, string newPwd);
   }
}

 

实现类:

using System;
using Data.Models;
using Domain.Repository;
using Service.Implements.Insfrastructure;

namespace Service.Implements
{
   public class SysUserServiceImpl : BaseServiceImpl<SysUserInfoEntity>, 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("用户信息已经存在");
           }

           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("原密码错误");
           }

           auth.Password = newPwd;

       }
   }
}

 

这里没对密码进行加密处理,直接使用明文。这在正式开发中是不允许的,密码不能使用明文保存。

上述就是小编为大家分享的asp.net core 中Service层的实现样板是怎样的了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/93986.html

(0)

相关推荐

  • 怎么理解Impala元数据

    技术怎么理解Impala元数据本篇文章给大家分享的是有关怎么理解Impala元数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。背景Impala是一个高性能

    攻略 2021年11月23日
  • LeetCode 树

    技术LeetCode 树 LeetCode 树认识LeetCode树的定义方式
    /*** Definition for a binary tree node.* struct TreeNode {*

    礼包 2021年11月1日
  • 如何理解Java设计模式的装饰模式

    技术如何理解Java设计模式的装饰模式这篇文章主要介绍“如何理解Java设计模式的装饰模式”,在日常操作中,相信很多人在如何理解Java设计模式的装饰模式问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望

    攻略 2021年11月8日
  • 中序遍历python(python前序后序遍历构造二叉树)

    技术如何从前序与中序遍历序列构造python二叉树如何从前序与中序遍历序列构造python二叉树,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获

    攻略 2021年12月13日
  • Python怎样爬取全网美食杰信息

    技术Python怎样爬取全网美食杰信息这期内容当中小编将会给大家带来有关Python怎样爬取全网美食杰信息,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。前言相信很多人是吃货,其实我也是

    攻略 2021年10月25日
  • C++中怎么使用try和catch捕获异常

    技术C++中怎么使用try和catch捕获异常这篇文章主要介绍“C++中怎么使用try和catch捕获异常”,在日常操作中,相信很多人在C++中怎么使用try和catch捕获异常问题上存在疑惑,小编查阅了各式资料,整理出

    攻略 2021年11月29日