Skip to content

Service Location

Xet Erixon edited this page Aug 17, 2017 · 4 revisions

Service location examples

This service locator works more or less (probably less) the same as any other one. You register an object or interface somewhere in your code (usually in some bootstrap section of your app) and resolve the instance somewhere else when you need it.

Register an interface with an implementing class

NotNet.Core.Container.Default.RegisterTransient<ITest,Test>();

Resolve it...

var test = NotNet.Core.Container.Default.Resolve<ITest>();

Register a class as a singleton

NotNet.Core.Container.Default.RegisterSingleton<Test>();

Resolve it...

var test = NotNet.Core.Container.Resolve<Test>();

Resolve it again and it will be the same instance.

It is possible to to register and resolve multiple implementations for one interface. Resolve them with ResolveAll<T>(). Using Resolve<T>() for an interface with multiple implementations will resolve the implementation that was registered first.

Clone this wiki locally