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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM