02.Abp-Notes:模块化

02.Abp-Notes:模块化

官方文档:https://docs.abp.io/zh-Hans/abp/latest/Module-Development-Basics

模块依赖

创建Abp模块

  • 创建一个类库项目Kawe.Abp.Learning.AccountAbpModule,通过Nugget,添加Volo.Abp项目的引用
  • 添加模块类AccountAbpModule
    public class AccountAbpModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddSingleton<AccountService>();
        }
    }
  • 添加服务类AccountService:
    public class AccountService
    {
        public void Run()
        {
            Console.WriteLine("Hello, I'm AccountService!");
        }
    }

使用模块

在上一节创建的项目Kawe.Abp.Leaning.ConsoleClient中使用AccountAbpModule模块

  • 使用特性 [DependsOn(typeof(AccountAbpModule))]显示添加模块依赖
namespace Kawe.Abp.Leaning.ConsoleClient
{
    [DependsOn(typeof(AccountAbpModule))]
    public class ConsoleClientModule : AbpModule
    {
       public override void ConfigureServices(ServiceConfigurationContext context)
        {
            //在此处注入依赖项
            context.Services.AddSingleton<IMyService,MyService>();
        }
    }
}
  • Program.cs
namespace Kawe.Abp.Leaning.ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var application = AbpApplicationFactory.Create<ConsoleClientModule>())
            {
                application.Initialize();

                var accountService = application.ServiceProvider.GetService<AccountService>();
                accountService.Run();

                Console.ReadLine();
                application.Shutdown();
            }
        }
    }
}

输出

Hello, I'm AccountService!

通过模块化,模块内的行为和依赖注入到全局application的DI容器中,模块AccountAbpModule的依赖注入也会被注入到application的全局DI容器中

插件式模块

参考下Abp Framework(不是Abp vNext)
Abp Framework 插件模块

创建模块:BlogMoudle

  • 新建一个解决方案Kawe.Abp.Learning.BlogMoudle
  • 并添加一个类库项目Kawe.Abp.Learning.BlogMoudle,通过Nugget,添加Volo.Abp项目的引用
  • 添加模块类BlogAbpMoudle
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp;
using Volo.Abp.Modularity;

namespace Kawe.Abp.Learning.BlogMoudle
{
    public class BlogAbpMoudle : AbpModule
    {
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            base.OnApplicationInitialization(context);
            Console.WriteLine("BlogAbpMoudle->OnApplicationInitialization");
        }
    }
}

使用插件模块

  • 在控制台项目Kawe.Abp.Leaning.ConsoleClient中,新建一个文件夹MyPlugIns,
  • 把项目Kawe.Abp.Learning.BlogMoudle生成的Kawe.Abp.Learning.BlogMoudle.dll文件拷贝到文件夹MyPlugIns中,
    并右键Kawe.Abp.Learning.BlogMoudle.dll->属性->复制到输出目录->选择:始终复制
  • 使用options.PlugInSources.AddFolder()导入插件模块:
        static void Main(string[] args)
        {
            using (var application = AbpApplicationFactory.Create<ConsoleClientModule>(options =>
            {
                options.PlugInSources.AddFolder(@$"{AppDomain.CurrentDomain.BaseDirectory}/MyPlugIns");
            }))
         ......

运行控制台程序,输出:

BlogAbpMoudle->OnApplicationInitialization
Hello, I'm BlogService!
posted @ 2020-11-11 17:31  easy5  阅读(182)  评论(0编辑  收藏  举报