Orchard CMS multiple dependency classes for the same interface

When using multiple implementations of the same service, what's the best way to specify which dependency Orchard CMS should inject?

For example, I need to use a Fake / Mock implementation for a web service user class in my development environment, but in a test / UAT / production environment, I need to use a real implementation. for example Interface:

public interface IWebServiceClient : IDependency {...}

      

Real implementation:

public class WebServiceClient : IWebServiceClient {...}

      

Fake implementation:

public class FakeWebServiceClient : IWebServiceClient {...}

      

Using Autofac on a non-Orchard site, I can use the ConfigurationSettingsReader and list my fake dependencies as overrides in the web.config. These overrides will be removed by transforming the web.config for non-debug deployments.

+3


source to share


2 answers


The easiest way to do this is to do every function.

[OrchardFeature("MyModule.Live")]
public class WebServiceClient : IWebServiceClient {...}

[OrchardFeature("MyModule.Test")]
public class FakeWebServiceClient : IWebServiceClient {...}

      



Then just define those functions in your Module.txt and enable the test on your computer and live in uat / live

+2


source


You can always define compile time constants for each of your project configurations, similar to how DEBUG works by default. That way, you could implement a single interface implementation and change behavior using preprocessor directives.



+1


source







All Articles