它是坏的设计参考Autofac在我的项目只是为国有< T&GT ;? [英] Is it bad design to reference Autofac in my projects just for Owned<T>?

查看:244
本文介绍了它是坏的设计参考Autofac在我的项目只是为国有< T&GT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近成为Autofac的OwnedInstances设有一个沉重的用户。例如,我用它来创建用于我的数据库,一个工作单元提供一个工厂,这意味着我的课这取决于的UnitOfWork工厂所要求的类型的对象:

I've recently become a heavy user of Autofac's OwnedInstances feature. For example, I use it to provide a factory for creating a Unit of Work for my database, which means my classes which depend on the UnitOfWork factory are asking for objects of type :

Func<Owned<IUnitOfWork>>

这是非常有用的 - 伟大的保持了IDisposable出我的接口 - 但它是有代价的:因为拥有<>为Autofac组件的一部分,我必须引用Autofac在每个我的项目,它知道拥有和LT的;>,并把使用Autofac.Features.OwnedInstances在每一个代码文件。

This is incredibly useful--great for keeping IDisposable out of my interfaces--but it comes with a price: since Owned<> is part of the Autofac assembly, I have to reference Autofac in each of my projects that knows about Owned<>, and put "using Autofac.Features.OwnedInstances" in every code file.

Func键<>有被内置在.NET框架大有裨益,因此我毫不怀疑,它的优良使用Func键作为通用工厂包装。但是,国有及LT;>是Autofac装配,每次我用它时我创建一个硬参考Autofac(甚至当我到Autofac仅供参考,是一家拥有<在接口方法参数>类型)。

Func<> has the great benefit of being built into the .NET framework, so I have no doubts that it's fine to use Func as a universal factory wrapper. But Owned<> is in the Autofac assembly, and every time I use it I'm creating a hard reference to Autofac (even when my only reference to Autofac is an Owned<> type in an interface method argument).

我的问题是:这是一个糟糕的事情吗?这是否会开始咬我回以某种方式,我还没有考虑到?有时候,我会是由许多其他项目中引用的项目,所以很自然我需要保持它的依赖尽可能接近零;我在做通过传递函数求<恶;国有< IUnitOfWork >>(这是一个有效的数据库事务提供者)到这些接口的方法(这本来是autofac无关)?

My question is: is this a bad thing? Will this start to bite me back in some way that I'm not yet taking into account? Sometimes I'll have a project which is referenced by many other projects, and so naturally I need to keep its dependencies as close as possible to zero; am I doing evil by passing a Func<Owned<IUnitOfWork>> (which is effectively a database transaction provider) into methods in these interfaces (which would otherwise be autofac-agnostic)?

或许,如果国有<>是一个内置的.NET类型,这整个的困境会消失? (应我甚至认为我的呼吸要做到这一点?)

Perhaps if Owned<> was a built-in .NET type, this whole dilemma would go away? (Should I even hold my breath for that to happen?)

推荐答案

我会说,这是细到引用一个定义良好的集在企业应用解决方案的各个项目的核心第三方的DLL(或的任何应用程序都需要的灵活性)。我认为没有错有对,至少在需要它的每个项目下面的依赖关系:

I would say that it's fine to reference a well defined set of core 3rd party DLLs in every project of an "enterprise application" solution (or any application that needs flexibility). I see nothing wrong with having a dependency on at least the following in every project that needs it:


  • 系统日志框架(如log4net的)

  • 某些IoC容器(如Autofac)

这这些都不是部分的事实。核心NET框架不应该利用他们作为自由地阻止我们

The fact that these aren't part of the core .NET framework shouldn't stop us from using them as liberally.

相比,可能带来的好处,唯一可能的底片,我可以看到相对较小:

The only possible negatives I can see are relatively minor compared to the possible benefits:


  • 这可能使应用程序更难理解为一般的程序员

  • 您可以在未来版本的兼容性问题其中,如果你只是使用.NET Framework

  • 有与添加所有这些引用的每一个解决方案

  • 明显,但是小的开销,你不会遇到
  • This may make the application harder to understand for the average programmer
  • You could have version compatibility problems in the future which you wouldn't encounter if you were just using the .NET framework
  • There is an obvious but minor overhead with adding all of these references to every solution

这篇关于它是坏的设计参考Autofac在我的项目只是为国有&LT; T&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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