-
Notifications
You must be signed in to change notification settings - Fork 0
Dependency Injection
DUONG Phu-Hiep edited this page Aug 27, 2024
·
3 revisions
class Toto {
ctor(IServiceProvider s)
}
=> depend directly on the ServiceProvider and use it to resolve the required Dependency
My choice: DryIoc
- Fairly popular
- Active development: lot of commits (total commit, and recent commits)
- All documentation's codes is runnable tests codes => feels very reliable.
Ninject
SimpleInjector
DryIoc
LightInject
StashBox
builder.Register(c => new A { B = c.Resolve<B>() });
Ninject: (15 ans, no new commit since 2 years, 80 issues)
class Samurai
{
[Inject]
public IWeapon Weapon { get; set; }
}
Simpleinjector (15 ans / 2259 commits / 27 issues)
container.RegisterInitializer<HandlerBase>(handlerToInitialize => {
handlerToInitialize.ExecuteAsynchronously = true;
});
Or
class ImportPropertySelectionBehavior : IPropertySelectionBehavior
{
public bool SelectProperty(Type implementationType, PropertyInfo prop) =>
prop.GetCustomAttributes(typeof(ImportAttribute)).Any();
}
container.Options.PropertySelectionBehavior = new ImportPropertySelectionBehavior();
LightInject (13ans / 990 commits / 80 issues)
container.RegisterPropertyDependency<IBar>((factory, propertyInfo) => new Bar());
Dryloc (11 ans/ 4754 commits) doc is unsearchable (cs2md)! Very active development
var ws = c.Resolve<IWidgetService>();
c.InjectPropertiesAndFields(ws);
Or
c.Register<IWidgetService, WidgetService>(Reuse.Singleton,
made: PropertiesAndFields.Of.Name("WidgetRepository"));
or strongly typed:
c.Register<IWidgetService, WidgetService>(
Made.Of(() => new WidgetService { WidgetRepository = Arg.Of<IWidgetRepository>() }),
Reuse.Singleton);
Stashbox good docs, Very active development
options.WithAutoMemberInjection(
Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter)