|
|||||
|
|||||
Attributes Overview
NanoContainer.NET AttributesAttributes can also be used instead of a scripts. The con against using the custom Attributes is that your code will be tied directly to NanoContainer because adding Attributes requires that using NanoContainer.Attributes; be added to those classes that should register themselves to the container. This is unlike what is required when writing a script to define your container. Scripts allow you to define the content of a container externally from the Assembly or Assemblies being used. Whether this is an issue or not will be dependent on the application or project being developed. Utilizing NanoContainer Attributes does NOT make the code any less testable. In fact it is possible to define your classes with the custom attributes and then use a script instead without having to remove the attributes. Processing of scripts or Attributes are handled by the implementation ContainerBuilder choosen. If you are looking for something closer to a How-To see an Example Using NanoContainer.NET Attributes. Overview of NanoContainer.NET AttributeRegisterWithContainerAttribute - This is a class level attribute used by concrete classes that need to be registered with the container. The properties of this class are:
PicoParameterAttribute - This is an abstract class level Attribute used to define Parameter which are needed for constructing some objects. Multiple Attributes of this type are allowed.
ConstantParameterAttribute: extends the PicoParameterAttribute abstract class and as it's name implies is used to define a constant to use as a parameter
ComponentParameterAttribute: extends the PicoParameterAttribute abstract class and is used to reference a specific component registered under a particular key value.
RegisterWithContainerAttribute usage examples...Constructor based injection with caching [RegisterWithContainer] public class Foo : IFoo { private IBar bar; public Foo(IBar bar) { this.bar = bar; } } Setter based injection with caching [RegisterWithContainer(DependencyInjection=DependencyInjectionType.Setter)] public class Foo : IFoo { private IBar bar; public IBar Bar { set {this.bar = value; } } } Constructor based injection with non-caching [RegisterWithContainer(ComponentAdapterType.NonCaching)] public class Foo : IFoo { private IBar bar; public Foo(IBar bar) { this.bar = bar; } } Setter based injection with non-caching [RegisterWithContainer(ComponentAdapterType.NonCaching,
DependencyInjection=DependencyInjectionType.Setter)]
public class Foo : IFoo
{
private IBar bar;
public IBar Bar
{
set {this.bar = value; }
}
}
A Custom ComponentAdapter (caching and injection type are ignored for custom) [RegisterWithContainer(ComponentAdapterType.Custom,
ComponentAdapter=typeof(CustomComponentAdapter))]
public class Foo : IFoo
{
private IBar bar;
public Foo(IBar bar)
{
this.bar = bar;
}
}
// example of the Custom ComponentAdapter...
public class CustomComponentAdapter: CachingComponentAdapter
{
public CustomComponentAdapter(Type type) : base(new ConstructorInjectionComponentAdapter(type))
{
}
}
Okay so now it should be clear how to use the custom NanoContainer.NET attributes. But what probably is not clear is how do we get the dynamically constructed PicoContainer from these classes tagged with these Attributes. The next example should look similar, it is almost identical to how we process a Script. The difference is that we need to reference the Assembly we want to search for usages of the RegisterWithContainerAttribute and the fact we are instantiating a AttributeBasedContainerBuilder. Building a PicoContainer from those classes marked with RegisterWithContainerAttribute // Define the assemlies we want to load from StringCollection assemblies = new StringCollection(); assemblies.Add("../FooBar.dll"); // Notice we are using a different type of ContainerBuilderFacade (for supporting Attributes) ContainerBuilderFacade cbf = new AttributeBasedContainerBuilderFacade(); IMutablePicoContainer container = cbf.Build(assemblies); More info on Nano.NET Attributes can be found here. |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||