# DemoNormal\Demo.DI\Demo.DI.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Demo.Entity\Demo.Entity.csproj" />
<ProjectReference Include="..\Demo.Service\Demo.Service.csproj" />
</ItemGroup>
</Project>
DemoNormal\Demo.DI\Program.cs
using Demo.Entity;
using Demo.Service;
//用户信息
var user = new User() { Id = 1, Name = "admin", RoleName = "管理员" };
//新增用户
var userService = new UserService();
userService.AddUser(user);
Console.WriteLine("新增成功!");
//更新用户
userService.UpdateUserRoleName(1, "客服");
Console.WriteLine("更新成功!");
DemoNormal\Demo.Entity\Demo.Entity.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
DemoNormal\Demo.Entity\User.cs
namespace Demo.Entity
{
/// <summary>
/// 用户
/// </summary>
public class User
{
/// <summary>
/// 用户Id
/// </summary>
public long Id { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 角色名
/// </summary>
public string RoleName { get; set; }
}
}
DemoNormal\Demo.Entity\UserChangeLog.cs
namespace Demo.Entity
{
/// <summary>
/// 用户变动日志
/// </summary>
public class UserChangeLog
{
/// <summary>
/// Id
/// </summary>
public long Id { get; set; }
/// <summary>
/// 用户Id
/// </summary>
public long UserId { get; set; }
/// <summary>
/// 旧角色名称
/// </summary>
public string OldRoleName { get; set; }
/// <summary>
/// 新角色名称
/// </summary>
public string NewRoleName { get; set; }
}
}
DemoNormal\Demo.Repository\Demo.Repository.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Demo.Entity\Demo.Entity.csproj" />
</ItemGroup>
</Project>
DemoNormal\Demo.Repository\UserChangeLogRepository.cs
using Demo.Entity;
namespace Demo.Repository
{
/// <summary>
/// 用户变动日志仓储
/// </summary>
public class UserChangeLogRepository
{
/// <summary>
/// 构造函数
/// </summary>
public UserChangeLogRepository()
{
}
/// <summary>
/// 新增日志
/// </summary>
/// <param name="userChangeLog"></param>
/// <returns></returns>
public UserChangeLog AddUserChangeLog(UserChangeLog userChangeLog)
{
return userChangeLog;
}
}
}
DemoNormal\Demo.Repository\UserRepository.cs
using Demo.Entity;
namespace Demo.Repository
{
/// <summary>
/// 用户仓储
/// </summary>
public class UserRepository
{
/// <summary>
/// 构造函数
/// </summary>
public UserRepository()
{
}
/// <summary>
/// 添加用户
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public User AddUser(User user)
{
return user;
}
/// <summary>
/// 获取用户
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public User GetUser(long Id)
{
return new User() { Id = 1, Name = "admin", RoleName = "管理员" };
}
/// <summary>
/// 更新用户角色
/// </summary>
/// <param name="userId"></param>
/// <param name="newRoleName"></param>
public User UpdateUserRoleName(long userId, string newRoleName)
{
return new User() { Id = userId, Name = "admin", RoleName = newRoleName };
}
}
}
DemoNormal\Demo.Service\Demo.Service.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Demo.Entity\Demo.Entity.csproj" />
<ProjectReference Include="..\Demo.Repository\Demo.Repository.csproj" />
</ItemGroup>
</Project>
DemoNormal\Demo.Service\UserChangeLogService.cs
using Demo.Entity;
using Demo.Repository;
namespace Demo.Service
{
/// <summary>
/// 用户变动日志
/// </summary>
public class UserChangeLogService
{
/// <summary>
/// 用户变动日志仓储
/// </summary>
private UserChangeLogRepository _userChangeLogRepository;
/// <summary>
/// 用户仓储
/// </summary>
private UserRepository _userRepository;
/// <summary>
/// 构造函数
/// </summary>
public UserChangeLogService()
{
_userRepository = new UserRepository();
_userChangeLogRepository = new UserChangeLogRepository();
}
/// <summary>
/// 新增日志
/// </summary>
/// <param name="userId"></param>
/// <param name="newRoleName"></param>
/// <returns></returns>
public UserChangeLog AddUserChangeLog(long userId, string newRoleName)
{
//获取用户
var user = _userRepository.GetUser(userId);
//变动信息
var userChangeLog = new UserChangeLog()
{
UserId = userId,
OldRoleName = user.RoleName,
NewRoleName = newRoleName
};
//插入日志
return _userChangeLogRepository.AddUserChangeLog(userChangeLog);
}
}
}
DemoNormal\Demo.Service\UserService.cs
using Demo.Entity;
using Demo.Repository;
namespace Demo.Service
{
/// <summary>
/// 用户业务
/// </summary>
public class UserService
{
/// <summary>
/// 用户仓储
/// </summary>
private UserRepository _userRepository;
/// <summary>
/// 用户日志
/// </summary>
private UserChangeLogService _userChangeLogService;
/// <summary>
/// 构造函数
/// </summary>
public UserService()
{
_userRepository = new UserRepository();
_userChangeLogService = new UserChangeLogService();
}
/// <summary>
/// 新增用户
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public User AddUser(User user)
{
//参数校验
if (user == null)
{
throw new ArgumentNullException("用户对象不可为空!");
}
if (string.IsNullOrEmpty(user.Name))
{
throw new ArgumentNullException("用户名不可为空!");
}
//Todo: 业务逻辑
//保存至数据库,
return _userRepository.AddUser(user);
}
/// <summary>
/// 更新用户角色
/// </summary>
/// <param name="userId"></param>
/// <param name="newRoleName"></param>
public void UpdateUserRoleName(long userId, string newRoleName)
{
//更新角色
_userRepository.UpdateUserRoleName(userId, newRoleName);
//新增日志
_userChangeLogService.AddUserChangeLog(userId, newRoleName);
}
}
}