Translation
When using a dependency injection container, you first register your component with the container, Windsor uses installers (which implements the Iwindsorinstaller interface) to encapsulate and isolate the registered logic, You can use configuration and fromassembly to do your work.
Installers is a simple type that implements the Iwindsorinstaller interface, and there is only one install method that receives the container parameter, which uses the fluent registration API to register the component
public class repositoriesinstaller:iwindsorinstaller{ Public void Install (IWindsorContainer Container, Iconfigurationstore store) {container. Register (alltypes.fromassemblynamed ( " Acme.Crm.Data " ). Where (Type = = type. Name.endswith ( " repository " => c.lifestyle.perwebrequest)); }}
Note: Use a separate installer to register some related service information (such as repositories, controllers)
Installer must be public and contain a public default constructor, Windsor is exposed using installerfactory scanning installers
Using installers
var New WindsorContainer (); container. Install ( new Controllersinstaller (), new Repositoriesinstaller (), // And all your other installers);
When there's a new intallers, remember to register here
Adding this code is a bit tedious if you installers too much. Windsor provides fromassembly static classes and configuration for external configurations
The fromassembly static class scans all classes in the specified assembly that implement the Iwindsorinstaller interface, and if a new installer is added, no additional code is added, and the Windsor automatically detects the type and instantiates
container. Install ( fromassembly.this (), fromassembly.named ("Acme.Crm.Bootstrap" ), fromassembly.containing<ServicesInstaller> (), fromassembly.indirectory ( New Assemblyfilter ("Extensions")), fromassembly.instance ( this. Getpluginassembly ()));
Installer registration does not have a specific order, so you do not know which installer is first registered, if you want to make a special sort, use installerfactory to achieve
Public classWindsorbootstrap:installerfactory { Public OverrideIenumerable<type> Select (ienumerable<type>installertypes) { varretval = Installertypes.orderby (x = This. GetPriority (x)); returnretval; } Private intgetpriority (Type type) {varattribute = type. GetCustomAttributes (typeof(Installerpriorityattribute),false). FirstOrDefault () asInstallerpriorityattribute; returnAttribute! =NULL?attribute. Priority:InstallerPriorityAttribute.DefaultPriority; }}[attributeusage (AttributeTargets.Class)] Public Sealed classInstallerpriorityattribute:attribute { Public Const intDefaultpriority = -; Public intPriority {Get;Private Set; } PublicInstallerpriorityattribute (intPriority ) { This. Priority =Priority ; }}container. Install (Fromassembly.this (NewWindsorbootstrap ()));
To sort installer add attributes
1. Fromassembly.this () the assembly that called the method
fromassembly. Named ("Acme.Crm.Bootstrap")
You can also provide path to a. dll or. exe file when you have the assembly in some non-standard location.
If it is a. dll, use an absolute path
assembly contains a specific type
Configuration 类
Read configuration information for XML files
container. Install ( configuration.fromappconfig (), configuration.fromxmlfile ("settings.xml " ), configuration.fromxml (new Assemblyresource ("assembly://acme.crm.data/ Configuration/services.xml")));
The settings.xml file format is as follows
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <installers> <Installtype= "Windsorinstaller.customerinstaller,windsorinstaller"/> <Installtype= "Windsorinstaller.secondinstaller,windsorinstaller"/> </installers></Configuration>
Windsor automatically gets the installers in the XML file
Can also be written in the application configuration file
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <configsections> < Sectionname= "Castle"type= "Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, castle.windsor" /> </configsections> <Castle> <installers> <Installtype= "Windsorinstaller.customerinstaller,windsorinstaller"/> <Installtype= "Windsorinstaller.secondinstaller,windsorinstaller"/> <!--find the type of all Iwindsorinstaller interfaces under the assembly to register - <!--<install assembly= "Windsorinstaller" ></install> - <!--Finding DLL files - <!--<install directory= "Extensions" filemask= "*.dll" ></install> - </installers> </Castle></Configuration>
Castle Windsor Learning-----Several ways to install installer