Question

I have the following setup:

builder.Register<Configuration>(
   c => ConfigurationFile.Load<Configuration>(filename)
).SingleInstance();

Somewhere else in my program I have

void Dispose()
{
    ConfigurationFile.Save(Configuration, @"c:\config.cfg");
}

How do I resolve Configuration here?

Was it helpful?

Solution

Since you Build your Component Container, there is Resolve method which can be used to inject all your dependencies automatically. From the scope defined in your question it is not clear what kind of relations you have between classes and Component Container. But hope this example helps

builder.Register<Configuration>(c => 
        ConfigurationFile.Load<Configuration>(filename)).SingleInstance();
builder.RegisterType<MainWindow>().SingleInstance();
return builder.Build();

Main window:

public class MainWindow
{
    public MainWindow(Configuration configuration)
    {

    }
}

Some bootstrapper or factory or whatever:

public MainWindow GetMainWindow()
{
    container.Resolve<MainWindow>();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top