ABP development framework to develop series --- front and rear ends (4) before and after encapsulation Web API call category and terminal using ABP development series --- development framework (3) and a hierarchical file organization framework

ABP described in the previous application essay organization of the framework of the project, and the project of each class code field tissue layers, and simplifies the content of the individual layers ABP frame, so that we project structure more clearly. The essays have been described in the achievement of the application service layer interface module in the dictionary, and the interface can be tested on the host interface through the Web API of the program running herein, this essay based on the foundation described earlier, introduce Web API calls class packaging and use, including the use of the console and is used to call the package Winform class.

On the essays " front end development series development framework ABP --- (3) frame and a hierarchical file organization ", I plotted the architecture shown ABP improved frame, as shown in FIG.

03-Application.Common common application services layer layered inside this project, we are mainly placed inside each module DTO and application of public service interface class. With these DTO classes and interfaces files, we do not have to repeat the preparation of the content of this part of the client (such as Winform client, console, WPF / UWP, etc.), can be used directly.

The DTO class file documents and interfaces, our main purpose is to encapsulate the client calls the class Web API calls, so when we use the interface, calling more convenient.

1) Web API calls class encapsulates

To make it easier to call functions in the Web API Console client, Winform clients and other scenes, we need to throw the application service layer interface of Web API package, and then combined with DTO class implements a standard interface.

Because these calls class may be shared among multiple clients, so based on the experience we have accumulated in the mixed framework, we put them to independently manage a project, view the project as shown below.

Wherein the field corresponding to the object is DictDataApiCaller <art objects> ApiCaller of naming rules.

如对于字典模块的API封装类,它们继承一个相同的基类,然后实现特殊的自定义接口即可,这样可以减少常规的Create、Get、GetAll、Update、Delete等操作的代码,这些全部由调用基类进行处理,而只需要实现自定义的接口调用即可。如下是字典模块DictType和DictData两个业务对象的API封装关系。

如对于字典类型的API封装类定义代码如下所示。

    /// <summary>
    /// 字典类型对象的Web API调用处理
    /// </summary>
    public class DictTypeApiCaller : AsyncCrudApiCaller<DictTypeDto, string, DictTypePagedDto, CreateDictTypeDto, DictTypeDto>, IDictTypeAppService
    {
        /// <summary>
        /// 提供单件对象使用
        /// </summary>
        public static DictTypeApiCaller Instance
        {
            get
            {
                return Singleton<DictTypeApiCaller>.Instance;
            }
        }

......

这里我们可以通过单件的方式来使用字典类型API的封装类实例 DictTypeApiCaller.Instance

对于Web API的调用,我们知道,一般需要使用WebClient或者HttpRequest的底层类进行Url的访问处理,通过提供相应的数据,获取对应的返回结果。

而对于操作方法的类型,是使用POST、GET、INPUT、DELETE的不同,需要看具体的接口,我们可以通过Swagger UI 呈现出来的进行处理即可,如下所示的动作类型。

如果处理动作不匹配,如本来是Post的用Get方法,或者是Delete的用Post方法,都会出错。

在Abp.Web.Api项目里面有一个AbpWebApiClient的封装方法,里面实现了POST方法,可以参考来做对应的WebClient的封装调用。

我在它的基础上扩展了实现方法,包括了Get、Put、Delete方法的调用。

我们使用的时候,初始化它就可以了。

apiClient = new AbpWebApiClient();

例如,我们对于常规的用户登录处理,它的API调用封装的操作代码如下所示,这个是一个POST方法。

        /// <summary>
        /// 对用户身份进行认证
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">用户密码</param>
        /// <returns></returns>
        public async virtual Task<AuthenticateResult> Authenticate(string username, string password)
        {
            var url = string.Format("{0}/api/TokenAuth/Authenticate", ServerRootAddress);
            var input = new
            {
                UsernameOrEmailAddress = username,
                Password = password
            };

            var result = await apiClient.PostAsync<AuthenticateResult>(url, input);
            return result;
        }

对于业务接口来说,我们都是基于约定的规则来命名接口名称和地址的,如对于GetAll这个方法来说,字典类型的地址如下所示。

/api/services/app/DictData/GetAll

另外还包括服务器的基础地址,从而构建一个完整的调用地址如下所示。

http://localhost:21021/api/services/app/DictData/GetAll

由于这些规则确定,因此我们可以通过动态构建这个API地址即可。

            string url = GetActionUrl(MethodBase.GetCurrentMethod());//获取访问API的地址(未包含参数)
            url += string.Format("?SkipCount={0}&MaxResultCount={1}", dto.SkipCount, dto.MaxResultCount);

而对于GetAll函数来说,这个定义如下所示。

Task<PagedResultDto<TEntityDto>> GetAll(TGetAllInput input)

它是需要根据一定的条件进行查询的,不仅仅是 SkipCount 和 MaxResultCount两个属性,因此我们需要动态组合它的url参数,因此建立一个辅助类来动态构建这些输入参数地址。

        /// <summary>
        /// 获取所有对象列表
        /// </summary>
        /// <param name="input">获取所有条件</param>
        /// <returns></returns>
        public async virtual Task<PagedResultDto<TEntityDto>> GetAll(TGetAllInput input)
        {
            AddRequestHeaders();//加入认证的token头信息
            string url = GetActionUrl(MethodBase.GetCurrentMethod());//获取访问API的地址(未包含参数)
            url = GetUrlParam(input, url);

            var result = await apiClient.GetAsync<PagedResultDto<TEntityDto>>(url);
            return result;
        }

这样我们这个API的调用封装类的基类就实现了常规的功能了。效果如下所示。

而字典类型的API封装类,我们只需要实现特定的自定义接口即可,省却我们很多的工作量。

namespace MyProject.Caller
{
    /// <summary>
    /// 字典类型对象的Web API调用处理
    /// </summary>
    public class DictTypeApiCaller : AsyncCrudApiCaller<DictTypeDto, string, DictTypePagedDto, CreateDictTypeDto, DictTypeDto>, IDictTypeAppService
    {
        /// <summary>
        /// 提供单件对象使用
        /// </summary>
        public static DictTypeApiCaller Instance
        {
            get
            {
                return Singleton<DictTypeApiCaller>.Instance;
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public DictTypeApiCaller()
        {
            this.DomainName = "DictType";//指定域对象名称,用于组装接口地址
        }

        public async Task<Dictionary<string, string>> GetAllType(string dictTypeId)
        {
            AddRequestHeaders();//加入认证的token头信息
            string url = GetActionUrl(MethodBase.GetCurrentMethod());//获取访问API的地址(未包含参数)
            url += string.Format("?dictTypeId={0}", dictTypeId);

            var result = await apiClient.GetAsync<Dictionary<string, string>>(url);
            return result; 
        }

        public async Task<IList<DictTypeNodeDto>> GetTree(string pid)
        {
            AddRequestHeaders();//加入认证的token头信息
            string url = GetActionUrl(MethodBase.GetCurrentMethod());//获取访问API的地址(未包含参数)
            url += string.Format("?pid={0}", pid);

            var result = await apiClient.GetAsync<IList<DictTypeNodeDto>>(url);
            return result;
        }
    }
}

 

2)API封装类的调用

前面小节介绍了针对Web API接口的封装,以适应客户端快速调用的目的,这个封装作为一个独立的封装层,以方便各个模块之间进行共同调用。

到这里为止,我们还没有测试过具体的调用,还没有了解实际调用过程中是否有问题,当然我们在开发的时候,一般都是一步步来的,但也是确保整个路线没有问题的。

实际情况如何,是骡是马拉出来溜溜就知道了。

首先我们创建一个基于.net Core的控制台程序,项目情况如下所示。

在其中我们定义这个项目的模块信息,它是依赖于APICaller层的模块。

namespace RemoteApiConsoleApp
{
    [DependsOn(typeof(CallerModule))]
    public class MyModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}

在ABP里面,模块是通过一定顺序启动的,如果我们通过AbpBootstrapper类来启动相关的模块,启动模块的代码如下所示。

//使用AbpBootstrapper创建类来处理
using (var bootstrapper = AbpBootstrapper.Create<MyModule>())
{
    bootstrapper.Initialize();

        ..........

模块启动后,系统的IOC容器会为我们注册好相关的接口对象,那么调用API封装类的代码如下所示。

                //使用AbpBootstrapper创建类来处理
                using (var bootstrapper = AbpBootstrapper.Create<MyModule>())
                {
                    bootstrapper.Initialize();

                    #region Role
                    using (var client = bootstrapper.IocManager.ResolveAsDisposable<RoleApiCaller>())
                    {
                        var caller = client.Object;

                        Console.WriteLine("Logging in with TOKEN based auth...");
                        var token = caller.Authenticate("admin", "123qwe").Result;
                        Console.WriteLine(token.ToJson());

                        caller.RequestHeaders.Add(new NameValue("Authorization", "Bearer " + token.AccessToken));

                        Console.WriteLine("Getting roles...");
                        var pagerDto = new PagedResultRequestDto() { SkipCount = 0, MaxResultCount = 10 };
                        var result = caller.GetAll(pagerDto);
                        Console.WriteLine(result.ToJson());

                        Console.WriteLine("Create role...");
                        List<string> permission = new List<string>() { "Pages.Roles" };
                        var createRoleDto = new CreateRoleDto { DisplayName = "test", Name = "Test", Description = "test", Permissions = permission };
                        var roleDto = caller.Create(createRoleDto).Result;
                        Console.WriteLine(roleDto.ToJson());

                        var singleDto = new EntityDto<int>() { Id = roleDto.Id };
                        Console.WriteLine("Getting role by id...");
                        roleDto = caller.Get(singleDto).Result;
                        Console.WriteLine(roleDto);

                        Console.WriteLine("Delete role...");
                        var delResult = caller.Delete(singleDto);
                        Console.WriteLine(delResult.ToJson());

                        Console.ReadLine();
                    }
                    #endregion

上面是对角色的相关接口操作,如果对于我们之前创建的字典模块,那么它的操作代码类似,如下所示。

    #region DictType

    using (var client = bootstrapper.IocManager.ResolveAsDisposable<DictTypeApiCaller>())
    {
        var caller = client.Object;

        Console.WriteLine("Logging in with TOKEN based auth...");
        var token = caller.Authenticate("admin", "123qwe").Result;
        Console.WriteLine(token.ToJson());

        caller.RequestHeaders.Add(new NameValue("Authorization", "Bearer " + token.AccessToken));

        Console.WriteLine("Get All ...");
        var pagerDto = new DictTypePagedDto() { SkipCount = 0, MaxResultCount = 10 };
        var result = caller.GetAll(pagerDto).Result;
        Console.WriteLine(result.ToJson());

        Console.WriteLine("Get All by condition ...");
        var pagerdictDto = new DictTypePagedDto() { Name = "民族" };
        result = caller.GetAll(pagerdictDto).Result;
        Console.WriteLine(result.ToJson());
        
        Console.WriteLine("Get count by condition ...");
        pagerdictDto = new DictTypePagedDto() {};
        var count = caller.Count(pagerdictDto).Result;
        Console.WriteLine(count);
        Console.WriteLine();

        Console.WriteLine("Create DictType...");
        var createDto = new CreateDictTypeDto { Id = Guid.NewGuid().ToString(), Name = "Test", Code = "Test" };
        var dictDto = caller.Create(createDto).Result;
        Console.WriteLine(dictDto.ToJson());

        Console.WriteLine("Update DictType...");
        dictDto.Code = "testcode";
        var updateDto = caller.Update(dictDto).Result;
        Console.WriteLine(updateDto.ToJson());

        if (updateDto != null)
        {
            Console.WriteLine("Delete DictType...");
            caller.Delete(new EntityDto<string>() { Id = dictDto.Id });
        }

    }
    #endregion

测试字典模块的处理,执行效果如下所示。

删除内容,我们是配置为软删除的,因此可以通过数据库记录查看是否标记为删除了。

同时,我们可以看到审计日志里面,有对相关应用层接口的调用记录。

以上就是.net core控制台程序中对于API封装接口的调用,上面代码如果需要在.net framework里面跑,也是一样的,我同样也做了一个基于.net framework控制台程序,代码调用都差不多的,它的ApiCaller我们做成了 .net standard程序类库的,因此都是通用的。

前面我们提到,我们的APICaller的类,设计了单件的实例调用,因此我们调用起来更加方便,除了上面使用ABP的启动模块的方式调用外,我们可以用传统的方式进行调用,也就是创建一个ApiCaller的实例对象的方式进行调用,如下代码所示。

    string loginName = this.txtUserName.Text.Trim();
    string password = this.txtPassword.Text;
    AuthenticateResult result = null;
    try
    {
        result = await DictTypeApiCaller.Instance.Authenticate(loginName, password);
    }
    catch(AbpException ex)
    {
        MessageDxUtil.ShowTips("用户帐号密码不正确。\r\n错误信息:" + ex.Message);
        return;
    }

由于篇幅的原因,基于winform界面模块的调用,我在后面随笔在另起一篇随笔进行介绍吧,毕竟那是毕竟漂亮的字典模块呈现了。

 

Guess you like

Origin www.cnblogs.com/wuhuacong/p/10932139.html