[英]Visual Studio C# Solution with many Projects (Dependencies)
I have ac# WebAPI Application that has the following structure 我有一个具有以下结构的ac#WebAPI应用程序
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. 该解决方案有许多项目,核心项目是实际的WebAPI项目,还有多个用于与外部系统通信的独立项目。 My Project uses dependency Injection like this: The core Project might have in its web config defined an external system.
我的项目使用依赖项注入是这样的:核心项目可能在其Web配置中定义了一个外部系统。 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. IExternalService的定义在核心项目中,每个Extrnal系统都有自己的接口实现。 So the decision on which of the implementations is going to be used is defined in the web config file in the Core Project.
因此,在Core Project中的Web配置文件中定义了将使用哪种实现的决定。 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
创建发布dll时,我必须包括所有外部系统的Dll(即使不使用它们),否则像这样的代码行
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.
换句话说,我的应用程序可以由仅与外部系统A通信的客户使用。在此配置中,我只希望包含核心项目和外部系统A dll中的dll。 If the customer is using B then only B will be included and so on.
如果客户使用的是B,则仅包含B,依此类推。 Is that Possible?
那可能吗?
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. 您可以将其与
switch
方法结合使用,以检查是否做对了所有事情,但这基本上意味着您做了两次。
References: 参考文献:
#if (C# Reference)
#if(C#参考)
https://msdn.microsoft.com/en-us/library/4y6tbswk.aspxhttps://msdn.microsoft.com/zh-CN/library/4y6tbswk.aspx
/define (C# Compiler Options)
/ define(C#编译器选项)
https://msdn.microsoft.com/en-us/library/0feaad6z.aspxhttps://msdn.microsoft.com/zh-CN/library/0feaad6z.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.