在我的项目中仅为 Owned<T> 引用 Autofac 是不是很糟糕的设计? [英] Is it bad design to reference Autofac in my projects just for Owned<T>?

查看:31
本文介绍了在我的项目中仅为 Owned<T> 引用 Autofac 是不是很糟糕的设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近成为 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 排除在我的接口之外--但它是有代价的:由于 Owned<> 是 Autofac 程序集的一部分,我必须在我的每个了解 Owned<的项目中引用 Autofac>,并将使用 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 用作通用工厂包装器是可以的.但是 Owned<> 在 Autofac 程序集中,每次我使用它时,我都会创建对 Autofac 的硬引用(即使我对 Autofac 的唯一引用是接口方法参数中的 Owned<> 类型).

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).

我的问题是:这是一件坏事吗?这是否会开始以某种我尚未考虑到的方式反咬我?有时我会有一个被许多其他项目引用的项目,所以我很自然地需要将它的依赖项尽可能地保持为零;通过将 Func>(实际上是数据库事务提供程序)传递到这些接口中的方法(否则将与 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)?

也许如果 Owned<> 是内置的 .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?)

推荐答案

我想说,在企业应用程序"解决方案(或任何需要灵活性).我认为在每个需要它的项目中至少依赖以下内容没有错:

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 框架就不会遇到这些问题
  • 将所有这些引用添加到每个解决方案中会产生明显但很小的开销

这篇关于在我的项目中仅为 Owned&lt;T&gt; 引用 Autofac 是不是很糟糕的设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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