Skip to content

Factory registration

Peter Csajtai edited this page May 9, 2019 · 12 revisions

You can register a Func<> delegate as a service factory:

container.RegisterFunc<IDrow>(resolver => new Drizzt(resolver.Resolve<IWeapon>()));

Then you can access the registered factory by requesting a Func<> type:

var factory = container.Resolve<Func<IDrow>>();
var drizzt = factory();

Custom parameters

You can also register factory delegates with custom parameters:

container.RegisterFunc<IWeapon, IWeapon, IDrow>((leftHand, rightHand, resolver) => new Drizzt(leftHand, rightHand));

//then request the factory
var factory = container.Resolve<Func<IWeapon, IWeapon, IDrow>>();
var drizzt = factory(new Icingdeath(), new Twinkle());