Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orchard CMS multiple IDependency classes for same interface

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.

like image 709
Lance H Avatar asked Sep 13 '26 02:09

Lance H


2 Answers

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

like image 130
Hazza Avatar answered Sep 14 '26 15:09

Hazza


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.

like image 45
HP_Hovercraft Avatar answered Sep 14 '26 15:09

HP_Hovercraft