Skip to end of metadata
Go to start of metadata

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 against
  • ComponentAdapterType componentAdapterType: An enum that defines the type of component adapter (choices consist of Caching, NonCaching and Custom). Caching is the default.
  • DependencyInjectionType dependencyInjectionType: An enum that defines the DI construct to follow (Constructor or Setter are the choices) as expected Constructor is 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 Value represent the value of the Constant (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 Value represent the key value of the Component being referenced. This is useful when one or more of a specific type (i.e. string) is registered to the container, the ComponentParameter allows you specify which component the container should use.

RegisterWithContainerAttribute usage examples...

Constructor based injection with caching
Setter based injection with caching
Constructor based injection with non-caching
Setter based injection with non-caching
A Custom ComponentAdapter (caching and injection type are ignored for custom)

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

More info on Nano.NET Attributes can be found here.

Labels
  • None