Whilst doing further research into the topic of Dependency Injection and Rhino Mocks I came across a blog entry that gave me a certain amount of food for thought. The article spoke about using the friend assembly directive in C# and how you could effectively hide the injected constructor from all but your unit test assembly. From MSDN:
The friend assemblies feature allows you to access internal members; private types and private members will remain inaccessible.
To give an assembly (assembly B) access to another assembly’s (assembly A’s) internal types and members, use the InternalsVisibleToAttribute attribute in assembly A.
Revisiting the code from the examples used in the previous articles we would need to add the attribute to the class declaration so that the Unit Test assembly “BlogSamples.UnitTest” would be allowed access to the internal constructor:
-
/// <summary>
-
/// Single parameter constructor using dependency injection pattern.
-
/// </summary>
-
/// <param name="i_employeeRepository">An object instance that implements the IEmployeeRepository interface.</param>
-
internal EmployeeManager(IEmployeeRepository i_employeeRepository)
-
{
-
Initialize(i_employeeRepository);
-
}
We would also have to add the InternalsVisibleTo attribute declaration to the AssemblyInfo.cs file:
-
[assembly: InternalsVisibleTo("BlogSamples.UnitTest")]
This would have the effect of allowing the “BlogSamples.UnitTest” assembly access to all internal members of the “BlogSamples.DbConnector” assembly. This can be easily seen when utilizing the intellisense whist within the code of a “BlogSamples.UnitTest” class:

Whilst any other assembly accessing the members of “BlogSamples.DbConnector” would only see the parameter-less constructor:

This would mean that the existing client’s remain un-changed, whilst future development will not be able to use the Constructor Injection design of the EmployeeConnector class as it would be hidden via the internal directive.
