ASP.NET Boilerplate provides an infrastructure and a model to configure it and modules on startup.
ASP.NET提供了一個基礎和樣板模型配置和模塊啟動。
Configuring ASP.NET Boilerplate
Configuring ASP.NET Boilerplate is made on PreInitialize event of your module. Example configuration:
配置ASP.NET樣板在模塊的PreInitialize 事件里。配置示例:
public class SimpleTaskSystemModule : AbpModule { public override void PreInitialize() { //Add languages for your application Configuration.Localization.Languages.Add(new LanguageInfo("en", "English", "famfamfam-flag-england", true)); Configuration.Localization.Languages.Add(new LanguageInfo("tr", "Türkçe", "famfamfam-flag-tr")); //Add a localization source Configuration.Localization.Sources.Add( new XmlLocalizationSource( "SimpleTaskSystem", HttpContext.Current.Server.MapPath("~/Localization/SimpleTaskSystem") ) ); //Configure navigation/menu Configuration.Navigation.Providers.Add<SimpleTaskSystemNavigationProvider>(); } public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); } }
ASP.NET Boilerplate is designed modularity in mind. Different modules can configure ASP.NET Boilerplate. For example, different modules can add navigation provider to add their own menu items to the main menu. (Seelocalization and navigation documents for details on configuring them).
ASP.NET樣板設計模塊化的思想。不同的模塊可以配置ASP.NET樣板。例如,不同的模塊可以添加導航提供程序,將自己的菜單項添加到主菜單中。(在它們配置細節seelocalization導航文件)。
Replacing Built-In Services(替換內置服務)
Configuration.ReplaceService method can be used to override a built-in service. For example, you can replace IAbpSession service with your custom implementation as shown below:
configuration.replaceservice方法可用於重寫內置服務。例如,您可以與您的自定義實現替代iabpsession服務如下圖所示:
Configuration.ReplaceService<IAbpSession, MySession>(DependencyLifeStyle.Transient);
ReplaceService method has an overload to pass an action to make replacement in a custom way (you can directly use Castle Windsor for advanced registration API).
replaceservice方法具有過載通過一個自定義的方式做替代動作(可以直接使用Castle Windsor 高級注冊API)。
Same service can be replaced multiple times (especially, in different modules). Last replaced will be valid (As you know, module PreInitialize methods are executed by dependency order).
同樣的服務可以多次更換(特別是在不同的模塊中)。最后更換將是有效的(你知道,模塊分發方法是通過依賴順序執行)。
Configuring Modules(配置模塊)
Beside framework's own startup configuration, a module can extend IAbpModuleConfigurations interface to provide configuration points for the module. Example:
在框架的啟動配置,一個模塊可以擴展iabpmoduleconfigurations接口提供的模塊配置點。例子:
... using Abp.Web.Configuration; ... public override void PreInitialize() { Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true; } ...
In this example, we configured AbpWebCommon module to send all exceptions to clients.
在這個例子中,我們配置abpwebcommon模塊發送給客戶的所有異常。
Not every module should define this type of configuration. It's generally needed when a module will be re-usable in different applications and needs to be configured on startup.
不是每個模塊都應該定義這種類型的配置。當一個模塊在不同的應用程序中重新使用,並且需要在啟動時進行配置時,這通常是必需的。
Creating Configuration For a Module(創建一個模塊配置)
Assume that we have a module named MyModule and it has some configuration properties. First, we create a class for these cofigurable properties:
假設我們有一個模塊叫MyModule,它有一些配置屬性。首先,我們創建一個可配置特性這類:
public class MyModuleConfig { public bool SampleConfig1 { get; set; } public string SampleConfig2 { get; set; } }
Then we register this class to Dependency Injection on PreInitialize event of MyModule (Thus, it will be injectable):
然后我們登記這類依賴注入在起始事件mymodule(因此,它將注射):
IocManager.Register<MyModuleConfig>();
It should be registered as Singleton as in this sample. Now, we can use the following code to configure MyModule in our module's PreInitialize method:
它應該像本示例一樣注冊為單例。現在,我們可以使用下面的代碼在我們的模塊的配置MyModule的起始方法:
Configuration.Get<MyModuleConfig>().SampleConfig1 = false;
While we can use IAbpStartupConfiguration.Get method as shown below, we can create an extension method to IModuleConfigurations like that:
我們可以用iabpstartupconfiguration。獲取方法如下圖所示,我們可以創造imoduleconfigurations這樣的擴展方法:
public static class MyModuleConfigurationExtensions { public static MyModuleConfig MyModule(this IModuleConfigurations moduleConfigurations) { return moduleConfigurations.AbpConfiguration.Get<MyModuleConfig>(); } }
Now, other modules can configure this module using the extension method:
現在,其他模塊可以使用擴展方法配置這個模塊:
Configuration.Modules.MyModule().SampleConfig1 = false; Configuration.Modules.MyModule().SampleConfig2 = "test";
This makes easy to investigate module configurations and collect them in a single place (Configuration.Modules...). ABP itself defines extension methods for it's own module configurations.
At some point, MyModule needs to this configuration. You can inject MyModuleConfig and use configured values. Example:
這便於調查模塊配置,並在單個位置收集它們(配置……)。ABP本身為其自己的模塊配置定義了擴展方法。
在某一點上,MyModule需要這樣的配置。你可以把MyModuleConfig和使用配置值。例子:
public class MyService : ITransientDependency { private readonly MyModuleConfig _configuration; public MyService(MyModuleConfig configuration) { _configuration = configuration; } public void DoIt() { if (_configuration.SampleConfig2 == "test") { //... } } }
Thus, modules can create central configuration points in ASP.NET Boilerplate system.
因此,模塊可以在ASP.NET系統中心配置點創建樣板文件。