ASP.NET Core实践:ABP模块化&统一返回结果

一、ABP框架

1、简介

ASP.NET Boilerplate (ABP) is an open source and well-documented application framework. It's not just a framework, it also provides a strong architectural model based on Domain Driven Design, with all the best practices in mind.

2、特点

(1)模块化

  • 模块结构

包括 IOC、应用配置、应用startup&shutdown的事件

  • 使用

注册服务

实现模块的属性、事件及模块依赖

(2)默认仓库

提供了完善的数据库操作方法

(3)工作单元

  • 默认工作单元

Some methods are unit of work methods by default:

All MVC, Web API and ASP.NET Core MVC Controller actions.
All Application Service methods.
All Repository methods.

  • 使用特性

[UnitOfWork]

(4)多语言

  • 多数据源

    支持Json、XML、Resource、自定义数据源

  • 满足各种使用场景

    Class、Razor View、JS

(5)自动映射

[AutoMapTo(typeof(User))]

[AutoMapFrom(typeof(UserDto))]

[AutoMap]

(6)动态API

直接将“应用服务层”的方法生成Restful API进行暴露

(7)统一返回结果

效果

{
  "result": {
  ...
  },
  "targetUrl": null,
  "success": true,
  "error": null,
  "unAuthorizedRequest": false,
  "__abp": true
}

模型类

(8)...

异常处理、动态JS代理等

二、ABP模块化-返回结果分析

1、Startup注册ABP模块化服务

// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
	//此处省略其它注册服务
	...
	
	//Configure Abp and Dependency Injection
	return services.AddAbp<WebApiModule>(options =>
	{
		//Configure Log4Net logging
		options.IocManager.IocContainer.AddFacility<LoggingFacility>(
			 f => f.UseAbpLog4Net().WithConfig("log4net.config")
		 );
	});

第一个模块化类

    [DependsOn(
        typeof(WebCoreModule))]
    public class WebApiModule: AbpModule
    {
        private readonly IConfigurationRoot _appConfiguration;

        public WebApiModule(IWebHostEnvironment env)
        {
            _appConfiguration = AppConfigurations.Get(env.ContentRootPath, env.EnvironmentName);
        }

2、针对AbpModule的服务容器扩展

 调用MvcOptions的扩展

 3、MvcOptions的扩展

添加了结果过滤器Action Result Filter

 4、结果过滤器

 5、包装器工厂

 6、包装器

 7、统一的返回结果

三、参考

  • ABP官网

https://aspnetboilerplate.com/Pages/Documents/Introduction

原文地址:https://www.cnblogs.com/hepc/p/13894433.html