它是一个很好的做法,创造超过如MS企业库或log4net的第三方组件的包装? [英] Is it a good practice to create wrapper over 3rd party components like MS enterprise Library or Log4net?

查看:98
本文介绍了它是一个很好的做法,创造超过如MS企业库或log4net的第三方组件的包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更像是一个很好的做法问题。我想提供如日志,缓存等不同的通用库有很多,如MS企业库,log4net的,NCACHE等这些第三方库。



我想知道,如果它是一个很好的做法,直接使用这些或通过每个服务创建包装和使用DI代码注入该服务。



关于


解决方案

这是主观的,但也取决于库。



例如,log4net的或NHibernate的有严格的界面基于API的。没有必要对包裹它们。还有其他的图书馆这将使你的课很难对其进行测试,如果你没有在之间的任何接口。在那里,它也许应该写一个干净的界面。



有时善意的提醒,让只的代码访问的API例如像NHibernate的或GUI库的一小部分。 (注:这不是包装,这是建设抽象层)另一方面,它没有任何意义,以减少进入log4net的电话,这将在整个应用程序中使用

log4net的可能是一个很好的例子,其中包装仅仅是矫枉过正。有些人遭受wrappitis,这是一种抗图案的(有时也被称为包装羊毛棉),只是产生更多的工作。 log4net的有这样一个简单的API,是高度可定制的,他们做的更好那么你的包装很可能会。



您会发现,包装出库不会让你只是与其他产品的交流一下。升级到新版本也将不容易,而你需要更新你的什么包装。


This is more like a good practise question. I want to offer different generic libraries like Logging, caching etc. There are lots of third party libraries like MS enterprise library, log4Net, NCache etc for these.

I wanted to know if its a good practise to use these directly or create wrapper over each service and Use a DI to inject that service in the code.

regards

解决方案

This is subjective, but also depends on the library.

For instance, log4net or NHibernate have strictly interface based API's. There is no need to wrap them. There are other libraries which will make your classes hard to test if you don't have any interfaces in between. There it might be advisable to write a clean interface.

It is sometimes good advise to let only a small part of the code access API's like for instance NHibernate or a GUI library. (Note: This is not wrapping, this is building abstraction layers.) On the other side, it does not make sense to reduce access to log4net calls, this will be used in the whole application.

log4net is probably a good example where wrapping is just overkill. Some people suffer of "wrappitis", which is an anti-pattern (sometimes referred to as "wrapping wool in cotton") and just produces more work. Log4net has such a simple API and is highly customizable, they made it better then your wrapper most probably will be.

You will find out that wrapping a library will not allow you to just exchange it with another product. Upgrading to newer versions will also not be easier, rather you need to update your wrapper for nothing.

这篇关于它是一个很好的做法,创造超过如MS企业库或log4net的第三方组件的包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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