SimpleInjector – Registering when you have a primitive in the constructor

Suppose you have a class that has something like a string parameter in its constructor, like:

public class QueryBus(string busName) : IQueryBus

If you try to register this using SimpleInjector the normal way, like this:

container.Register<IQueryBus, QueryBus>();

you will get an exception when you call container.Verify(), like this:

The constructor of type QueryBus contains parameter 'name' of type String which can not be used for constructor injection.

You can also get other errors, depending on what you are trying to do, and obviously, you can’t register a string (and if you try to do that, or other types of ‘tricks’, you will be told that it is bad design and you are a bad developer and you should give up and go into sales….or something like that).

You can instead register like this:

container.Register<IQueryBus>(() => new QueryBus("MyQueryBus"));

That will work.

posted on Sunday, January 11, 2015 1:47 PM Print
No comments posted yet.

Post Comment

Title *
Name *
Comment *  
Please add 8 and 4 and type the answer here: