Array Injection Using Unity Application Block v1.2

One of the new features in Unity is the injection of arrays. The Unity Documentation is pretty good on showing how to do this, but here is a more complete sample for those interested in using Unity to inject arrays.

Injecting Array of ILogFilter into ConsoleLogger using Unity

Similar to the Enterprise LibraryLogging Application Block, let’s pretend we have a custom ILogger Service, called ConsoleLogger, that accepts an array of ILogFilter into its constructor. The Console Logger will only log a message if it passes all the filters.

publicinterfaceILogger

{

void Log(LogMessage message);

}

publicclassConsoleLogger : ILogger

{

privatereadonlyILogFilter[] _filters;

public ConsoleLogger(ILogFilter[] filters)

{

_filters = filters;

}

publicvoid Log(LogMessage message)

{

foreach(var filter in _filters)

if (!filter.CanLog(message))

return;

Console.WriteLine(message.Message);

}

}

publicclassLogMessage

{

publicstring Category { get; set; }

publicint Priority { get; set; }

publicstring Message { get; set; }

}

We have two custom ILogFilter Classes, CategoryFilter and PriorityFilter, that can be used to filter messages based on the category and priority, respectively.

publicinterfaceILogFilter

{

bool CanLog(LogMessage message);

}

publicclassCategoryFilter : ILogFilter

{

privatereadonlystring[] _categories;

///<summary>

/// Filters log messages by category.

///</summary>

///<param name=”categories”>The categories to log.</param>

public CategoryFilter(string[] categories)

{

_categories = categories;

}

publicbool CanLog(LogMessage message)

{

foreach (var category in _categories)

if (message.Category.Equals(category, StringComparison.InvariantCultureIgnoreCase))

returntrue;

returnfalse;

}

}

publicclassPriorityFilter : ILogFilter

{

privatereadonlyint _lowerBound;

privatereadonlyint _upperBound;

///<summary>

/// Filters log messages by priority.

///</summary>

///<param name=”lowerBound”>The minimum priority that will be logged.</param>

///<param name=”upperbound”>The maximum priority that will be logged.</param>

Now that we have the infrastructure in place we need to configure our UnityContainer so that ConsoleLogger is our default ILogger and that any filters of our choosing get properly injected into ConsoleLogger at runtime.

Setting Up UnityContainer

We can configure our UnityContainer via configuration or using the Unity API at runtime. In this case I choose to configure Unity at runtime using its API.

First, let’s register both a CategoryFilter and PriorityFilter with the UnityContainer for injection into the ConsoleLogger. In this case, I am registering an instance that I will create myself.

You don’t have to use RegisterInstance. You could just as easily read this from a configuration file or use RegisterType. In this case I am specifying that only LogMessages with a Category of “Security” and a priority of 3 to 5 inclusive will be logged.

If you had multiple constructors and wanted to be more specific about using the constructor with 1 parameter accepting an array of ILogFilters, you can use InjectionConstructor with ResolvedParameter as such: