ABP之动态WebAPI

ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只是将ApiController公共化,对于这一点的处理类似于MVC,对服务端的 调用没有跨过HttpHandler一样),这样不仅减少了ApiController的开发,也更能体现驱动领域设计的层结构。

对WebApi服务的替换与路由配置

AbpWebApiModule是Abp.Web.Api的模块类,该类中定义InitializeAspNetServices,InitializeRoutes两个方法,并且在模块的Initialize方法中执行,这两个方法分别是对WebApi的服务的替换与路由的配置,。这两处对WebApi的变更才使得直接调用服务层成为可能。

  1.  

对服务的分析与缓存

再对服务信息的存储上,作者提供了DynamicApiControllerInfo,DynamicApiActionInfo(源码中的DynamicApiMethodInfo.cs),其中DynamicApiControllerInfo包含了一DynamicApiActionInfo集合。

在执行AbpHttpControllerSelector, AbpApiControllerActionSelector, AbpControllerActivator的时候,系统已经在初始化的时候对服务层进行了分析与缓存。

在作者给的Demo SimpleTaskSystem下有一模块类SimpleTaskSystemWebApiModule

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[DependsOn(typeof(AbpWebApiModule))] //We declare depended modules explicitly
public class SimpleTaskSystemWebApiModule : AbpModule
{
    public override void Initialize()
    {
        //This code is used to register classes to dependency injection system for this assembly using conventions.
        IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
 
        //Creating dynamic Web Api Controllers for application services.
        //Thus, 'web api layer' is created automatically by ABP.
 
        DynamicApiControllerBuilder
            .ForAll<IApplicationService>(Assembly.GetAssembly(typeof (SimpleTaskSystemApplicationModule)), "tasksystem")
            .Build();
    }
}

  

在这里是使用到了DynamicApiControllerBuilder,这个类主要是对服务接口进行一个注册,再由IBatchApiControllerBuilder按照注册的服务接口对提供的程序集进行分析。

DynamicApiControllerBuilder提供的ForAll只是返回的一个IBatchApiControllerBuilder实现对象

1
2
3
4
public static IBatchApiControllerBuilder<T> ForAll<T>(Assembly assembly, string servicePrefix)
{
    return new BatchApiControllerBuilder<T>(assembly, servicePrefix);
}

  

这个方法为BatchApiControllerBuilder提供了服务接口与服务接口与需要分析的程序集,以及服务地址前缀。

BatchApiControllerBuilder从程序集中获取实现服务接口的非抽象类。BatchApiControllerBuilder再通过DynamicApiControllerBuilder将这些类与服务名信息传递给IApiControllerBuilder。

  

IApiControllerBuilder将通过服务类生成DynamicApiControllerInfo,再将IApiControllerBuilder存储于DynamicApiControllerManager中,同时分析服务类,将公开非静态方法作为action,存储到DynamicApiControllerManager.Actions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
internal class ApiControllerBuilder<T> : IApiControllerBuilder<T>
{
    /// <summary>
    /// Name of the controller.
    /// </summary>
    private readonly string _serviceName;
 
    /// <summary>
    /// List of all action builders for this controller.
    /// </summary>
    private readonly IDictionary<string, ApiControllerActionBuilder<T>> _actionBuilders;
 
    /// <summary>
    /// Action Filters to apply to the whole Dynamic Controller.
    /// </summary>
    private IFilter[] _filters;
 
    /// <summary>
    /// Creates a new instance of ApiControllerInfoBuilder.
    /// </summary>
    /// <param name="serviceName">Name of the controller</param>
    public ApiControllerBuilder(string serviceName)
    {
        if (string.IsNullOrWhiteSpace(serviceName))
        {
            throw new ArgumentException("serviceName null or empty!""serviceName");
        }
 
        if (!DynamicApiServiceNameHelper.IsValidServiceName(serviceName))
        {
            throw new ArgumentException("serviceName is not properly formatted! It must contain a single-depth namespace at least! For example: 'myapplication/myservice'.""serviceName");
        }
 
        _serviceName = serviceName;
 
        _actionBuilders = new Dictionary<string, ApiControllerActionBuilder<T>>();
        foreach (var methodInfo in DynamicApiControllerActionHelper.GetMethodsOfType(typeof(T)))
        {
            _actionBuilders[methodInfo.Name] = new ApiControllerActionBuilder<T>(this, methodInfo);
        }
    }
 
    /// <summary>
    /// The adds Action filters for the whole Dynamic Controller
    /// </summary>
    /// <param name="filters"> The filters. </param>
    /// <returns>The current Controller Builder </returns>
    public IApiControllerBuilder<T> WithFilters(params IFilter[] filters)
    {
        _filters = filters;
        return this;
    }
 
    /// <summary>
    /// Used to specify a method definition.
    /// </summary>
    /// <param name="methodName">Name of the method in proxied type</param>
    /// <returns>Action builder</returns>
    public IApiControllerActionBuilder<T> ForMethod(string methodName)
    {
        if (!_actionBuilders.ContainsKey(methodName))
        {
            throw new AbpException("There is no method with name " + methodName + " in type " typeof(T).Name);
        }
 
        return _actionBuilders[methodName];
    }
 
    /// <summary>
    /// Builds the controller.
    /// This method must be called at last of the build operation.
    /// </summary>
    public void Build()
    {
        var controllerInfo = new DynamicApiControllerInfo(_serviceName, typeof(DynamicApiController<T>), _filters);
         
        foreach (var actionBuilder in _actionBuilders.Values)
        {
            if (actionBuilder.DontCreate)
            {
                continue;
            }
 
            controllerInfo.Actions[actionBuilder.ActionName] = actionBuilder.BuildActionInfo();
        }
 
        IocManager.Instance.IocContainer.Register(
            Component.For<AbpDynamicApiControllerInterceptor<T>>().LifestyleTransient(),
            Component.For<DynamicApiController<T>>().Proxy.AdditionalInterfaces(new[] { typeof(T) }).Interceptors<AbpDynamicApiControllerInterceptor<T>>().LifestyleTransient()
            );
 
        DynamicApiControllerManager.Register(controllerInfo);
 
        LogHelper.Logger.DebugFormat("Dynamic web api controller is created for type '{0}' with service name '{1}'."typeof(T).FullName, controllerInfo.ServiceName);
    }
}

  

ABP之动态WebAPI的更多相关文章

  1. ABP源码分析三十五:ABP中动态WebAPI原理解析

    动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类就可以对外提供WebAPI的功能, ...

  2. ABP之动态WebAPI(一)

    ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只是将ApiController公共化,对于这一点的处理类似于MVC,对服务端的 调用没有跨过HttpHan ...

  3. ABP中动态WebAPI原理解析

    ABP中动态WebAPI原理解析 动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类 ...

  4. ABP之动态WebAPI(二)

    HttpControllerDescriptor与HttpActionDescriptor HttpControllerDescriptor封装了某个HttpController类型的元数据,我们可以 ...

  5. 动态WebApi

    动态WebApi实现了直接对Service的调用,其实没有跨过ApiController,只是我们自己创建出ApiController 实现主要分以下几步 一 对默认WebApi服务的替换 ApiGl ...

  6. ABP项目中使用Swagger生成动态WebAPI

    本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...

  7. 最新版ABP 动态WebAPI 日期转json带T的解决方案| ABP DateTIme Json format

    ABP动态webapi返回的json数据中,日期时间带T还有毫秒数的问题,在以往的版本中可以使用下面方法解决: 在XXXAbpWebApiModule中加上下面的代码: 很老的很老的版本有效: pub ...

  8. [ABP框架]动态web Api的拦截用法。

    先进行配置 首先这种需求,一般发生在APP端,我们给APP,不会给所有项目系统的接口给他们用.我们系统有200个接口,但是APP的需求只会用20个.那么这个需求也就应运而生了. 以上为API文件夹中为 ...

  9. ASP.NET Core 奇淫技巧之动态WebApi

    一.前言 接触到动态WebApi(Dynamic Web API)这个词的已有几年,是从ABP框架里面接触到的,当时便对ABP的这个技术很好奇,后面分析了一波,也尝试过从ABP剥离一个出来作为独立组件 ...

随机推荐

  1. IT行业为什么没有进度

    参加工作多年了,来来回回参与了N多项目,大部分都是政府性的招标项目.每个项目都是顺利进行验收,在这些验收过的项目中大部分都或多或少都有一定的时间延期,每个项目都能够和合同或者需求说明书对应的上,但是真 ...

  2. 【原创】leetCodeOj ---Partition List 解题报告

    原题地址: https://oj.leetcode.com/problems/partition-list/ 题目内容: Given a linked list and a value x, part ...

  3. 数据结构c字符串操作语言版本

    #include<stdio.h> #include<malloc.h> #include<string.h> //结构的定义字符串 typedef struct ...

  4. B. Dreamoon and WiFi(Codeforces Round 272)

    B. Dreamoon and WiFi time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. Please read “Security” section of the manual to find out how to run mysqld as root!错误解决(转)

    2016-03-12T15:40:45.717762Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please ...

  6. dwz 照片回头处理

    我的要求.要选择封面文章,回头一看,实现,查找回头功能bringBack代码中发现的,它们朝着input 标签处理,所以img总是标签不能显示,这么dwz源所做的更改,于dwz.databases.j ...

  7. 获取Winform窗体、工作区 宽度、高度、命名空间、菜单栏高度等收集

    MessageBox.Show("当前窗体标题栏高"+(this.Height - this.ClientRectangle.Height).ToString());//当前窗体标 ...

  8. 【转】ubuntu终端方向键不能用(主机名不显示)问题的解决

    sudo gedit /etc/passwd 在/etc/passwd中修改该用户对应的shell:/bin/sh改为/bin/bash即可解决该问题 来自:http://blog.csdn.net/ ...

  9. 【翻译】Ext JS最新技巧——2014-10-30

    原文:Top Support Tips Greg Barry:Ext JS 5的ExtraParams Ext JS 4同意用户直接将extraParams加入到一个链接,相似例如以下代码: Ext. ...

  10. OAuth打造webapi认证服务

    使用OAuth打造webapi认证服务供自己的客户端使用 一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorizati ...