Castle practice 7-batchregiility facility

Source: Internet
Author: User
Batchregistration facility is an expansion unit that assists with Castle IOC container configuration. We can easily load component and facility in the container. You can compile a separate component or facility into a DLL, and then use the configuration file batchregiility facility" One-time "Or" Filter "And then added to the container without the need to hard code addcomponent and addfacility, the flexibility is very high ~

1) load all components in the Assembly that define Castle. model. castlecomponentattribute.

< Facility ID = "Batchregistration" >
< Assemblybatch Name = "Myassembly" Useattributes = "True"   />
</ Facility >

2) It will not be loaded except component1. Other castlecomponentattribute definitions will be loaded.

<FacilityID= "Batchregistration">
< Assemblybatch Name = "Myassembly" Useattributes = "True" >
< Exclude Type = "Myassembly. component1"   />
</ Assemblybatch >
</Facility>

3) load only component2

< Facility ID = "Batchregistration" >
< Assemblybatch Name = "Myassembly" Useattributes = "False" >
< Include Key = "Other" Component = "Myassembly. component2"   />
</ Assemblybatch >
</ Facility >

4) Load faclity

< Facility ID = "Batchregistration" >
< Addfacility ID = "Facility1" Type = "Myassembly. facility1, myassembly"   />
< Addfacility ID = "Facility2" Type = "Myassembly. facility2, myassembly"   />
</ Facility >

In fact, what this facility does is scan according to the configuration.ProgramSet and then dynamically loaded into the container, although very simple but flexible. For example, if you want to change the behavior of a component, you only need to replace the component DLL, And the other parts do not need to be re-compiled.

[Usage]
1) Create the Facility Configuration

<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< Facilities >
< Facility ID = "Batchregistration" >
< Assemblybatch Name = "Myassembly" Useattributes = "True"   />
</ Facility >
</ Facilities >
</ Configuration >

2) Declare the component class and edit it into the configuration file myassembly. dll.

[Castlecomponent ( " Comp1 " )]
Public   Class Component1
{
Public Component1 ()
{
}
}

[Castlecomponent ("Comp2")]
Public ClassComponent2
{
PublicComponent2 ()
{
}
}

3) Use facility so that component1 and component2 are automatically loaded.

Iwindsorcontainer container =   New Windsorcontainer ( " ../Castleconfig. xml " );
Container. addfacility ( " Batchregistration " , New Batchregistrationfacility ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.