简体   繁体   English

创建使用.NET名称空间的库的最佳实践

[英]Best practices for creating libraries that use .NET namespaces

Is it bad practice to write a library that defines an interface dependent on another library? 编写定义依赖于另一个库的接口的库是不好的做法吗?

I know tight coupling is bad, but does this still apply when using .NET classes? 我知道紧密耦合是不好的,但是在使用.NET类时,这仍然适用吗?

For example, in .NET, if I have a library that returns a Color object, it would force a dependancy on System.Drawing on anything that uses my library. 例如,在.NET中,如果我有一个返回Color对象的库,则它将强制依赖System.Drawing使用我的库的任何内容。 Would I be better off creating my own Color-type class inside my library? 我会在自己的库中创建自己的Color-type类更好吗?

I distinguish between Volatile and Stable Dependencies . 我区分易变稳定依赖项

In general, Color looks like a Stable Dependency because it's already in the BCL, it's deterministic in nature and doesn't involve any resource-intensive out-of process communication, and neither does it rely on a particular set-up of its run-time environment. 通常,Color看起来像是稳定依赖项,因为它已经存在于BCL中,本质上是确定性的,不涉及任何资源密集型的进程外通信,也不依赖于其运行的特定设置-时间环境。

The only consideration here is that when it comes to Color, there are more than one such class in the BCL, so make sure that you really mean to target only Windows Forms applications with your API, because WPF has its own definition of Color. 这里唯一要考虑的是,涉及Color时,BCL中有多个这样的类,因此请确保您确实要使用API​​定位Windows Forms应用程序,因为WPF有其自己的Color定义。

If you just need the Color to paint parts of the UI in a certain color, then the built-in Color class is probably fine, but if Color is a main concept in your Domain Model, and you need to target different UIs (WPF, Windows Forms, Web) you would probably be better of by defining your own abstraction. 如果您只需要Color即可将用户界面的某些部分绘制成某种颜色,则内置的Color类可能很好,但是如果Color是您的域模型中的主要概念,则您需要定位不同的UI(WPF, Windows Forms,Web),最好定义自己的抽象。

In such a more advanced case, you could subsequently create Adapters and Mappers around your abstraction to bridge the gap between the abstraction and the concrete Color classes. 在这种更高级的情况下,您可以随后在抽象周围创建适配器和映射器,以弥合抽象与具体Color类之间的差距。

If it's a standard .NET library, I wouldn't worry about it. 如果它是一个标准的.NET库,我就不用担心。 No reason to map from one class to another ... what if System.Color changed in the next .NET release? 没有理由将一个类映射到另一个类...如果System.Color在下一个.NET版本中发生了更改,该怎么办? You'd have to change your mapping code too, and possibly have to detect the version and map accordingly. 您也必须更改映射代码,并且可能必须检测版本并进行相应的映射。 That's a real pain. 那真是痛苦。

With all of my libraries, I return objects that depend only on things in the library. 在所有库中,我返回仅依赖库中对象的对象。

I would ask myself why I was writing a library that would depend upon another namespace that was not implicit. 我会问自己为什么要编写一个依赖于另一个非隐式命名空间的库。 It seems to go against the whole "Encapsulation" concept. 它似乎与整个“封装”概念背道而驰。

So just by going off of my own reasoning and knowledge of OOP, I would say you are on the right track with returning your own non-dependent object. 因此,仅凭我自己的OOP推理和知识,我会说您在返回自己的非依赖对象的正确轨道上。

You pose an excellent question. 你提出了一个很好的问题。 The answer is: it depends. 答案是:这取决于。 In the case of standard libraries that will always be available, then it's fine; 如果标准库始终可用,那就很好了; the core library references different .DLLs all the time. 核心库始终引用不同的.DLL。

In the case of a third party library you run into issues. 对于第三方库,您会遇到问题。 It's not always a good idea to roll your own if something else does what you want, but then you have a dependency on another library, which is a logistical problem for users. 如果有其他事情可以满足您的需要,那么自己动手滚动并不是一个好主意,但是您却依赖于另一个库,这对用户来说是一个后勤问题。

There's no right answer here, you just have to go with what makes the most sense for your project. 这里没有正确的答案,您只需要选择最适合您的项目的方法即可。 Try to decouple as much as possible, yes, but sometimes you just gotta cuddle up and get the job done. 尝试使耦合尽可能地分离,是的,但是有时您只是抱紧拥抱并完成工作。

It depends on your usage of the class. 这取决于您对课程的使用情况。

If you ever need to obtain an instance of the system Color class from an instance of your Color class (for example if you draw to a windows form) then it would be better to use the System class - it saves you the effort of having to convert between the two types and gives you the advantage of being able to use all the "Features" of the Color class for free (such as built in constants for "Red", "Black", "Green" etc... 如果您需要从Color类的实例中获取系统Color类的实例(例如,如果绘制到Windows窗体),则最好使用System类-这样可以省去在两种类型之间进行转换,并为您提供了可以免费使用Color类的所有“功能”(例如,内置的“红色”,“黑色”,“绿色”等常量)的优点。

If on the other hand you are simply working with arbitrary RGB values (perhaps for scientific calculations) and never need to convert to an instance of System.Color, then it might make sense to create your own class. 如果在另一方面,你只是用任意的RGB值的工作(也许是科学计算), 绝不需要转换为System.Color的实例,那么它可能是有意义的创建自己的类。

In all likelihood you are better off using the System.Color class - yes encapsulation and all that is a good idea, however not at the expense of saving you massive amounts of time! 很有可能您最好使用System.Color类-是的,封装是个好主意,但是不以节省大量时间为代价!

You shouldn't worry about using anything in the .NET core library. 您不必担心在.NET核心库中使用任何东西。 You wouldn't get very far in writing a DLL without it. 没有它,编写DLL不会走得太远。 The only place possibly to be careful around it is the System.Web namespace as I believe .NET 4 has a client profile installer which basically means if you use this installer it will only install things expected to be used on a client. 唯一可能需要注意的地方是System.Web命名空间,因为我相信.NET 4具有客户端配置文件安装程序,这基本上意味着,如果您使用此安装程序,它将仅安装预期在客户端上使用的内容。 I personally think it is a bad idea on Microsoft's Part as it just adds un-necessary complication for saving a small amount of download time. 我个人认为这对Microsoft而言不是一个好主意,因为它只会增加不必要的复杂性,从而节省少量的下载时间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM