IoC & Dependency Injection - SingletonTheory/SingletonTheory.github.io GitHub Wiki
ServiceStack has a default IoC Container that uses Funq. It also allows for your own IoC Containers, which is outlined here.
ServiceStack allows you to replace the default container by your own as outlined on their page here.
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>();
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.
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.