参考 abp设计ui菜单栏的源码分析,抽出了abp这块自定义扩展的实现。在abp的源码里面有很多地方都用到了这种设计方式,实现了用户自定义扩展。

新建一个空的asp.net core项目,新建一个类,源码:

using microsoft.extensions.options;
using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;

namespace designpatternsample.infrastructure
{
    public class languageinfo
    {
        public string culturename { get; set; }

        public string displayname { get; set; }

        public languageinfo(string culturename, string displayname)
        {
            culturename = culturename;
            displayname = displayname;
        }

    }

    public class abpsampleoptions
    {
        public list<languageinfo> languages { get; }

        public abpsampleoptions()
        {
            languages = new list<languageinfo>();
        }
    }

    public interface ilanguageprovider
    {
        task<ireadonlylist<languageinfo>> getlanguagesasync();
    }

    public class defaultlanguageprovider : ilanguageprovider
    {
        protected abpsampleoptions options { get; }

        public defaultlanguageprovider(ioptions<abpsampleoptions> options)
        {
            options = options.value;
        }

        public task<ireadonlylist<languageinfo>> getlanguagesasync()
        {
            return task.fromresult((ireadonlylist<languageinfo>)options.languages);
        }
    }
}

startup类源码:

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using designpatternsample.infrastructure;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.hosting;

namespace designpatternsample
{
    public class startup
    {
        // this method gets called by the runtime. use this method to add services to the container.
        // for more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkid=398940
        public void configureservices(iservicecollection services)
        {
            // 依赖注入
            services.addtransient<ilanguageprovider, defaultlanguageprovider>();

            // 配置多语言
            services.configure<abpsampleoptions>(options =>
            {
                options.languages.add(new languageinfo( "cs", "čeština"));
                options.languages.add(new languageinfo("en", "english"));
                options.languages.add(new languageinfo("pt-br", "português"));
                options.languages.add(new languageinfo("tr", "türkçe"));
                options.languages.add(new languageinfo("zh-hans", "简体中文"));
            });
        }

        // this method gets called by the runtime. use this method to configure the http request pipeline.
        public void configure(iapplicationbuilder app, iwebhostenvironment env, ilanguageprovider provider)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }

            app.userouting();
            app.useendpoints(endpoints =>
            {
                // 测试
                endpoints.mapget("/abp/test", async context =>
                {
                    var result = provider.getlanguagesasync();
                    var output = string.join(",", result.result.select(s => s.culturename).toarray());
                    await context.response.writeasync(output);
                });
            });
        }
    }
}

扩展点:在configureservice中提供用户自定义扩展点,完美的是下了解耦。