How to include 3rd party code in separate versions in one project

夙愿已清 提交于 2019-12-06 09:32:10

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). There are a log of ways to approach this, but for example:

Inline Versions

#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:

  • Common.sln
    • Common.SC65.csproj
      • MyClass.cs [shared]
      • MyClass.SC65.cs
    • Common.SC7.csproj
      • MyClass.cs [shared]
      • MyClass.SC7.cs
    • Common.SC72.csproj
      • MyClass.cs [shared]
      • MyClass.SC72.cs

In the above, MyClass.cs resides in the root and is included in every project. However, the .SC#.cs files are only included in the project targeting the specific sitecore version.

This pattern is used a lot by libraries that target different .NET platforms or various configurations. 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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!