NanoContainer.NET Attributes
Attributes 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 Attribute
RegisterWithContainerAttribute - This is a class level attribute used by concrete classes that need to be registered with the container. The properties of this class are:
object key: which represents the key the component should be registered againstComponentAdapterType componentAdapterType: An enum that defines the type of component adapter (choices consist ofCaching,NonCachingandCustom).Cachingis the default.DependencyInjectionType dependencyInjectionType: An enum that defines the DI construct to follow (ConstructororSetterare the choices) as expectedConstructoris the default type.Type componentAdapter: represents the concrete Type to use as the component adapter. This allows a concrete class to define a custom component adapter to use for itself.
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.
int Index: is needed to define in what order the parameter should be applied.object Value: the usage of this field is specific to the implementation.
ConstantParameterAttribute: extends the PicoParameterAttribute abstract class and as it's name implies is used to define a constant to use as a parameter
object Valuerepresent the value of theConstant(i.e. a string: "hello"; a number: 10; etc...)
ComponentParameterAttribute: extends the PicoParameterAttribute abstract class and is used to reference a specific component registered under a particular key value.
object Valuerepresent the key value of theComponentbeing referenced. This is useful when one or more of a specific type (i.e. string) is registered to the container, theComponentParameterallows you specify which component the container should use.
RegisterWithContainerAttribute usage examples...
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.
More info on Nano.NET Attributes can be found here.
