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

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

问题描述

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



我知道紧耦合是坏的,但是在使用时仍然适用。 NET类?



例如,在.NET中,如果我有一个返回Color对象的库,那么它会强制在使用我的任何东西的System.Drawing上依赖图书馆。我可以在我的图书馆内创建自己的Color-type类吗?

解决方案

我区分 Volatile 稳定依赖关系



一般来说,Color看起来像一个稳定的依赖关系,因为它已经在BCL中,它在本质上是确定性的,不涉及任何资源密集型的进程外通信,也不依赖于其运行的特定设置时间环境。



这里唯一的考虑是,当涉及到Color时,BCL中有不止一个这样的类,所以确保你真的意思是目标只有Windows Forms应用程序与您的API,因为WPF有自己的颜色定义。



如果您只需要颜色以某种颜色绘制UI的部分,那么内置的Color类可能很好,但是如果Color是您的域模型中的一个主要概念,并且您需要针对不同的nt UI(WPF,Windows Forms,Web)你可能会更好的定义自己的抽象。



在这样一个更高级的情况下,你可以随后创建适配器围绕你的抽象的绘图者来弥合抽象和具体的颜色类之间的差距。


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?

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?

解决方案

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.

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.

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天全站免登陆