------*%*%##$@$##%*%*----*%*%##$@$##%*%*----*%*%##$@$##%*%*----*%*%##$@$##%*%*------
Thuốc để giải ưu phiền là công việc, thú vui chỉ là liệu thuốc tạm thời..
---------------------------------------------------------------------------------------------------------------
Thứ Hai, 22 tháng 12, 2014
Castle Windsor: How to Register Components
You can register your components in the following ways:
Registering components using Xml configuration, which can be combined with the code options (Read more)
Registering components one-by-one
123456
// Initialize the containervarcontainer=newWindsorContainer();// Register your component with the desired lifestylecontainer.Register(Component.For<IComponent>().ImplementedBy<Component>().LifestylePerThread());
What about open generic types?
12345
// Initialize the containervarcontainer=newWindsorContainer();// Register your component for instance with the default lifestyle = Singletoncontainer.Register(Component.For(typeof(IRepository<>).ImplementedBy(typeof(Repository<>));
How to replace an allready registered component?
In Windsor you can simple register it again, the last registered component will be the one used
12345678910
// Initialize the containervarcontainer=newWindsorContainer();// Register your component for instance with a lifestylecontainer.Register(Component.For(typeof(IRepository<>).ImplementedBy(typeof(Repository<>).LifestylePerWebRequest());// Register a specialized CustomerRepositorycontainer.Register(Component.For<IRepository<Customer>>().ImplementedBy<CustomerRepository>().LifestylePerWebRequest());
How to make one class resolvable by two interface but have them share the same instance?
123456
// Initialize the containervarcontainer=newWindsorContainer();// Register your componentcontainer.Register(Component.For<IRepository<Customer>,ICustomerRepository>().ImplementedBy<CustomerRepository>().LifestylePerWebRequest());
With Castle Windsor the order of the registrations enables this behavior, so the first implementation will be injected into the decorator.
12345678910
// Initialize the containervarcontainer=newWindsorContainer();// Register the default implementationcontainer.Register(Component.For<IRepository<Customer>().ImplementedBy<CustomerRepository>().LifestylePerWebRequest());// Now register the decoratorcontainer.Register(Component.For<IRepository<Customer>().ImplementedBy<LoggingCustomerRepository>().LifestylePerWebRequest());
Registering components by conventions
To do exactly the same but with conventions syntax
1234567
// Initialize the containervarcontainer=newWindsorContainer();// Register all non abstract class inheriting from IRepository with all interfaces// as service, so resolvable by all interfacescontainer.Register(Classes.FromThisAssembly().BasedOn(typeof(IRepository<>)).WithServiceAllInterfaces());
WithServiceAllInterfaces: Means windsor will register the component bound to all it’s interfaces, so if for instance your CustomerRepository implements IRepository but also ICustomerRepository, when you resolve an instance, it will be shared across both contracts for the specified lifetime ( transient means no sharing )
Using installers
Installers provide you a way to group related registrations into one class, to create an installer simply create a class and implement IWindsorInstaller, like this:
Không có nhận xét nào:
Đăng nhận xét