Unregister Type with Service Locator or Change lifeTime Status

Topics: Feature requests
Aug 21, 2012 at 8:00 PM


First thank you for the great product and the hard work for maintaining and expanding it.

I'm doing my first steps with Catel and started with writing some unit tests.

So I registered a class as a singleton:

ServiceLocator.Instance.RegisterType<IMyClass, MyClass>();

and confirmed that ResolveType<>() returns a reference to a singleton.

After that I decided to register the same class again, but this time as a transient object.

ServiceLocator.Instance.RegisterType<IMyClass, MyClass>(true, false);

I found that the SreviceLocator does'n preregister my class even with:

 "registerIfAlreadyRegistered = true" parameter.

It sounds to me that "registerIfAlreadyRegistered  = true" tells the container to override the previous instance, but it's not working like that.

A'm asking if there is a way to change lifetime status of already registered type (probably a bad idea since an instance of the class may be already bound) or

is there a way to unregister already registered type.


My questions are academical only since I don't need it in a production code. 

Aug 24, 2012 at 10:22 AM

I think this is not possible, but I cannot see a real world scenario for this. If there is, we will of course change this behavior.

Aug 24, 2012 at 2:38 PM

Please try out with the latest source code.

Aug 25, 2012 at 6:17 PM

Great. It works exactly like I expected.

I'll start using it for changing application layout (MVP, WinForms). 

public interface ILayout
    DockStile Dock {get; set;}
    Control Parent {get; set;}

public class DefaultLayout: UserControl, ILayout
    // ...

// Start UP
ServiceLocator.Instance.RegisterType<ILayout, DefaultLayout>();

// On User Change Layout
ServiceLocator.Instance.RegisterType<ILayout, SimpleLayout>(true,true);

// On User Change Layout
ServiceLocator.Instance.RegisterType<ILayout, CustomLayout>(true,true);

void ResetLayout()
    var view = ServiceLocator.Instance.Resolve<ILayout>();
    view.Dock = DockStile.Full;
    view.Parent = MainForm;

Do you think it will work well?