-
Notifications
You must be signed in to change notification settings - Fork 10
Factory registration
Peter Csajtai edited this page Jul 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();
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());
You have the option to bind a factory delegate to a registration, which will be invoked by the container directly to instantiate your service. This delegate gets the current resolution scope as an argument, so you can use it inside the body of your function.
container.Register<IDrow, Drizzt>(context => context.
WithFactory(resolver =>
new Drizzt(resolver.Resolve<IWeapon>("Twinkle"), resolver.Resolve<IWeapon>("Icingdeath")));
container.Resolve<IDrow>(); //the container will use the given factory to instantiate Drizzt.
- Service registration
- Factory registration
- Assembly registration
- Composition
- Fluent registration api
- Service resolution
- Resolution by attributes
- Conventional resolution
- Delegate resolution
- Conditional resolution
- Multi resolution
- Lifetimes
- Generics
- Generic wrappers
- Decorators
- Resolvers
- Scopes
- Container configuration
- Container diagnostics
- Exceptions