How To Register Interface In Windsor Caastle
My preferred IoC container is StructureMap, simply I'thousand going to be working with a client who uses Castle Windsor every bit their standard container, so I decided to learn a bit nearly it this week. I created a simple console application and included some interfaces and implementations to meet how things work. Registering private interfaces and wiring them up to their implementations is pretty straightforward:
Simple Registration with Castle Windsor
var container = new WindsorContainer ( ) ; // register interfaces and their implementation container. Register (Component. For <IGreeting> ( ) . ImplementedBy <HelloGreeting> ( ) ) ; container. Register (Component. For <IWriter> ( ) . ImplementedBy <ConsoleWriter> ( ) ) ; Getting resolved types out of the container is too simple. Greeter requires an IGreeting and an IWriter in its construction:
Resolving Types with Windsor
var greeter = container. Resolve <Greeter> ( ) ; greeter.Name = "Bob the Greeter" ; greeter. Execute ( "Steve" ) ; Especially while you're learning how to work with the container, information technology can exist useful to see a listing of everything that is currently registered. I'm not aware of a built-in method that does this straight, but this code will suffice:
Show Contents of Windsor Container
foreach ( var handler in container.Kernel . GetAssignableHandlers ( typeof ( object ) ) ) { Console. WriteLine ( "{0} {1}" , handler.ComponentModel.Services, handler.ComponentModel.Implementation) ; } The ane tricky part I ran into is the fact that Castle Windsor does non automatically resolve concrete types, so yous have to annals them all yourself. You tin do this one past i, like this:
Register I Type at a Time
container. Annals (Component. For <Greeter> ( ) ) ; But that doesn't necessarily scale in a big project that'south going to have hundreds of classes. Fortunately, you tin as well register many types at once using a multifariousness of predicates to filter the list. For instance, all classes that inherit from a particular base class, or belong to a certain namespace, or end with a certain string. One of my favorite StructureMap features automatically maps concrete types to interfaces with like names, due east.grand. IFoo gets mapped to class Foo with no code required. In StructureMap this is achieved using .WithDefaultConventions(). You tin do the same thing in Castle.Windsor by using .WithServiceDefaultInterfaces(), which seems to go beyond StructureMap in terms of the naming conventions it supports.
Here'due south an example showing how to register a agglomeration of types at once using this feature:
Register Many Types Automatically
container. Register (Classes. FromThisAssembly ( ) . InNamespace ( "CastleWindsorConsole" ) . WithServiceDefaultInterfaces ( ) ) ; If you're familiar with StructureMap, the .WithServiceDefaultInterfaces() call above is similar to StructureMap's WithDefaultConventions() method. Non that you tin can't just say container.Register(Classes.FromThisAssembly()) by itself – y'all must provide some kind of predicate, even if it'southward only a Where() that always returns true.
You tin view and download the code for this CastleWindsorSample awarding from GitHub.
About Ardalis
Software Architect
Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.
How To Register Interface In Windsor Caastle,
Source: https://ardalis.com/getting-started-with-castle-windsor/
Posted by: johnsonwisay1979.blogspot.com

0 Response to "How To Register Interface In Windsor Caastle"
Post a Comment