I have a class which should have an instance of a Service
(to access the database and other services):
public class MyFoo : IFoo
{
public Service Service { get; set; }
public MyFoo (Service service)
{
Service = service;
}
}
I want to register this class with ServiceStack's IoC container. Currently, I am doing it like this
container.Register<IFoo> (c => new MyFoo (c.TryResolve<MyService> ()));
This does work fine, but it strikes me as pretty ugly to resolve an element in a container while registering another one. Is there a more elegant way to accomplish this?
You can also Register the dependency using one of the AutoWired APIs, e.g:
container.RegisterAutoWiredAs<MyFoo,IFoo>()
Also ServiceStack will inject each public property with any registered dependencies so you don't need your constructor, e.g:
public class MyFoo : IFoo
{
public Service Service { get; set; }
}