Skip to content

Latest commit

 

History

History
54 lines (32 loc) · 1.06 KB

README.md

File metadata and controls

54 lines (32 loc) · 1.06 KB

SharpInjector

Micro DI for netstandard

How to Use

Install the SharpInjector package via Nuget:

Install-Package SharpInjector

Registering a singleton

ServiceLocator.Default.Register<IFoo>(new Foo());

Registering a transient

ServiceLocator.Default.Register<IFoo>(() => new Foo());

Concrete classes

You don't need to register concrete classes, they will be instanciated as transient if not found.

var foo = ServiceLocator.Default.Resolve<Foo>();

Overriding registered types

If you try to register a type twice, an exception is thrown. If you don't want this behavior but a registration replacement instead, just set the last paramenter to true:

ServiceLocator.Default.Register<IFoo>(new Foo());
ServiceLocator.Default.Register<IFoo>(new Foo2()); //throws DuplicateRegistrationException

But...

ServiceLocator.Default.Register<IFoo>(new Foo());
ServiceLocator.Default.Register<IFoo>(new Foo2(), true); //replace the registration

Enjoy!