Using structuremap and c# 4.0 basically what i have is this:
interface IBoard
{
void Setup();
}
class Board : IBoard
{
IConfig _config;
Board(IConfig config)
{
_config = config;
}
void Setup()
{
//use the _config object here
}
}
class Game
{
IBoard _board;
Game(IBoard board)
{
_board = board;
}
}
partial class Form1
{
Form1()
{
InitializeForm();
}
//in here we need to do some work to setup the IConfig object
//via form controls
}
partial class Form2
{
Game _game;
Form1(Game game)
{
InitializeForm();
_game = game;
}
}
now under normal usage id just say
For<Type>().Use<Class>()
or thereabouts for all my dependancies. However what i am after is what is the best pattern to use to set the values of the config object in form1 and then call form2 with the config values set in memory and maintained throughout the app? i thought of using a singleton however the singleton should be imuttable or at least be statically created and not accept a parameter based config… so what to do? i currently create the form1 in the Program of the winform start up via
ObjectFactory.Get<Form1>();
I don’t think that the IConfig is a good fit for creating using the container since you don’t know the parameter values until it’s time to instantiate it. I think you have to supply the configuration instance to the code that calls the container in order to fetch the form.
You can supply arguments to the container using the With method:
You want to minimize calls to the container, preferably just one place in you application that wire up the rest of it during bootstrapping. An alternative is to register a Func, resolve that during bootstrap and use that to create an instance of the Form2.
Registration:
Usage: