![](/img/trans.png)
[英]Build a Visual Studio Solution with C# AND Powershell projects
[英]Visual Studio C# Solution with many Projects (Dependencies)
该解决方案有许多项目,核心项目是实际的WebAPI项目,还有多个用于与外部系统通信的独立项目。 我的项目使用依赖项注入是这样的:核心项目可能在其Web配置中定义了一个外部系统。 当注册了各种依赖项时,就存在这样的代码
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;
}
IExternalService的定义在核心项目中,每个Extrnal系统都有自己的接口实现。 因此,在Core Project中的Web配置文件中定义了将使用哪种实现的决定。 这种实现有一个问题。 创建发布dll时,我必须包括所有外部系统的Dll(即使不使用它们),否则像这样的代码行
this.RegisterType<ExtA.ExternalService>().As<IExternalService>().InstancePerRequest();
不会编译。 换句话说,我的应用程序可以由仅与外部系统A通信的客户使用。在此配置中,我只希望包含核心项目和外部系统A dll中的dll。 如果客户使用的是B,则仅包含B,依此类推。 那可能吗?
也许使用条件语句可能有效。 代替这个:
using Lib_ExtA;
using Lib_ExtB;
using Lib_General;
... 用这个:
#if UseExtA
using ExternalService = ExtA.ExternalService;
#endif
#if UseExtB
using ExternalService = ExtB.ExternalService;
#endif
#if UseGeneral
using ExternalService = General.ExternalService;
#endif
...然后是:
this.RegisterType<ExternalService>().As<IExternalService>().InstancePerRequest();
您可以将其与switch
方法结合使用,以检查是否做对了所有事情,但这基本上意味着您做了两次。
参考文献:
#if(C#参考)
https://msdn.microsoft.com/zh-CN/library/4y6tbswk.aspx
/ define(C#编译器选项)
https://msdn.microsoft.com/zh-CN/library/0feaad6z.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.