简体   繁体   English

具有许多项目的Visual Studio C#解决方案(依赖项)

[英]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.aspx https://msdn.microsoft.com/zh-CN/library/4y6tbswk.aspx

/define (C# Compiler Options) / define(C#编译器选项)
https://msdn.microsoft.com/en-us/library/0feaad6z.aspx https://msdn.microsoft.com/zh-CN/library/0feaad6z.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C#和Powershell项目构建Visual Studio解决方案 - Build a Visual Studio Solution with C# AND Powershell projects 为解决方案中的多个 Visual Studio 2012 C# 项目启动调试器时出现 Visual Studio 2017 警告错误 - Visual Studio 2017 warning errors when starting a debugger for multiple Visual Studio 2012 C# projects in a solution C#解决方案 - 有多少个项目? - C# Solution - How many projects? .Net解决方案与c#和可视化基础项目 - .Net Solution with c# and visual basic projects MS Visual Studio解决方案,结合了非托管C ++项目和C#项目 - MS Visual Studio solution combining unmanaged C++ project and C# projects 使用 CMake(针对 Visual Studio 的 CMake)在同一解决方案中创建 C# 和 C++/CLR 项目 - Creating C# and C++/CLR projects in the same solution using CMake (CMake targeting visual studio) 解决方案中的太多项目会降低Visual Studio的性能吗? - Does too many projects in a solution slow down Visual Studio performance? 我可以使用 c# 在 Visual Studio 中的同一解决方案中使用两个项目吗 - Can I use two projects in same solution in Visual Studio using c# 使用Visual Studio中的NLog在多个项目中设置C#解决方案 - Setting up C# solution with multiple projects using NLog in Visual Studio c# .net - 关于 Visual Studio 2010 中同一解决方案下的多个项目的问题 - c# .net - Question about multiple Projects under same solution in Visual Studio 2010
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM