BodeAbp服務端介紹

BodeAbp服務端只提供api,絕大部分api經過abp的動態WebApi機制提供,原理能夠參考這篇文章: http://www.cnblogs.com/1zhk/p/5418694.html
與業務相關的api寫在模塊內部,這樣服務端幾乎都不須要有Controller了,目前整個項目只有「登陸」和「文件上傳」有Controller代碼,最大化的減小業務模塊與系統的耦合,也更方便業務模塊的加載/卸載。
 
服務端目錄結構:

BodeAbp.Frame:abp框架css

BodeAbp.Modules:業務模塊html

BodeAbp.Plugins:插件web

BodeAbp.Samples:示例數據庫

業務模塊目錄結構:
BodeAbp.Product:功能模塊程序集
Localization:本地化資源文件夾
Providers:模塊權限、菜單、設置項文件夾
Attributes:子功能文件夾(這裏是商品屬性)
Domain:領域層,存放聚合根、領域服務、值對象等。
Dtos:存放數據傳輸對象
ModelConfigs:存放Model配置類
SeedActions:存放種子數據(建立數據庫或遷移數據庫時添加到即數據庫的數據)
...AppServices:應用程序服務,業務實現,是向外提供webapi的基礎
 
Module中代碼:
using System.Reflection;
using Abp.EntityFramework.Default;
using Abp.Localization.Dictionaries;
using Abp.Localization.Dictionaries.Xml;
using Abp.Modules;
using BodeAbp.Product.Providers;

namespace BodeAbp.Product
{
    /// <summary>
    /// 產品模塊
    /// </summary>
    public class BodeAbpProductModule : AbpModule
    {
        /// <summary>
        /// 版本號
        /// </summary>
        public const string CurrentVersion = "0.1.0";

        /// <summary>
        /// 初始化前執行
        /// </summary>
        public override void PreInitialize()
        {
            Configuration.Localization.Sources.Add(
                new DictionaryBasedLocalizationSource(
                    BodeAbpProductConsts.LocalizationSourceName,
                    new XmlEmbeddedFileLocalizationDictionaryProvider(
                        Assembly.GetExecutingAssembly(),
                        "BodeAbp.Product.Localization.Source"
                        )
                    )
                );

            Configuration.Settings.Providers.Add<BodeAbpProductSettingProvider>();
            Configuration.Navigation.Providers.Add<BodeAbpProductNavigationProvider>();
            Configuration.Authorization.Providers.Add<BodeAbpProductAuthorizationProvider>();

            DefaultDbContextInitializer.Instance.MapperAssemblies.Add(Assembly.GetExecutingAssembly());
        }

        /// <summary>
        /// 初始化執行
        /// </summary>
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }

        /// <summary>
        /// 初始化後執行
        /// </summary>
        public override void PostInitialize()
        {
            base.PostInitialize();
        }
    }
}
View Code
關於abp中的模塊機制,能夠參考文章: http://www.cnblogs.com/farb/p/ABPModuleSystem.html
DefaultDbContextInitializer類是BodeAbp默認的數據庫初始化類,能夠仿造其實現將不一樣模塊中的實體註冊到不一樣的DbContext來達到分庫的目的。DefaultDbContext默認讀取webconfig中Default的鏈接字符串。
 
IApplicationService中代碼:
using Abp.Application.Services;
using System.ComponentModel;
using Abp.Application.Services.Dto;
using BodeAbp.Product.Attributes.Dtos;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BodeAbp.Product.Attributes
{
    /// <summary>
    ///  屬性 服務
    /// </summary>
    [Description("屬性接口")]
    public interface IAttributesAppService : IApplicationService
    {
        #region 屬性模版

        /// <summary>
        /// 獲取 屬性模版分頁
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<PagedResultOutput<GetAttributeListOutput>> GetAttributePagedList(QueryListPagedRequestInput input);

        /// <summary>
        /// 獲取 屬性模版詳情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        Task<GetAttributeOutput> GetAttribute(int id);

        /// <summary>
        /// 添加 屬性模版
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task CreateAttribute(CreateAttributeInput input);

        /// <summary>
        /// 更新 屬性模版
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task UpdateAttribute(UpdateAttributeInput input);


        /// <summary>
        /// 刪除 屬性模版
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task DeleteAttribute(List<IdInput> input);

        #endregion

        #region 屬性值

        /// <summary>
        /// 獲取 屬性值分頁
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<PagedResultOutput<GetAttributeOptionListOutput>> GetAttributeOptionPagedList(QueryListPagedRequestInput input);

        /// <summary>
        /// 獲取 屬性值詳情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        Task<GetAttributeOptionOutput> GetAttributeOption(int id);

        /// <summary>
        /// 添加 屬性值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task CreateAttributeOption(CreateAttributeOptionInput input);

        /// <summary>
        /// 更新 屬性值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task UpdateAttributeOption(UpdateAttributeOptionInput input);


        /// <summary>
        /// 刪除 屬性值
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task DeleteAttributeOption(List<IdInput> input);

        #endregion

        #region 分類
        
        /// <summary>
        /// 獲取 分類分頁
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<PagedResultOutput<GetProductClassifyListOutput>> GetClassifyPagedList(QueryListPagedRequestInput input);

        /// <summary>
        /// 獲取 分類詳情
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        Task<GetProductClassifyOutput> GetClassify(int id);

        /// <summary>
        /// 添加 分類
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task CreateClassify(CreateProductClassifyInput input);

        /// <summary>
        /// 更新 分類
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task UpdateClassify(UpdateProductClassifyInput input);


        /// <summary>
        /// 刪除 分類
        /// </summary>
        /// <param name="classifyId">分類Id</param>
        /// <returns></returns>
        Task DeleteClassify(int classifyId);

        #endregion
    }
}
View Code

 

加載模塊:api

在WebApi項目的Module類中加載模塊,代碼以下:
using System.Reflection;
using System.Web.Http;
using Abp.Application.Services;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.WebApi;
using Abp.WebApi.Controllers.Dynamic.Builders;
using Swashbuckle.Application;
using System.Linq;
using System.Web.Http.Cors;
using BodeAbp.Zero;
using System;
using WebDemo.WebApi.Swagger;
using BodeAbp.Activity;
using BodeAbp.Product;

namespace WebDemo.WebApi
{
    [DependsOn(
        typeof(AbpWebApiModule)
        , typeof(WebDemoCoreModule)
        , typeof(BodeAbpZeroModule)
        , typeof(BodeAbpActivityModule)
        , typeof(BodeAbpProductModule))]
    public class WebDemoWebApiModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(WebDemoCoreModule).Assembly, "app")
                .Build();

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(BodeAbpZeroModule).Assembly, "zero")
                .Build();

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(BodeAbpActivityModule).Assembly, "activity")
                .Build();

            DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(BodeAbpProductModule).Assembly, "product")
                .Build();


            Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer"));

            var cors = new EnableCorsAttribute("*", "*", "*");
            GlobalConfiguration.Configuration.EnableCors(cors);
            
            ConfigureSwaggerUi();
        }

        private void ConfigureSwaggerUi()
        {
            Configuration.Modules.AbpWebApi().HttpConfiguration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "WebDemo.WebApi");
                    //c.OperationFilter<AuthorizationOperationFilter>();
                    c.DocumentFilter<ApplicationDocumentFilter>();
                    c.IncludeXmlComments(GetXmlCommentsPath(typeof(WebDemoCoreModule)));
                    c.IncludeXmlComments(GetXmlCommentsPath(typeof(BodeAbpZeroModule)));
                    c.IncludeXmlComments(GetXmlCommentsPath(typeof(BodeAbpActivityModule)));
                    c.IncludeXmlComments(GetXmlCommentsPath(typeof(BodeAbpProductModule)));
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                })
                .EnableSwaggerUi(c => {
                    c.CustomAsset("index", typeof(WebDemoWebApiModule).Assembly, "WebDemo.WebApi.Swagger.index.html");
                    c.InjectStylesheet(typeof(WebDemoWebApiModule).Assembly, "WebDemo.WebApi.Swagger.theme-flattop.css");
                    c.InjectJavaScript(typeof(WebDemoWebApiModule).Assembly, "WebDemo.WebApi.Swagger.translator.js");
                });
        }

        private static string GetXmlCommentsPath(Type moduleType)
        {
            return string.Format(@"{0}\bin\{1}.XML", AppDomain.CurrentDomain.BaseDirectory, moduleType.Assembly.GetName().Name);
        }
    }
}
View Code
 
瀏覽Api:

確保webconfig中數據庫鏈接正確,直接運行項目,瀏覽器訪問:http://localhost:61759/swagger/ui/index#/,效果圖以下:瀏覽器


BodeAbp採用了swagger展現api,關於swagger的配置參考WebApi項目的Module類中的ConfigureSwaggerUi方法。
相關文章
相關標籤/搜索