banner



How To Register An Interface In Castle.windsor

My preferred IoC container is StructureMap, just I'yard going to be working with a client who uses Castle Windsor as their standard container, so I decided to learn a bit about it this week. I created a simple console application and included some interfaces and implementations to run into how things work. Registering individual 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.              Annals              (Component.                              For                                  <IGreeting>                                            (              )              .                              ImplementedBy                                  <HelloGreeting>                                            (              )              )              ;              container.              Register              (Component.                              For                                  <IWriter>                                            (              )              .                              ImplementedBy                                  <ConsoleWriter>                                            (              )              )              ;                      

Getting resolved types out of the container is as well simple. Greeter requires an IGreeting and an IWriter in its construction:

Resolving Types with Windsor

                                          var                            greeter              =              container.                              Resolve                                  <Greeter>                                            (              )              ;              greeter.Proper noun              =              "Bob the Greeter"              ;              greeter.              Execute              (              "Steve"              )              ;                      

Particularly while you're learning how to work with the container, information technology tin be useful to see a list of everything that is currently registered. I'm not aware of a built-in method that does this direct, just this code will suffice:

Bear witness Contents of Windsor Container

                          foreach              (                              var                            handler              in              container.Kernel              .              GetAssignableHandlers              (              typeof              (                              object                            )              )              )              {              Console.              WriteLine              (              "{0} {1}"              ,              handler.ComponentModel.Services,              handler.ComponentModel.Implementation)              ;              }                      

The one tricky part I ran into is the fact that Castle Windsor does not automatically resolve concrete types, so you have to register them all yourself. Y'all can do this one past ane, like this:

Register 1 Type at a Time

            container.              Register              (Component.                              For                                  <Greeter>                                            (              )              )              ;                      

Simply that doesn't necessarily calibration in a large projection that's going to accept hundreds of classes. Fortunately, you can also register many types at one time using a variety of predicates to filter the list. For case, 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 similar names, east.g. IFoo gets mapped to grade Foo with no code required. In StructureMap this is achieved using .WithDefaultConventions(). Y'all can do the same thing in Castle.Windsor by using .WithServiceDefaultInterfaces(), which seems to go beyond StructureMap in terms of the naming conventions information technology supports.

Here'south an case showing how to register a bunch of types at one time using this feature:

Register Many Types Automatically

            container.              Register              (Classes.              FromThisAssembly              (              )              .              InNamespace              (              "CastleWindsorConsole"              )              .              WithServiceDefaultInterfaces              (              )              )              ;                      

If y'all're familiar with StructureMap, the .WithServiceDefaultInterfaces() call to a higher place is similar to StructureMap's WithDefaultConventions() method. Not that you tin't just say container.Register(Classes.FromThisAssembly()) by itself – you must provide some kind of predicate, even if it's only a Where() that ever returns true.

Y'all tin view and download the code for this CastleWindsorSample application from GitHub.

Steve Smith

Most Ardalis

Software Architect

Steve is an experienced software builder and trainer, focusing on lawmaking quality and Domain-Driven Design with .NET.

How To Register An Interface In Castle.windsor,

Source: https://ardalis.com/getting-started-with-castle-windsor/

Posted by: henrydonve1960.blogspot.com

0 Response to "How To Register An Interface In Castle.windsor"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel