When using multiple implementations of the same service what is the best way to specify which dependency Orchard CMS should inject?
For example, I need to be able to use a Fake/Mock implementation of a web-service consumer class in my development environment, but in a Test/UAT/Production environment I need to use a real implementation. e.g. Interface:
public interface IWebServiceClient : IDependency {...}
Real Implementation:
public class WebServiceClient : IWebServiceClient {...}
Fake Implementation:
public class FakeWebServiceClient : IWebServiceClient {...}
Using Autofac on a non-Orchard site for example I might use the ConfigurationSettingsReader and specify my fake dependencies as overrides in the web.config. These overrides would be removed by a web.config transformation for non-debug deployments.
The simplest way to do it would be to make each a feature.
[OrchardFeature("MyModule.Live")]
public class WebServiceClient : IWebServiceClient {...}
[OrchardFeature("MyModule.Test")]
public class FakeWebServiceClient : IWebServiceClient {...}
Then just define those features in your Module.txt and enable the test on your machine and live in uat/live
You could always define compile-time constants for each of your project's configurations, similar to the way that DEBUG works by default. That way you could have a single implementation of the interface and alter the behavior using pre-processor directives.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With