I have a winforms app that dynamically creates sub form popups in runtime based on runtime data. I would like each popup to have it's own scope, which I think that I have achieved. Also, I would like to set a "player profile" resolved by the scope, based on the runtime data. This is where I need help.
Each popup form has a profile data property:
public partial class PlayerForm : Form
{
public IPlayerProfile Profile { get; set; }
}
I open the forms using a registered form generator:
public TForm GetForm<TForm>(ActiveGame gameInfo) where TForm : PlayerForm
{
var scope = new Scope(_container);
var form = scope.GetInstance<TForm>();
form.GameInfo = gameInfo;
// set the profile:
// form.Profile = ...
form.Closed += (s, e) => scope.Dispose();
return form;
}
The scope
doesn't have a GetAllInstances
method. How can I get and set the matching IPlayerProfile
here? The gameInfo
contains the type name to find the appropriate profile.
Relevant container config:
container.Options.DefaultScopedLifestyle = ScopedLifestyle.Flowing;
var registrations = profiles.Select(t => Lifestyle.Scoped.CreateRegistration(t, container));
container.Collection.Register<IPlayerProfile>(registrations);
Calling Container.GetAllInstances<T>()
is equivalent to Container.GetInstance<IEnumerable<T>>()
and this holds with Scope
too that, as you mentioned, misses a GetAllInstances
method. So you can call this:
var profiles = scope.GetInstance<IEnumerable<IPlayerProfile>>();
Or you can resolve any other supported collection type, such as:
var profiles = scope.GetInstance<IReadOnlyCollection<IPlayerProfile>>();