This project is read-only.

Register an Interface wich implementation's constructor has arguments

May 17, 2013 at 2:51 AM
Well I read the documentation but I couldn't find how to do this.
I've for example a class called "DocumentViewModel" wich constructor is something like:
Public Sub New(ByVal filepath As String)
    _filepath = New FilePath(filepath)
    //more code...
End Sub
How should I register this? And how should I implement this?

Thanks in advance
May 17, 2013 at 9:37 AM
View Models are created by your presentation layer (most often your controller, presenter, page, or whatever your pattern you're using) and are filled with data from the model and possibly other sources. Since this View Model class (this data package) is created using (almost) all run-time values, in shouldn't delegate the creation to your DI container. Further more, in general you're not interested to abstract the View Model from your View or Controller, because it is simply a data package.

If you still try this, you will find out very soon that this gets really awkward, and this holds not only when working with Simple Injector, but for all DI frameworks.

But when we ignore the fact that you're dealing with a view model, you can register types with a string parameter as follows (excuse my C#):
container.Register<ISomeService>(() => new SomeServiceImpl("somePath"));
And when you need to supply a runtime value, you can register a factory or register a Func<T> delegate as follows:
container.RegisterSingle<Func<string, ISomeService>>(
    path => new SomeServiceImpl(path));
Marked as answer by dot_NET_Junkie on 1/1/2014 at 9:02 PM
May 19, 2013 at 8:00 AM
Thank you for your answer. I'll try to avoid delegating the creation to the DI container.