Static Checking for Framework Conventions
Asked Answered
C

1

9

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.

Coplin answered 27/1, 2012 at 19:35 Comment(1)
did you try FxCop/Code Analysis?Pieeyed
L
4

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.

Laverne answered 27/1, 2012 at 22:30 Comment(3)
this is a really cool approach, I will give it a shot to see if it will cover the use cases I'm concerned with this weekend.Coplin
I like it. It's as flexible as you're going to get. It's only limited by the flexibility of reflection. That is, to say, you probably won't run into something that can't be tested.Laverne
thanks I was able to cover all of my cases through reflection!Coplin

© 2022 - 2024 — McMap. All rights reserved.