什么时候应该使用[程序集:InternalsVisibleTo()]?

发布于 2024-09-10 03:18:07 字数 156 浏览 4 评论 0原文

据我所知,InternalVisibleTo 属性用于将具有内部访问修饰符的类型和方法公开给指定的程序集。我仅使用它来将内部方法公开给包含一组单元测试的单独程序集。

我正在努力思考应该使用此功能的另一种场景。这个属性是专门为了帮助单元测试而引入的还是还有其他原因?

I understand that the InternalVisibleTo attribute is used to expose types and methods with the internal access modifier to a specified assembly. I have only ever used this for exposing internal methods to a separate assembly containing a suite of unit tests.

I am struggling to think of another scenario when this should be used. Was this attribute introduced specifically to aid unit testing or was there another reason?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不爱素颜 2024-09-17 03:18:07

一种情况是程序集之间的逻辑是分离的(例如内部数据对象和逻辑层)。您不想向用户公开这些类,但仍想在自己的程序集中使用这些对象。

我认为这不是一个非常常见的场景,我很少在非单元测试上下文中使用 InternalsVisibleTo

A scenario can be that you have separation of logic between assemblies (like internal data objects and the logic layer). You don't want to expose the classes to your users, but you still want to use the objects in your own assemblies.

I think this is not a very common scenario, I hardly ever use InternalsVisibleTo in non unit tests context.

轮廓§ 2024-09-17 03:18:07

此场景与 Elisha 的类似,但有针对性在域驱动设计中强制正确使用域模型。

假设您有一个程序集 MyProject.Core,其中包含所有域模型。如果您不希望其他人直接创建您的域模型的实例,您可以将构造函数设置为内部

另一个程序集称为MyProject.Services,包含专门用于创建有效域对象的域服务。该程序集将引用MyProject.CoreInternalsVisibleTo 属性用于授予域服务程序集对内部 构造函数的访问权限。

MyProject.Services 引用到 MyProject.Core 的另一个好处是,它不允许域对象保留对域服务的任何引用,这被认为是另一种良好的 DDD 实践。

注意:我从未在实践中应用过上述场景,因此在 DDD 层面上可能不完全准确。但这是我能想到的 InternalsVisibleTo 的使用,与单元测试无关。

This scenario is similar to that of Elisha's, but is targeted at enforcing correct use of your domain model in Domain-driven design.

Let's say you have an assembly MyProject.Core, which contains all of your domain models. If you don't want other people directly creating instances of your domain models, you can make the constructors internal.

Another assembly, called MyProject.Services, contains domain services that are specialized in creating valid domain objects. This assembly will have a reference to MyProject.Core. The InternalsVisibleTo attribute is used to grant the domain service assembly access to the internal constructors.

Another benefit of the reference from MyProject.Services to MyProject.Core is that it disallows the domain objects to keep any references to domain services, which is considered another good DDD practice.

Note: I have never applied the above scenario in practice, so it may not be completely accurate on the DDD level. But this is a use of InternalsVisibleTo that I could think of, that isn't unit test related.

帝王念 2024-09-17 03:18:07

除了测试之外,我使用过InternalsVisibleTo 属性的唯一其他场景是创建序列化程序集时。

除此之外,我从未使用过,也不需要它。

Apart from testing, the only other scenario I've ever used the InternalsVisibleTo attribute, was when creating serialization assemblies.

Other than that, I've never used, nor needed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文