RegisterTypes and ServiceLocator

Aug 10, 2016 at 3:56 PM
I was trying to use this container with the Microsoft ServiceLocator. When I register the types they are not able to be discovered when I call GetInstance on the ServiceLocator object.

ServiceLocator.Current.GetInstance<IFoo>(); // returns null

I am also not able to register an instance with an interface. I was looking for something like this...


I tried...

RootWorkItem.Items.Add(_fooObject as IFoo) // gives unpredictable results

Is there a way for me to accomplish this or would I need to customize the framework?

I was also are there any other comparable framework for CF. I am introducing this framework to the project team and I would like to compare it against other options. All of the IoC frameworks I have come across are to heavy for CE device applications.
Aug 10, 2016 at 8:21 PM
So I've not used the ServiceLocator is a long time - it was something I put in ages ago, but it doesn't get continues testing.

That said, this library is used a lot on CF apps still. I still use it in everything I do, so it's still used, tested and maintained.

The difference between Items and Services boils down to "do I want a singleton, or multiple instances of a type". Singletons go into the Services. Multiple-instance types go into Items.

So for a singleton, you'd do something like this:

RootWorkItem.Services.AddNew<FooImplementor, IFoo>();
var foo = new FooImplementor();

Then get it back by interface:

var resolve = RootWorkItem.Services.Get<IFoo>();

Now if you want multiple instances, it's best to give them IDs (or the container will just assign them GUIDs):

var foo1 = new FooImplementor();
var foo2 = new FooImplementor();

RootWorkItem.Items.Add(foo1, Constants.Foo1ID);
RootWorkItem.Items.Add(foo2, Constants.Foo2ID);

The retrieve like so:

var resolved = RootWorkItem.Items.Get<IFoo>(Constants.Foo1ID);
Aug 10, 2016 at 8:22 PM
As for other frameworks, I honestly don't know. I created this framework nearly 10 years ago I think and haven't found a need to use anything else. Sorry.
Aug 12, 2016 at 6:16 PM
Thanks for the quick response. That helped. I was able to get it working the way I needed. I have an unrelated questions. Hope you don't me posting it here. Do you know of any unit test mocking frameworks for wince (compact framework)?

Aug 12, 2016 at 8:59 PM
For unit testing, I'd go this route:

There's nothing for mocking that I'm aware of.
Aug 12, 2016 at 10:35 PM
Ok thanks! I will check it out. I haven't been able to find any mocking solutions for CF.