使用.NET命名空间创建库的最佳实践 [英] Best practices for creating libraries that use .NET namespaces

查看:241
本文介绍了使用.NET命名空间创建库的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是不好的做法编写定义了一个接口依赖于另一个库?

Is it bad practice to write a library that defines an interface dependent on another library?

我知道紧耦合不好,但是这是否使用.NET类时,仍然适用?

I know tight coupling is bad, but does this still apply when using .NET classes?

例如,在.NET中,如果我有一个返回Color对象库,它将迫使上使用我的库上的任何一个System.Drawing中依于。我会过得更好创造我自己的颜色,类型的类我的图书馆里面?

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. Would I be better off creating my own Color-type class inside my library?

推荐答案

我分辨的挥发性 稳定依赖

在一般情况下,颜色看起来像一个稳定的相关性,因为它已经在BCL,它的确定性性质,不涉及任何资源密集的过程通信外,并且它也不依赖于特定的一组镜头,它的运行时环境。

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.

这里唯一要考虑的是,当涉及到颜色,有不止一个这样的类的BCL,所以一定要确保你真正的意思是只针对Windows窗体应用程序与你的API,因为WPF有自己的定义颜色。

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.

如果你只需要颜色绘制UI的部分中某一种颜色,那么内置色彩类可能是好的,但如果颜色是你的域模型一个主要概念,你需要针对不同的用户界面(WPF,Windows窗体,网络),你可能会定义自己的抽象是更好的。

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.

在这种更先进的情况下,可以随后创建适配器和映射器在你的抽象弥合抽象和具体的颜色类之间的差距。

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.

这篇关于使用.NET命名空间创建库的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆