Resharper 和命名空间别名限定符

发布于 2024-12-28 11:56:48 字数 523 浏览 4 评论 0原文

我已经对此进行了一些搜索并尝试了一些东西,但如果不关闭一些我想要打开的东西就无法让它工作。

通常我让 Resharper 进行命名空间优化。在将 DTO 映射到域模型对象的服务实现中,为每个对象创建别名是一个很好的视觉效果。这样,当天色已晚且睡眠不足时,Dtos.CustomerDomainModel.Customer 会有所帮助。

using DomainModel = MyProduct.Core.Domain.Model;
using Dtos = MyProduct.ServiceModel.Dtos;

当我运行代码清理时,它会将这些更改为:

using DomainModel = MyProduct.Core.Domain.Model;
using Customer = MyProduct.Core.Domain.Model.Customer;

是否有人执行此操作或类似操作并阻止 R# 对其进行攻击?

I've searched around a bit for this and tried a few things and can't get it to work without turning some stuff off that I want on.

Normally I let Resharper have its way with namespace optimizations. In a Service implementation that's mapping DTO's to Domain Model objects it's a nice visual to create an alias for each. That way when it's late and you're sleep deprived seeing Dtos.Customer and DomainModel.Customer helps.

using DomainModel = MyProduct.Core.Domain.Model;
using Dtos = MyProduct.ServiceModel.Dtos;

When I run code cleanup it changes those to:

using DomainModel = MyProduct.Core.Domain.Model;
using Customer = MyProduct.Core.Domain.Model.Customer;

Does anyone do this or something similar and keep R# from whacking it?

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

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

发布评论

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

评论(1

离不开的别离 2025-01-04 11:56:48

当您允许时,ReSharper 会执行以下操作:缩短引用、清理代码。

尽管这种行为可能不适合您,但从技术上来说它是正确的。因为在你的代码中它会缩短一些东西。这就是算法应该做的事情。

这就是导致此结果的算法的工作原理:

  1. Resharper 找到一个可以缩短的引用(您没有给我足够的代码来准确地告诉您在哪里,但这并不重要)。
  2. 缩短后,需要确保可以解析该对象,因此它添加了一个新的 using (在您的情况下使用 Customer = MyProduct.Core.Domain.Model.Customer; )。
  3. Resharper 检测到 using Dtos = MyProduct.ServiceModel.Dtos; 不再使用(毕竟,其他 using 涵盖了您缩短的引用)。并将其删除。

我怀疑这就是造成这种情况的原因。如果没有看到代码中的实际用法,我无法 100% 确定。但这很可能是重构的原因。

This is something ReSharper will do when you let it: shorten references, on code cleanup.

Even though this behavior might be unwanted for you, it's technically seen correct. Because in your code it'll shorten something. And that's what the algorithm is supposed to do.

This is how the algorithm works that causes this result:

  1. Resharper finds a reference that can be made shorter (you've not given me enough code to exactly tell you where, but that doesn't matter).
  2. After shortening it needs to assure that the object can be resolved, so it adds a new using for it (using Customer = MyProduct.Core.Domain.Model.Customer; in your case).
  3. Resharper detects that using Dtos = MyProduct.ServiceModel.Dtos; is no longer in use (after all, the other using covers your shortened reference). And removes it.

This is what I suspect is causing this. I cannot be 100% certain without seeing the actual usages in code. But this is most likely the cause of the refactoring.

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