Posts tagged Dependency Injection
Too Much of a Good Thing? Constructor Injection Overload
Jan 21st
This week there have been two posts covering dependency inversion and going too far with constructor injection. In the first post Robert C Martin (Uncle Bob) cautions against littering your code with calls to your IoC container.
I think these frameworks are great tools. But I also think you should carefully restrict how and where you use them.
The post goes on to discuss how to keep the code which builds dependencies contained in one place so that you are not littering your code with calls to the container framework or coupling yourself to other classes using new() statements.
The second post by Jeffrey Palmero takes this a little further and is the one that really got me thinking. Palmero sets up an example where a class requires two interface dependencies in its constructor. However, in the code, both dependencies are not always used. This was a key thing to note, because Palmero’s point was,
The anti-pattern is that one of these constructor arguments is really not a class dependency.
In his example, one of the interface dependencies required in the constructor is only used in some cases. This was a huge “duh” for me because I know there are several times when I have required everything the class might use in the constructor.
I think the key here is balance. Require the real dependencies up front, but also allow for other dependencies to be resolved by blending the use of a container with a factory type pattern. Keep all the dirty details of your dependent objects and how they are resolved hidden and encapsulated in one place. This would allow you to switch containers or stop using one all together.
** Update **
A couple readers have posted comments and I would love to hear more. Be sure to give them a read!
Using a Container To Make a Better Factory
Jan 13th
Lately I have been delegating a lot to containers. If you consider what containers can provide, there are a lot of ways you can leverage them. Let’s consider the case where you have a Factory implemented using a traditional Factory Pattern to create concrete instances of an abstract type like an interface or base class.
The most common use of the factory pattern involves a switch statement where you say something like “Switch key Case A return new TypeA()”. Factories are a great way to defer object creation but part of the problem with a factory is that it is very much coupled to the objects it creates and the values used to determine which object to create. This can be considered as acceptable since you know the factory has a dirty job. However, let’s consider how we can eliminate the need for switch statements in a factory and let the container do the work.
Most containers support adding objects with a name or key; commonly referred to as a named instance. This behavior is what we will leverage to make a “better factory.”
Consider this traditional factory (Figure 1)
public class WidgetRepositoryFactory { public IWidgetRepository Create(string repositoryKey) { switch (repositoryKey.ToLower()) { case "xml" : return new WidgetXmlRepository(); case "csv": return new WidgetFileRepository(); default : throw new ArgumentException("Invalid repositoryKey"); } } }
Nothing fancy. Our traditional factory can provide either an Xml Repository or File Repository. The repositoryKey could come from a configuration value or even a parameter to some type of request or user input.
Now let’s take a look at a “better factory”. This example will use StructureMap as the container of choice.
Note: What this example doesn’t illustrate is how our container bootstraps the application. To focus on the better factory concept just assume a valid container is populated and provided to the factory when needed.
Consider our better factory (Figure 2)
public class WidgetRepositoryBetterFactory { private readonly IContainerProvider _containerProvider; public WidgetRepositoryBetterFactory(IContainerProvider containerProvider) { _containerProvider = containerProvider; } public IWidgetRepository Create(string repositoryKey) { return (IWidgetRepository) _containerProvider.Resolve <IWidgetRepository>(repositoryKey); } }
Within the Create method we are simply using our container to resolve an instance of the IWidgetRepository by it’s key. This is a bare bones implementation and is not providing any sanity check on the arguments. There are a couple ways you could go about that, so I’ll leave it up to your imagination.
The “better factory” is wired up to take an instance of an IContainerProvider which looks like the following:
public interface IContainerProvider { object Resolve<T>(); object Resolve<T>(string key); }
We can wire this up in Structure Map like so:
public StructureMapProvider() { _container = new Container(); _container.Configure(x => { x.ForRequestedType<IContainerProvider>() .TheDefault.IsThis(this); x.ForRequestedType<IWidgetRepository>().AddInstances (i => i.OfConcreteType<WidgetFileRepository>() .WithName("csv")); x.ForRequestedType<IWidgetRepository>().AddInstances (i => i.OfConcreteType<WidgetXmlRepository>() .WithName("xml")); }); }
When someone calls our StructureMapProvider’s Resolve<T> method for IContainerProvider, they will be given that instance of the StructureMapProvider. This can be a little confusing at first, but in cases where you do not want your container to be created multiple times, this allows you to to set up your container to support that.
The important lines to review in the StructureMap configuration are these two:
x.ForRequestedType<IWidgetRepository>().AddInstances (i => i.OfConcreteType<WidgetFileRepository>().WithName("csv")); x.ForRequestedType<IWidgetRepository>().AddInstances (i => i.OfConcreteType<WidgetXmlRepository>().WithName("xml"));
As of StructureMap 2.5.2 this is how you can register named instances within the Configure expression. What I think is a little misleading is that this sounds as though you are creating this as a Singleton by saying “Add an instance of this type, with this name.” In my testing I found that the instance returned when resolving one of these types is a new instance. The grammar of the API can be a bit tricky if you are not fully-versed in it.
Containers certainly offer a lot of power and you can delegate a lot to a container. Hopefully next time you need to leverage a factory you can consider how to make it a “better factory”™. Enjoy!
Inverting Control in WCF – Follow Up
Nov 23rd
The code from “Inverting Control in WCF” has been posted to Codeplex here. Feel free to ping me with questions on Twitter or through this blog!
Thanks to all who attended and a big thanks to Alvin Ashcraft for hosting and to all the other organizers & sponsors!
WCF InstanceContextMode.Single, Default Constructors, and Unity
Apr 25th
During a refactoring of a WCF solution I was converting all the various services in the solution to use dependency injection with constructors. One of the services was configured as a singleton using the ServiceBehavior InstanceContextMode.Single. This particular service controls a resource that is best left for a singleton because it has an open socket listener that receives responses from our mainframe systems. The service was also configured for transactions and has the ReleaseServiceInstanceOnTransactionComplete explicitly set to false.
When the other developer on the project fired up the test harness for the services he was receiving the message “The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host. “. After testing a few theories I noticed that the service was marked up with the InstanceContextMode behavior. I removed the attribute and tested the service and it worked just fine which lead me to draw a conclusion about how WCF handles the InstanceContextMode attribute.
Because you are telling WCF to manage that service instances lifetime it makes sense that it would require a default constructor because WCF is going to be instantiating and disposing of the service instance. When I refactored the service with no default constructor that was a breaking change for the WCF infrastructure. So the question remains, can Unity handle a singleton service instance?
To test Unity with a Singleton service I followed the steps in my previous article on Unity and WCF and created an IInstanceProvider, IServiceBehavior, and a custom ServiceHost. The results were what I expected. Each call to the service was handled by the singleton instance of the service whose lifetime was being managed by the Unity container.
using System; namespace Service { //[System.ServiceModel.ServiceBehavior(InstanceContextMode=System.ServiceModel.InstanceContextMode.Single)] public class PersonService : Contracts.IPersonService { private Guid _instanceId; /// <summary> /// Initializes a new instance of the PersonService class. /// </summary> public PersonService() { _instanceId = Guid.NewGuid(); } #region IPersonService Members public Contracts.PersonResponse SavePersonRecord(Contracts.PersonRequest request) { return new Contracts.PersonResponse(_instanceId.ToString(), String.Format("Added Person - {0}", request.Person.Name)); } public Contracts.PersonResponse DeletePersonRecord(Contracts.PersonRequest request) { return new Contracts.PersonResponse(_instanceId.ToString(), String.Format("Deleted Person - {0}", request.Person.Name)); } #endregion } }
The client calls the SavePersonRecord method then subsequently the delete person method. As you can see the Instance Id returned by the service is the same for both calls. Removing the lifetime element from the Unity configuration results in two different instance Id’s returning from the service because the default behavior for WCF is to create a new instance per call to the service.
I ran one final test and that was to remove the default constructor and require the Instance Id to be passed to the constructor:
using System; namespace Service { //[System.ServiceModel.ServiceBehavior(InstanceContextMode=System.ServiceModel.InstanceContextMode.Single)] public class PersonService : Contracts.IPersonService { private Guid _instanceId; /// <summary> /// Initializes a new instance of the PersonService class. /// </summary> /// <param name="instanceId"></param> public PersonService(string instanceId) { _instanceId = new Guid(instanceId); } #region IPersonService Members public Contracts.PersonResponse SavePersonRecord(Contracts.PersonRequest request) { return new Contracts.PersonResponse(_instanceId.ToString(), String.Format("Added Person - {0}", request.Person.Name)); } public Contracts.PersonResponse DeletePersonRecord(Contracts.PersonRequest request) { return new Contracts.PersonResponse(_instanceId.ToString(), String.Format("Deleted Person - {0}", request.Person.Name)); } #endregion } }
The Unity configuration now looks like so:
<unity> <typeAliases> <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" /> <typeAlias alias="IPersonService" type="Contracts.IPersonService, Contracts" /> </typeAliases> <containers> <container> <types> <type type="IPersonService" mapTo="Service.PersonService, Service"> <lifetime type="singleton" /> <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"> <constructor> <param name="instanceId" parameterType="System.String, mscorlib"> <value value="2423f423-a4da-4306-9ba1-452214c517d6"/> </param> </constructor> </typeConfig> </type> </types> </container> </containers> </unity>
Hopefully it is no surprise that the results of calling the service are the same as the screenshot posted above.
I still need to test this with transactions but I believe using the right set of options will prevent WCF from disposing your singleton and allow your custom Unity service host to handle that.
Dependency Injection: Modularize Your Services and Applications
Dec 23rd
There are many different patterns for objected oriented development that have emerged over the years. More often than not I will stumble across one that we are inadvertently using in our services or applications at REJIS. Most recently I wrote about an approach we use when we need a service client that will take data from a service and transform or adapt it to another format (database, xml model, fixed length file, etc). At the core of that pattern is dependency injection.
Martin Fowler has an excellent (and lengthy) discussion on Inversion of Control and Dependency Injection which is well worth the read. Wikipedia defines dependency injection as "…the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency."
For example lets say that you have an interface, IMyInterface, which Class A depends on for some functionality. Class B and C both implement IMyInterface making them substitutable for Class A’s dependency. At runtime, through configuration or programmatically, you can decide which concrete class (B or C) for Class A to use. Rather than having Class A directly depend on Class B or C you reduce coupling by relying on the interface and then you inject this dependency at runtime.
Interfaces represent one of the best possible abstractions available to an object oriented programmer. The benefits to using interfaces and dependency injection are numerous:
- By using an interface you have defined a contract of expected behavior for implementers.
- Interfaces reduce coupling, allowing for independent unit testing, and multiple implementations of the interface.
- Code becomes simplified and focused by reducing branching logic and overall lines of code.
- Changes to the application can potentially be applied at runtime (if this is a web app, windows apps would require closing and reopening) and would not require a recompile of the entire application (if the new implementing class is in another assembly only that assembly needs to be compiled).
- The application will be more "plug and play" and allow for adding or removing functionality as needed.
MSDN has an article that discusses Dependency Injection in depth and how it relates to a few other Creational patterns such as factory. There are a number of Inversion of Control (IOC) frameworks that assist with dependency injection that use the notion of containers which help manage the lifecycle of objects and apply configuration settings to modify behaviors at runtime. For a list of open source IOC frameworks check out this post by Scott Hanselman.
Dependency Injection Example – Supporting Multiple Authentication Methods
I have created a sample project which I will use to illustrate the flexibility of this approach. The sample covers a very common scenario where we must support multiple authentication methods to allow flexibility for our customers. We’ll look at a traditional approach and then an updated approach that will use Dependency Injection.
At the core of this example is the IAuthenticator Interface and the User Class. Both are very simple for examples sake.
Public Interface IAuthenticator Function Authenticate(ByVal userName As String, _ ByVal password As String) As User End Interface
Public Class User Public Name As String Public AuthenticationType As String Public IsAuthenticated As Boolean End Class
The customer requested that the application first be built to support Database Authentication. Because our architect was thinking ahead and at least forced us to use an interface we will implement that interface in our DatabaseAuthenticator class.
Public Class DatabaseAuthenticator Implements IAuthenticator Public Function Authenticate(ByVal userName As String, _ ByVal password As String) As User Implements IAuthenticator.Authenticate Dim user As New User user.AuthenticationType = "DatabaseAuthentication" 'Simulate DB authentication If userName = "Test" AndAlso password = "Testing!" Then user.IsAuthenticated = True user.Name = userName 'else 'could throw security exception or any other action here. End If Return user End Function End Class
To test our authentication we’ll go "old school" with a little console program action. The console program will display a username prompt and password prompt then it will invoke our IAuthenticatorInstance to perform the authentication.
Let’s first look at life without dependency injection:
Module Module2 Sub Main() Dim authenticator As Authenticator.IAuthenticator Select Case My.Settings.AuthenticationType Case "Database" authenticator = New Authenticator.DatabaseAuthenticator Case Else Throw New ArgumentException("Invalid AuthenticationType!") End Select Dim userName As String Dim password As String Dim user As Authenticator.User Console.WriteLine("Username: ") userName = Console.ReadLine Console.WriteLine("Password: ") password = Console.ReadLine Console.Clear() user = authenticator.Authenticate(userName, password) If user.IsAuthenticated Then Console.WriteLine(String.Format("User authenticated via {0}!", _ user.AuthenticationType)) Else Console.WriteLine("User authentication failed!") End If Console.ReadLine() End Sub End Module
In order for Module2 to work we’ve had to hard-code a string value in our case statement to help us decide the type we want to create. From there we instantiate the appropriate class manually. This works fine; it will run and there are no problems with it…until you need a new authentication method. Then you must go back and add a new class that implements IAuthenticator, add a branch to the Case statement and then instantiate your new class. Of course you could do this without interfaces as well…but I digress.
On to life with dependency injection:
Module Module1 Sub Main() Dim authenticatorType As System.Type = _ Type.GetType(My.Settings.AuthenticatorType) Dim authenticator As Authenticator.IAuthenticator = _ Activator.CreateInstance(authenticatorType) Dim userName As String Dim password As String Dim user As Authenticator.User Console.WriteLine("Username: ") userName = Console.ReadLine Console.WriteLine("Password: ") password = Console.ReadLine Console.Clear() user = authenticator.Authenticate(userName, password) If user.IsAuthenticated Then Console.WriteLine(String.Format("User authenticated via {0}!", _ user.AuthenticationType)) Else Console.WriteLine("User authentication failed!") End If Console.ReadLine() End Sub End Module
You probably noticed the first line where we dim up the authenticator variable by calling Activator.CreateInstance and passing it a type variable which I resolved using the Type.GetType method. Type.GetType(string) returns the System.Type which we can then pass to activator to get our instance created.
Our settings file contains this section:
<applicationSettings> <DepdencyInjectionConsole.My.MySettings> <setting name="AuthenticatorType" serializeAs="String"> <value>Authenticator.DatabaseAuthenticator, Authenticator</value> </setting> </DepdencyInjectionConsole.My.MySettings> </applicationSettings>
The notation I used for the type information is the standard "Class, Assembly". Note that I am not using strong names so it is not necessary to include version and public key information. Also note that none of the code in Module 1 references the concrete class DatabaseAuthenticator.
You may have also noticed that, including whitespace, Module1 has 29 lines versus Module2’s 36 lines of code (excluding line continuations). In our simple example 7 lines isn’t much but in complex systems that factor could be easily multiplied by 10 or more.
The code that was written for Module1 has no dependencies other than it has to know about Authenticator.IAuthenticator. The true dependency is injected at runtime. If we try to use a type in the AuthenticatorType setting that the system cannot resolve then our program will blow up.
Which leads us to the paradigm shift; you have to be aware of your operating environment and ensure that the assemblies you need are available in the GAC or in your applications executing or bin folder (for web apps and IIS hosted services). This does not negate managing your dependent assemblies but it shifts it from something necessary throughout development to something that could potentially only happen during deployment. Because the code only depends on the Interface IAuthenticator two completely different teams could be coding the main module and an IAuthenticator implementation. Development for Module2 is more susceptible to breaking changes because of direct dependencies on the classes that implement IAuthenticator.
Adding Another Authenticator Implementation
The customer you wrote this system for now wants to add authentication that will read an XML document for user accounts. We create a new assembly called CustomAuthenticator and our new class looks like this:
Public Class XmlAuthenticator Implements Authenticator.IAuthenticator Private Shared authenticationStore As Xml.Linq.XDocument Sub New() authenticationStore = _ Xml.Linq.XDocument.Load("C:\Temp\AuthenticationStore.xml") End Sub Public Function Authenticate(ByVal userName As String, _ ByVal password As String) As Authenticator.User _ Implements Authenticator.IAuthenticator.Authenticate Dim user As New Authenticator.User Dim userQuery = From u In authenticationStore...<users>...<user> _ Where u...<name>.Value = userName _ And u...<password>.Value = password _ Select u user.AuthenticationType = "XmlAuthentication" user.IsAuthenticated = userQuery.Count = 1 If user.IsAuthenticated Then user.Name = userName End If Return user End Function End Class
The XmlAuthenticator uses an XML file to store user accounts and loads in its constructor. Using LINQ we query the XML document to find if our user exists. This class resides in a completely separate assembly.
If you are adept at reading LINQ XML Queries you can guess the documents structure:
<?xml version="1.0" encoding="utf-8" ?> <users> <user> <name>Test</name> <password>Testing!</password> </user> </users>
Adding XmlAuthenticator to Module2 (Anti-Dependency Injection Module)
In order to implement this in Module2 we need to add a branch to our case statement:
Case "XML" authenticator = New CustomAuthenticator.XmlAuthenticator
Total cost – two lines of code, another hard-coded string, someone has to update the config file and deploy the new assembly.
Adding XmlAuthenticator to Module1 (Pro-Dependency Injection Module)
In order to implement this in Module1 we need to…do nothing. Not programmatically anyhow, but we do need to update our AuthenticatorTypeValue in the app.config.
Here’s the new config:
<applicationSettings> <DepdencyInjectionConsole.My.MySettings> <setting name="AuthenticatorType" serializeAs="String"> <value>CustomAuthenticator.XmlAuthenticator, CustomAuthenticator</value> </setting> </DepdencyInjectionConsole.My.MySettings> </applicationSettings>
Total cost – zero lines of code and someone has to update a config file and deploy the new assembly. Less work and less code to maintain. Ladies and gentlemen I think we have a winner here!
This is a very simple example that hopefully illustrated what you can achieve using dependency injection. The code is simply for illustration and I do not recommend or condone anyone using for their authentication components (nor will I be liable for anything you do with this)!
What are some real world ways we use this?
For service clients where we are responsible for retrieving data from a service and doing something with it we use dependency injection for what we our "adapters". The adapters take a known type from a service and do whatever is needed; store data in a database, flat file, or another XML format.
For a service we are developing on a statewide data sharing project dependency injection is being used to allow for a custom implementation of activities within that service so that the service can be deployed to another geographical region in the state and customized via configuration. The major functionality points were identified and abstracted with interfaces so the functionality could be implemented in different ways for the two regions. This also allows a separate code base that the other region can maintain and update as needed. The core functionality is completely similar between each region and should require no modification except any bugs that may surface.
We will also use this approach on another service to allow a custom "path" for a document that we will receive from an external source. We have cases where, depending on what external partner sends us information, we need to do multiple things with it and this can vary from partner to partner. This last example may require a more sophisticated method so I am leaning towards the Unity Framework from Microsoft as we already use Enterprise Library extensively in our services.
Another Tool in the Toolbox
The best way to look at all of these object-oriented approaches is that they are tools in your toolbox. I will likely not use dependency injection everywhere. But in the work I do with services we need this pattern more often than not. Hopefully this gives you enough insight to decide if you may be able to benefit from using it in your own applications.

