Using my own IoC container to resolve additional dependencies

392 views
Skip to first unread message

Brandon Wallace

unread,
May 16, 2011, 2:58:06 PM5/16/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi,

We are using Castle as our IoC container. ServiceStack obviously uses
Funq which is great. I'd like to be able to inject some dependencies
from my container before the service is executed.

As far as I can tell, there isn't anyway to do this currently.

I was hoping I'd be able to override some method in my subclass of
AppHostBase that would get called after the IService<T> was created
but before its Execute method was called. Something like:

public MyAppHost : AppHostBase
{
public MyAppHost(IContainer myContainer) : base("my services",
typeof(HelloService).Assembly)
{ _myContainer = myContainer; }

// Called after 'service' instance is created but before it is
executed.
public override void BeforeExecute(object service)
{
// I can now use _myContainer to resolve some additional
dependencies on service before service.Execute() is called.
}

// Called after 'service.Execute()'
public override void AfterExecute(object service)
{
// I can release any dependencies back to the container
}
}

Is there currently any way to do this that I am overlooking, or do I
need to fork the source to add this capability?

Thanks!
Brandon

Demis Bellot

unread,
May 16, 2011, 3:08:33 PM5/16/11
to servic...@googlegroups.com
Hi Brandon,

The latest version of ServiceStack in GitHub should have support for delegating to another IOC.
Basically you just need to implement your own IContainerAdapter, here are some tests that demonstrate this behaviour:


As I don't think this made it into the last 2.09 release you will either need to cut a build from the source code in GitHub,
Or alternatively you should be able to use the newer builds available at:

Hope this helps.

Regards,

Brandon Wallace

unread,
May 16, 2011, 5:50:46 PM5/16/11
to ServiceStack .NET Open Source REST Web Services Framework
Hi Demis,

This is almost perfect. The only downside is that after the objects
are no longer needed, I need to be able to release the components I
resolve. I gather than Funq does not implement component burden?

Brandon

Demis Bellot

unread,
May 16, 2011, 6:24:03 PM5/16/11
to servic...@googlegroups.com
Hi Brandon,

Whenever I need to explicitly control the lifetime of a resource (i.e. with RDMBS or Redis connection pooling functionality) myself, I inject the connection factory into my services instead of a connection.

This makes it easier to manage the connection and ensure thread safety without resorting to any complex configuration / registration.
This would still be my preference even if we had an IOC that would allow me to configure this behaviour.

Note: the 2 common base classes for REST and Normal services are easy to change and suit your requirements so you could easily implement an OnAfterExecute method that would find all your dependencies and released them back into your IOC container.

Here's all the source code for both base classes:


Cheers,
D

Brandon Wallace

unread,
May 16, 2011, 8:15:29 PM5/16/11
to ServiceStack .NET Open Source REST Web Services Framework
Demis,

We've taken the stance that the service implementers get to decide the
appropriate lifetime of their components and the client code shouldn't
be concerned with it, even if the client code is getting objects from
an injected factory.

I've also realized the best way for me to proceed (without forking
service stack source code) is to indeed build a base class
implementation of IService<T> which does the dependency injection and
have all my services inherit from it.

Thank you very much for the quick responses and guidance. And of
course for the library :)
Reply all
Reply to author
Forward
0 new messages