框架约定的静态检查

发布于 2024-12-29 19:25:02 字数 420 浏览 0 评论 0原文

是否有一个产品/项目允许您定义约定,例如 MVC 项目来静态检查命名约定,例如将控制器附加在从控制器继承的类的末尾和/或在装饰方法时强制执行特定方法签名一个属性。

我基本上是在寻找一种方法,为加入我们团队的新开发人员设置一些护栏,我们有一套明确的约定,其中一些约定用于通过反射动态连接事物。看到这种反射连接会因为签名不兼容而失败,这对我们的启动过程来说是一个巨大的福音。

所需的关键功能:

  • 静态/编译时检查损坏的规则
  • 能够定位用特定属性修饰的方法(通过正则表达式或向导)
  • 基于不同类型的项目的不同规则集。 (例如:MVC 应用程序的一组约定,Web 表单应用程序的一组不同的约定,以及后缀为 .BLL 的类库的一组不同的约定)

任何输入建议都值得赞赏,尽管我要求您只有在知道时才做出回应支持这些功能。

Is there a product/project that would allow you to define conventions for say an MVC Project to statically check for naming conventions like Controller being appended on the end of classes that inherit from controller and/or enforce a certain method signature when decorating a method with an attribute.

I am basically looking for a way to kind of set up some guard rails for new developers coming onto our team where we have a clear set of conventions some of which are used to wire things up dynamically through reflection. Seeing that this reflection wire-up would fail because of an incompatible signature would be a huge boon to our ramp up process.

Key Features Needed:

  • Static/Compile time checking for broken rules
  • Ability to target methods decorated with specific attributes (via RegEx or a Wizard)
  • Different Sets of rules based on different types of projects. (example: A set of conventions for an MVC App, a different set for a Web Forms App, and a different set for a Class Library suffixed with .BLL)

Any input suggestions are appreciated although I ask that you only respond if you know that these features are supported.

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

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

发布评论

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

评论(1

木森分化 2025-01-05 19:25:02

这里是个人经验,但我总是为这样的事情编写测试。我解析我的程序集并确保一切遵循约定。对于几个具体示例,我检查 WCF 请求/响应对象,以确保它们没有通过线路发送“DTO”,并且它们都位于一致的 XML 命名空间中。

这是一个简单的示例,可确保所有服务方法返回继承 BaseResponse 对象的内容:

[Test]
public void All_IMyService_methods_should_return_a_BaseResponse()
{
    var methods = typeof (IMyService).GetMethods();
    foreach (var methodInfo in methods)
        Assert.That(typeof (BaseResponse).IsAssignableFrom(methodInfo.ReturnType), "Service Method " + methodInfo.Name + " does not return a BaseResponse");
}

我确信有人会有更好/更自动化的东西,但这对我有用。

Personal experience here, but I always write tests for things like this. I parse through my assemblies and make sure things are following conventions. For a couple of specific examples, I check WCF request/response objects to make sure they aren't sending "DTO" over the wire and they were all in a consistent XML namespace.

Here's a quick example that makes sure that all service methods return something that inherits a BaseResponse object:

[Test]
public void All_IMyService_methods_should_return_a_BaseResponse()
{
    var methods = typeof (IMyService).GetMethods();
    foreach (var methodInfo in methods)
        Assert.That(typeof (BaseResponse).IsAssignableFrom(methodInfo.ReturnType), "Service Method " + methodInfo.Name + " does not return a BaseResponse");
}

I'm sure someone will have something better/more automated, but this worked for me.

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