tags:

views:

383

answers:

6

I know it's possible, and I've seen simple examples in the docs, but I was wondering if anyone on here has actually used them. I use attributes at the class and method level all the time, but have never used them on method parameters. Have any of you guys? If so, what was your reason and can we see the example?

I'm not interested in seeing a textbook example, mind you. There are plenty of those out there. I want to see an actual reason why it solved a particular problem for you. Thanks!

EDIT: Let's place aside the discussion about whether or not to use attributes in the first place. I understand some people don't like them because they "dirty" their code. That's for a different discussion!

+7  A: 

You can for example create a ValidatorAttribute for every parameter, then before calling the method, you can reflect the parameter attributes and do parameter validation. Then call the method if all ok.

leppie
very interesting, i never thought of using that way. well, i have never though of using them at all, but regardless. makes you think.
mattlant
Very interesting. I like that.
Kilhoffer
very interesting and makes things robust, but doesn't this also add a lot of overhead?
Alexandre Brisebois
@Alexandre: Probably, but for say web calls, not much. You can of course cache the reflected info, and build a delegate of sorts to call. That overhead would be almost equivalent to 'inserting' the 'validation code' inside the method.
leppie
+2  A: 

I haven't used them myself, but Scott Gu's post about ASP.NET MVC Preview 5 shows parameter attributes being used to declare model binders for action methods.

The link is at: http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx

He notes that the attribute isn't yet available in Preview 5, but should be available in future builds.

Brian Vallelunga
+1  A: 

(I've left this answer here in case others find it a useful intro to PostSharp, but it doesn't actually answer the question properly! I misread the question as asking about method attributes instead of class attributes. Doh. From what I remember, the generated SOAP classes use parameter attributes. LINQ to SQL uses return attributes and may use parameter attributes too, when it comes to stored procs.)

I'm used them with PostSharp, although admittedly only in a quick demo so far (I haven't used PostSharp in production).

See my blog post for more details.

Oh, and of course NUnit tests specify [Test] all over the place :)

Jon

Jon Skeet
In answering this question, you introduced me to PostSharp. Very interesting library, indeed. Thanks!
Kilhoffer
Shame I answered the question so badly though - hadn't read it properly! Will edit now...
Jon Skeet
Actually, I don't think Kilhoffer was talking about method attributes. He's talking about parameter attributes.
drraude: Yes indeed, hence my edit :)
Jon Skeet
+1  A: 

Castle Monorail has been using for many years to databind request parameters. See http://www.castleproject.org/MonoRail/documentation/trunk/integration/ar.html

Mauricio Scheffer
+1  A: 

Dependency Injection is a very good example scenario.

ObjectBuilder (a dependecy injection container, part of the P&P Enterprise Libary, soon to be replaced by Unity), uses them all over the place to attribute what the container should be injecting at runtime. Here's a quick example of the constructor for a controller class that has a state value (injected from whatever state provider is active, usually HttpSession) as well as two service dependencies (a locator and an authorization service):

    public class ShellController : ControllerBase, IShellController
    {
        public ShellController([StateDependency("State")] StateValue<ShuttleState> state,
                               [ServiceDependency] IHttpContextLocatorService contextLocator,
                               [ServiceDependency] IAuthorizationService authService)
            : base(state, contextLocator, authService)
        {
            // code goes here
        }
}
Kevin Dostalek
A: 

Unit test frameworks use them extensively:

To do anything in nUnit or MSTest, you have to decorate methods with a [TestFixture] or [TestClass] attribute.

My favorite? MbUnit's [DataFixture] attribute: lets you seed test cases with specific test data either within the attribute directly or an external resource.

joshua.ewer
Those are class or method attributes, not method parameter attributes as he asked. A unit-testing example would be MbUnit's Column attribute (http://code.google.com/p/mb-unit/source/browse/trunk/v3/src/MbUnit/MbUnit/Framework/ColumnAttribute.cs)
Mauricio Scheffer