Kilhoffer
Kilhoffer

Reputation: 32914

Is there a practical example of how they have used attributes on method parameters in .NET?

I know it's possible, and I've seen simple examples in the documentation, but are they being used in the wild?

I use attributes at the class and method level all the time, but have never used them on method parameters. What are some real-world examples, and the reasons for the usage?

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.

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!

Upvotes: 8

Views: 1084

Answers (6)

joshua.ewer
joshua.ewer

Reputation: 3962

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.

Upvotes: 1

Kevin Dostalek
Kevin Dostalek

Reputation: 746

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
        }
}

Upvotes: 2

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99750

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

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1502246

(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

Upvotes: 5

Brian Vallelunga
Brian Vallelunga

Reputation: 10201

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.

Upvotes: 3

leppie
leppie

Reputation: 117280

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.

Upvotes: 10

Related Questions