Reputation: 883
I have a c# WebAPI Application that has the following structure
The Solution has many Projects, The core Project which is the actual WebAPI project and more than one separate projects that are used for communication with external systems. My Project uses dependency Injection like this: The core Project might have in its web config defined an external system. When the various dependencies are registered there exists a code like this
string ExternalSystem = ConfigurationManager.AppSettings["ExternalService"];
switch(ExternalSystem)
{
case "ExtA":
this.RegisterType<ExtA.ExternalService>().As<IExternalService>().InstancePerRequest();
break;
case "ExtB":
this.RegisterType<ExtB.ExternalService>().As<IExternalService>().InstancePerRequest();
break;
default:
this.RegisterType<ExternalService>().As<IExternalService>().InstancePerRequest();
break;
}
The definition of IExternalService is in the Core Project and each of the Extrnal Systems has it's own Implementation of this Interface. So the decision on which of the implementations is going to be used is defined in the web config file in the Core Project. This implemantation has one problem. When I create my release dlls, I have to include all external system's Dlls (even if they are not used), otherwise lines of code like this
this.RegisterType<ExtA.ExternalService>().As<IExternalService>().InstancePerRequest();
won't compile. In other words, my app may be used from a customer who only communicates with External System A. In this configuration I want to include only the dlls from the core Project and External System A dll. If the customer is using B then only B will be included and so on. Is that Possible?
Upvotes: 0
Views: 259
Reputation: 24280
Maybe using conditional statements could work. Instead of this:
using Lib_ExtA;
using Lib_ExtB;
using Lib_General;
... use this:
#if UseExtA
using ExternalService = ExtA.ExternalService;
#endif
#if UseExtB
using ExternalService = ExtB.ExternalService;
#endif
#if UseGeneral
using ExternalService = General.ExternalService;
#endif
... followed by just:
this.RegisterType<ExternalService>().As<IExternalService>().InstancePerRequest();
You might combine this with the switch
approach, giving an extra check if you did everything right, but it basically means you're doing it twice.
References:
#if (C# Reference)
https://msdn.microsoft.com/en-us/library/4y6tbswk.aspx
/define (C# Compiler Options)
https://msdn.microsoft.com/en-us/library/0feaad6z.aspx
Upvotes: 2