前言
本文开始前先简单介绍一下DDD分层的概念
表现层(Presentation Layer):图中的用户界面层包括用户接口层,用户输入和数据展示。
应用层(Application Layer):应用层定义系统的业务功能,并指挥领域层中的领域对象实现这些功能。
领域层(Domain Layer):核心层,实现所有业务逻辑。
基础设施层(Infrastructure Layer):提供整个业务系统的基础服务。
接下来将手动创建各层
领域层已经在上一章创建完成 xxx.xxx.Domain
一、完善框架
1.领域层-仓储
在xxx.xxx.Domain项目中新建接口 IBasicDictionaryRepository
/// <summary>
/// 基本字典仓储接口
/// </summary>
public interface IBasicDictionaryRepository
{
/// <summary>
/// 创建基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
BasicDictionary Post(BasicDictionary basicDictionary);
/// <summary>
/// 修改基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
int Put(Guid id, BasicDictionary basicDictionary);
/// <summary>
/// 删除基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
int Delete(Guid basicDictionary);
/// <summary>
/// 查询基础字典
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
IList<BasicDictionary> Get(BasicDictionaryType type);
}
在xxx.xxx.EntityFrameworkCore中添加接口 IBasicDictionaryRepository的实现 BasicDictionaryRepository
/// <summary>
/// 基本字典仓储实现
/// </summary>
public class BasicDictionaryRepository : IBasicDictionaryRepository
{
private PSIDbContext _context;
public BasicDictionaryRepository()
{
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json");
var config = configurationBuilder.Build();
string connection = config["connectionStrings:default"];
DbContextOptions<PSIDbContext> dbContextOption = new DbContextOptions<PSIDbContext>();
DbContextOptionsBuilder<PSIDbContext> dbContextOptionBuilder = new DbContextOptionsBuilder<PSIDbContext>(dbContextOption);
_context = new PSIDbContext(dbContextOptionBuilder.UseSqlServer(connection).Options);
}
/// <summary>
/// 创建基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
public BasicDictionary Post(BasicDictionary basicDictionary)
{
using (_context)
{
_context.Add(basicDictionary);
_context.SaveChanges();
return basicDictionary;
}
}
/// <summary>
/// 修改基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
public int Put(Guid id, BasicDictionary basicDictionary)
{
using (_context)
{
var item = _context.BasicDictionaries.Where(b => b.ID == id).FirstOrDefault();
if (item != null)
{
item.Sort = basicDictionary.Sort;
item.Name = basicDictionary.Name;
item.PinYinCode = basicDictionary.PinYinCode;
}
return _context.SaveChanges();
}
}
/// <summary>
/// 删除基础字典
/// </summary>
/// <param name="basicDictionary"></param>
/// <returns></returns>
public int Delete(Guid id)
{
using (_context)
{
var item = _context.BasicDictionaries.Where(b => b.ID == id).FirstOrDefault();
if (item != null)
{
_context.Remove(item);
}
return _context.SaveChanges();
}
}
/// <summary>
/// 查询基础字典
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public IList<BasicDictionary> Get(BasicDictionaryType type)
{
using (_context)
{
var item = _context.BasicDictionaries.Where(b => b.Type == type).OrderBy(o => o.Sort).ToList();
return item;
}
}
}
2.应用层
新建类库-应用层,名称可自定义如xxx.xxx.Application
新建类库-应用层契约,名称可自定义如xxx.xxx.Application.Contracts
在应用层中添加类BasicDictionaryService
/// <summary>
/// 基础字典服务
/// </summary>
public class BasicDictionaryService : IBasicDictionaryService
{
private IBasicDictionaryRepository _basicDictionaryRepository;
public BasicDictionaryService()
{
_basicDictionaryRepository = new BasicDictionaryRepository();
}
/// <summary>
/// 查询基础字典
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public IList<BasicDictionary> Get(BasicDictionaryType type)
{
return _basicDictionaryRepository.Get(type);
}
}
在应用契约层添加BasicDictionaryDto
public class BasicDictionaryDto
{
[Key]
public Guid ID { get; set; }
/// <summary>
/// 字典代码
/// </summary>
[MaxLength(40), Required]
public string Code { get; set; }
/// <summary>
/// 字典名称
/// </summary>
[MaxLength(80), Required]
public string Name { get; set; }
/// <summary>
/// 拼音码
/// </summary>
[MaxLength(40), Required]
public string PinYinCode { get; set; }
/// <summary>
/// 字典类型
/// </summary>
[Required]
public BasicDictionaryType Type { get; set; }
/// <summary>
/// 字典状态
/// </summary>
[Required]
public BasicDictionaryStatus Status { get; set; }
/// <summary>
/// 排序
/// </summary>
[Required]
public int Sort { get; set; }
}
3.展现层
新建Asp.Net core Web API,名称可自定义如 xxx.xxx.HttpApi.Host
在Program.cs中添加依赖注入 IBasicDictionaryService
using Yudian.PSI.Application;
using Yudian.PSI.Application.Contracts;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
#region Services
builder.Services.AddTransient<IBasicDictionaryService, BasicDictionaryService>();
#endregion
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.MapControllers();
app.Run();
添加controller
/// <summary>
/// 基础字典服务
/// </summary>
[Route("psi/[controller]/v1")]
[ApiController]
public class BasicDictionaryController : ControllerBase
{
private IBasicDictionaryService _basicDictionaryService;
public BasicDictionaryController(IBasicDictionaryService basicDictionaryService)
{
_basicDictionaryService = basicDictionaryService;
}
/// <summary>
/// 根据类别获取基础字典列表
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
[HttpGet]
[Route("{type}")]
public async Task<IActionResult> Get(BasicDictionaryType type)
{
var items = await Task.Factory.StartNew(() =>
{
return _basicDictionaryService.Get(type);
});
return Ok(items);
}
}
总结
最终项目目录结构如下
以上就是今天要讲的内容,下一章节将介绍依赖注入部分。