引用第三方程序集的最佳实践

时间:2022-09-11 21:46:38

I have been wondering what are best practices when it comes to referencing 3rd party assemblies. A while ago I asked a question "Dependencies and references: what exactly should I reference" and received something to think about but I am not completely convinced. I have a 3rd party assembly needed for building my project and according to what I have read so far I should only reference assemblies needed for building my project. This leaves the problem of the runtime dependencies of the 3rd party assembly. If I reference those too the list of references becomes huge (as there are several dependencies) and includes a lot of assemblies I have no clue what do and are really not related to my project. Also I want to avoid using public methods in any of those and just use the main 3rd party assembly. How can I go about doing this in a decent way? Any help is appreciated as I am quite confused.

我一直想知道在引用第三方程序集时最佳做法是什么。不久之前,我问了一个问题“依赖关系和参考文献:我究竟应该参考什么”,并收到了一些值得思考的问题,但我并不完全相信。我有一个第三方程序集需要构建我的项目,根据我到目前为止所读到的,我应该只引用构建我的项目所需的程序集。这留下了第三方程序集的运行时依赖性问题。如果我引用它们,引用列表也会变得很大(因为有几个依赖项)并且包含很多程序集,我不知道哪些与我的项目有什么关系。此外,我想避免在任何这些方法中使用公共方法,只需使用主要的第三方程序集。我怎样才能以体面的方式做到这一点?任何帮助都很受欢迎,因为我很困惑。

To give an idea of what I am dealing with here is the 3rd party library uses hibernate and log4net along with office interop assemblies.

为了让我知道我在这里处理的是第三方库使用hibernate和log4net以及办公室互操作程序集。

Thanks.

3 个解决方案

#1


3  

If i have to deal with libraries that aren't registered in the GAC, I use the following method:

如果我必须处理未在GAC中注册的库,我使用以下方法:

  • Make a folder "Libs" with all third party libraries and their dependencies in it
  • 创建一个文件夹“Libs”,其中包含所有第三方库及其依赖项

  • Reference the ones I need
  • 参考我需要的那些

  • Create a post-build action that copies all the files in the "Libs" folder to the "bin/debug" (or release) folder
  • 创建一个后构建操作,将“Libs”文件夹中的所有文件复制到“bin / debug”(或release)文件夹

If you don't know the dependencies, you can use Dependency Walker.

如果您不知道依赖项,则可以使用Dependency Walker。

#2


2  

An alternate I've been using on many (C#) projects is:

我在许多(C#)项目中使用的替代方案是:

  • Make a "Bin" folder in the solution directory (same level as the project directories)
  • 在解决方案目录中创建一个“Bin”文件夹(与项目目录相同)

  • Make the output of all projects ../Bin instead of bin/debug or bin/release
  • 输出所有项目../Bin而不是bin / debug或bin / release

  • Put whatever files that are not a result of the build in "Bin", including library dependencies, certain data files or whatever, depending on the project
  • 在“Bin”中放置任何不是构建结果的文件,包括库依赖项,某些数据文件或其他,具体取决于项目

  • Contents of "Bin" also goes to source control (without the build output, of course)
  • “Bin”的内容也进入源代码控制(当然没有构建输出)

  • Whatever libraries directly referenced by your projects goes to a "References" or "Libs" folder (sometimes I've also tried putting them in Bin too, but in this case you need to remember to disable copying of references to output dir when adding the reference to your project, which becomes tedious on large projects)
  • 无论您的项目直接引用哪些库都转到“引用”或“Libs”文件夹(有时我也尝试将它们放在Bin中,但在这种情况下,您需要记住在添加时禁用复制对输出目录的引用参考您的项目,这对大型项目来说变得乏味)

Not really sure if it's a good solution, but has worked for me till now.

不确定它是否是一个很好的解决方案,但到目前为止对我有用。

#3


1  

You can also use NDepend to know and control exactly what you use from tiers library. I wrote an article about this Controlling the usage of libraries

您还可以使用NDepend从层库中准确地了解和控制您使用的内容。我写了一篇关于控制库的使用的文章

#1


3  

If i have to deal with libraries that aren't registered in the GAC, I use the following method:

如果我必须处理未在GAC中注册的库,我使用以下方法:

  • Make a folder "Libs" with all third party libraries and their dependencies in it
  • 创建一个文件夹“Libs”,其中包含所有第三方库及其依赖项

  • Reference the ones I need
  • 参考我需要的那些

  • Create a post-build action that copies all the files in the "Libs" folder to the "bin/debug" (or release) folder
  • 创建一个后构建操作,将“Libs”文件夹中的所有文件复制到“bin / debug”(或release)文件夹

If you don't know the dependencies, you can use Dependency Walker.

如果您不知道依赖项,则可以使用Dependency Walker。

#2


2  

An alternate I've been using on many (C#) projects is:

我在许多(C#)项目中使用的替代方案是:

  • Make a "Bin" folder in the solution directory (same level as the project directories)
  • 在解决方案目录中创建一个“Bin”文件夹(与项目目录相同)

  • Make the output of all projects ../Bin instead of bin/debug or bin/release
  • 输出所有项目../Bin而不是bin / debug或bin / release

  • Put whatever files that are not a result of the build in "Bin", including library dependencies, certain data files or whatever, depending on the project
  • 在“Bin”中放置任何不是构建结果的文件,包括库依赖项,某些数据文件或其他,具体取决于项目

  • Contents of "Bin" also goes to source control (without the build output, of course)
  • “Bin”的内容也进入源代码控制(当然没有构建输出)

  • Whatever libraries directly referenced by your projects goes to a "References" or "Libs" folder (sometimes I've also tried putting them in Bin too, but in this case you need to remember to disable copying of references to output dir when adding the reference to your project, which becomes tedious on large projects)
  • 无论您的项目直接引用哪些库都转到“引用”或“Libs”文件夹(有时我也尝试将它们放在Bin中,但在这种情况下,您需要记住在添加时禁用复制对输出目录的引用参考您的项目,这对大型项目来说变得乏味)

Not really sure if it's a good solution, but has worked for me till now.

不确定它是否是一个很好的解决方案,但到目前为止对我有用。

#3


1  

You can also use NDepend to know and control exactly what you use from tiers library. I wrote an article about this Controlling the usage of libraries

您还可以使用NDepend从层库中准确地了解和控制您使用的内容。我写了一篇关于控制库的使用的文章