[英]How to include 3rd party code in separate versions in one project
I've got an interesting problem on my hands and I can't quite figure out the right way of handling it. 我手上遇到了一个有趣的问题,我无法完全弄清楚处理该问题的正确方法。 This is specific to sitecore, but I would imagine the fix to the issue would be one that could be applied to anyone that has multiple websites running different versions of a framework.
这是特定于sitecore的,但是我想解决此问题的方法将是可以应用于拥有多个运行不同版本框架的网站的任何人。
Right now I have 3 separate websites running Sitecore as the framework and CMS for the sites. 现在,我有3个单独的网站,分别运行Sitecore作为框架和网站的CMS。 One website it running code from Sitecore 6.5, another is on 7.0, and another is on 7.0 but will be 7.2 soon enough.
一个网站从Sitecore 6.5运行代码,另一个网站在7.0上,另一个网站在7.0上,但很快就会达到7.2。
One of the core principles of programming is do not repeat yourself. 编程的核心原则之一是不要重复自己。 I want to set up a separate C# project to include handling of Sitecore specific logic and classes.
我想建立一个单独的C#项目,以包括处理Sitecore特定的逻辑和类。 It would mostly include utility like classes that do simple functions to make my life easier checking many kinds of things.
它主要包括类之类的实用程序,它们执行简单的功能,使我的生活更轻松地检查多种事物。 These base features are included in each version of Sitecore I am using.
这些基本功能包含在我正在使用的每个Sitecore版本中。
Basically there is a ton of shared functionality between the Sitecore DLLs despite the differences, and I want to be able to write version agnostic code in one place. 尽管有差异,但基本上在Sitecore DLL之间有大量共享功能,我希望能够在一个地方编写与版本无关的代码。
I don't care if it needs to build out 3 separate DLLs for each set of Sitecore DLLs I need to compile with, as long as I can keep one base source. 我不在乎它是否需要为我需要编译的每套Sitecore DLL建立3个单独的DLL,只要我可以保留一个基本源即可。 Is this sort of thing possible?
这种事情可能吗?
How I would handle it: 我将如何处理:
Setup an independent project and make use of configurations/symbols. 设置一个独立的项目,并使用配置/符号。 A lot of the simple .NET code can probably be universally shared, however give you're working with different versions of SC you would most likely deal with deprecated functionality, API changes, etc. One example I can think of is
UIFilterHelpers.ParseDatasourceString
(which is deprecated in 7.2 in favor of SearchStringModel.ParseDatasourceString
). 许多简单的.NET代码可能可以通用共享,但是,如果您使用的是不同版本的SC,则很可能会处理不推荐使用的功能,API更改等。我可以想到的一个示例是
UIFilterHelpers.ParseDatasourceString
(在7.2中已弃用,而SearchStringModel.ParseDatasourceString
)。 There are a log of ways to approach this, but for example: 有很多方法可以解决此问题,例如:
#if SC7
IEnumerable<SearchStringModel> searchStringModel = UIFilterHelpers.ParseDatasourceString(Attributes["sc_datasource"]);
#else //SC72
IEnumerable<SearchStringModel> searchStringModel = SearchStringModel.ParseDatasourceString(Attributes["sc_datasource"]);
#endif
Another approach is to use partial
classes and define version-specific implementations (then only include those in the correct project. Maybe you have: 另一种方法是使用
partial
类并定义特定于版本的实现(然后仅将它们包含在正确的项目中。也许您具有:
In the above, MyClass.cs
resides in the root and is included in every project. 在上面,
MyClass.cs
驻留在根目录中,并且包含在每个项目中。 However, the .SC#.cs
files are only included in the project targeting the specific sitecore version. 但是,
.SC#.cs
文件仅包含在针对特定站点核心版本的项目中。
This pattern is used a lot by libraries that target different .NET platforms or various configurations. 针对不同的.NET平台或各种配置的库经常使用此模式。 To use an MVC example, you'd have
MyProject.csproj
, MyProject.MVC3.csproj
, MyProject.MVC4.csproj
, MyProject.MVC5.csproj
(each with different references and possibly framework versions). 要使用MVC示例,您需要拥有
MyProject.csproj
, MyProject.MVC3.csproj
, MyProject.MVC4.csproj
, MyProject.MVC5.csproj
(每个都有不同的引用以及可能的框架版本)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.