With simple injector is there a way to set a property or have a constructor parameter that is not an interface?
My application is a UWP WebVew and most of the services are dependent on it and its state.
I created a wrapper for it so that I could inject it into all of the services like this:
public interface IWebBrowser
{
WebView WebView { get; set; }
}
public class WebBrowser : IWebBrowser
{
public WebView WebView { get; set; }
}
public sealed partial class MainPage : Page
{
public MainPage()
{
InitializeComponent();
var Container = new Container();
Container.Register<IWebBrowser, WebBrowser>(Lifestyle.Singleton);
Container.Register<MainViewModel>();
var web = Container.GetInstance<IWebBrowser>();
web.WebView = WebView;
Vm = Container.GetInstance<MainViewModel>();
}
}
public class MainViewModel : ViewModelBase
{
private readonly web;
public MainViewModel(IWebBrowser web)
{
this.web = web;
}
}
This works but it looks error prone. Is there a way to do this without these two lines:
var web = Container.GetInstance<IWebBrowser>();
web.WebView = WebView;
You could use a RegisterInitializer
which is a little more robust
Registers an
Action<T>
delegate that runs after the creation of instances that implement or derive from the given TService. Please note that only instances that are created by the container (using constructor injection) can be initialized this way
container.RegisterInitializer<IWebBrowser>(x => o.WebView = WebView);
Note : Though i am a little confused at what WebView
is and why it needs to be poked in anyway.