Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Checking for Framework Conventions

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.

like image 398
Gent Avatar asked Jan 27 '12 19:01

Gent


1 Answers

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.

like image 169
Matt Kellogg Avatar answered Oct 16 '22 00:10

Matt Kellogg