IoC & Dependency Injection - SingletonTheory/SingletonTheory.github.io GitHub Wiki

IoC Container

Default Service Stack Container

ServiceStack has a default IoC Container that uses Funq. It also allows for your own IoC Containers, which is outlined here.

Replacing the Default

ServiceStack allows you to replace the default container by your own as outlined on their page here.

Getting Access to your Dependencies

ServiceStack allows you to get access to the configured dependencies by using two techniques as follows

  • Service From the Service Base class you can get access to your dependencies by doing the following.
base.GetResolver().TryResolve<IDepency>();
  • EndpointHost.Config.ServiceManager.Container
Container container = EndpointHost.Config.ServiceManager.Container;
IDependency dependency = container.Resolve<IDependency>();

Design Considerations

By using this feature it allows us to keep our code and projects clean of concrete dependencies. By extracting our types and behaviours into interfaces for databases, repositories and other items we alleviate ourselves from the getting into situations where we have to wire everything to tightly, thereby making it harder to inject new concrete types.

Overall Design

By making a decision that dependencies will be injected from the top of the stack to the lower level items, it centralizes where and how dependecies are managed. As an example, consider resolving your dependencies in the Service and passing it into the lower levels like Repositories rather than resolving them in the Repository itself. By doing so, your Service will be able to manage the dependencies such as databases. By using the IoC Container and managing them as dependencies in the AppHost, you'll be able to configure your entire service in one central place.

⚠️ **GitHub.com Fallback** ⚠️