0

I am just new to C# and trying to debug through a code during my project. At so many places i can see

ServiceContainer.Resolve<MyClass>

I just want to know if it will return the same object every time or it will create a new object of MyClass every time its get called ?.

i have two classes classA and classB. Placed in namespace A and B respectively. Namespace A has dependency on B but i need the object of classA in classB.I can't create the dependency of B in A(Circular dependency). How can i create object of ClassA in ClassB ?. I hope i am clear

Java_Jack
  • 577
  • 1
  • 5
  • 8

4 Answers4

2

What it will return depends on the underlying IoC (inversion of control) configuration. You can configure (most of) IoC containers to return, either the same instance, one instance per Resolve, one instance per thread, one instance per http request, etc.

There are also several ways of specifying how MyClass maps to AnotherImplementationOfMyClass (usually MyClass is an interface).

Usually you'll find the scope of the mapping together with the mapping configuration. Here you can find an example with Ninject

Rui
  • 4,847
  • 3
  • 29
  • 35
2

IoC.Resolve<> is an example of the Service Locator pattern.

Read IoC.Resolve vs Constructor Injection

Community
  • 1
  • 1
Mayank
  • 8,777
  • 4
  • 35
  • 60
2

From the code you pasted it looks like you are using this IOC container

https://github.com/jonathanpeppers/XPlatUtils/wiki/ServiceContainer

From that doc it says "all registrations are singleton" so you will get the same object each time you call it.

Maybe you could write a unit test to test this for yourself?

Loofer
  • 6,841
  • 9
  • 61
  • 102
1

If you are talking about the GitHub Project XplatUtils. Take a look at his Wiki https://github.com/jonathanpeppers/XPlatUtils/wiki/ServiceContainer

ServiceContainer is a simple IoC container, very similar to what you get with Game.Services in XNA or MonoGame.

Registration is explicit and accessed via a static class, all registrations are singleton.

blackmind
  • 1,286
  • 14
  • 27