The Power of Programming With Attributes

Nothing can compare to the Real Power of programming with attributes. Why, just one pair of square brackets and woosh – my object can be serialize to XML. Woosh – my object can persist to a database table. Woosh – there goes my object over the wire in a digitally signed SOAP payload. One day I expect to see a new item template in Visual Studio – the "Add New All Powerful Attributed Class" template: *

[Table]    
[
DataObject]
[
DataContract]    
[
Serializable]
[
TwoKitchenSinks]      
[
CLSCompliant(true)]        
[
DefaultProperty("Name")]
[
DefaultBindingProperty("Name")]
[
DebuggerStepThroughAttribute]
[
GuidAttribute("F0DD2CAA-2132-11DD-AC50-FE9355D89593")]
public class Person
{
    [
Column]        
    [
DataMember]        
    [
XmlAttribute]
    [
Browsable(true)]
    [
ReadOnly(false)]
    [
Category("Advanced")]
    [
Description("The person's name")]        
    
public string Name { get; set; }

    
// TODO: YOUR INSIGNIFIGANT BIZ LOGIC GOES HERE...
}

Which begs the question – could there ever be a way to separate attributes from the class definition?**

* Put down the flamethrower and step away - I'm kidding.

**This part was a serious question.

posted on Tuesday, May 13, 2008 10:41 PM by scott

Comments

Tuesday, May 13, 2008 10:05 PM by Kevin Dente

# re: The Power of Programming With Attributes

Put them in a separate XML config file?

I kid, I kid.

Seriously, though, attributes often seem like a violation of SOC.
Tuesday, May 13, 2008 11:19 PM by kokos

# re: The Power of Programming With Attributes

One simple way you can use today is partial classes: put the related attributes that contains only the type name and the actual content elsewhere. You can mix these however you want.

Or you can make a request for a new attribute in the next version of the platform:
[assembly: ApplyAttribute(Type=YourType,Attribute=YourAttribute)].

Or go crazy and put the attributes in xml and... go crazy :D
Tuesday, May 13, 2008 11:22 PM by kokos

# re: The Power of Programming With Attributes

Just wanted to add that the ApplyAttribute I mentioned should be understood by the compiler and put in the right place at compile time, so that GetCustomAttributes and related methods still work.
Tuesday, May 13, 2008 11:35 PM by Laurent

# re: The Power of Programming With Attributes

What about using .NET 3.5 partial methods ?
Tuesday, May 13, 2008 11:37 PM by Francis Norton

# re: The Power of Programming With Attributes

The nearest I've got to this experience so far was the realisation that I could add class attributes and even inheritance via partial classes.

What would you do with this feature? Would it be able to work across assemblies? Could you add attributes to other people's libraries?

AOP would be cool, and I can see something like Spec#.

And I can see lots of wizardry being required to stitch it all together again, to give a re-unified view in VS or Reflector.
Tuesday, May 13, 2008 11:38 PM by Christopher Steen

# Link Listing - May 13, 2008

Link Listing - May 13, 2008
Tuesday, May 13, 2008 11:39 PM by Christopher Steen

# Link Listing - May 13, 2008

Sharepoint Web Parts the Simple Way - Use User Controls [Via: janiver@microsoft.com ] ASP.NET Developer...
Wednesday, May 14, 2008 1:07 AM by Rob Paterson

# re: The Power of Programming With Attributes

Wouldn't a list of attributes separate from the source code just be an XML config file? Which is what attributes is trying to avoid I would imagine. I'm guessing a bit as I'm a Java programmer interpreting attributes as an 'annotation'.
Wednesday, May 14, 2008 1:37 AM by Lars Haupt-Hansen

# re: The Power of Programming With Attributes

Suppose you make an attribute [Template(name="PowerfulClass")] and then stored the Attribute templates in an XML document somewhere:

<Attributes>
<!-- something notation to include namespaces -->
<Templates>
<Template name="PowerfulTemplate">
<Table/>
<DataObject/>
<DefaultProperty name="Name"/>
</Template>
</Templates>

<!-- class specific goes here -->
<Classes>
<Class class="My.Demo.Foo">
</Class>
</Classes>
</Attributes>

Templates could be used for classes with the same behaviour and Class tags could be used for individual tweaking.

Of course you'd have to plugin at runtime so querying on attributes will read from the xml file instead from the assemblies directly. Not sure if that is possible though.
Wednesday, May 14, 2008 5:51 AM by scott

# re: The Power of Programming With Attributes

@Kevin: SoC violations are easy. Where attributes drive me nuts are in designer components. It's though to look at the source code when there are so many decorations on the class.

@kokos, @Laurent: I've thought about partial classes, partial methods, but it feels wrong. I don't like to use partials myself. I feel they should only be present when a code-generation tool is used.

@Lars: It hurts my eyes :) But, that was something along the lines of what I was thinking.

Wednesday, May 14, 2008 7:15 AM by Scott Seely

# re: The Power of Programming With Attributes

With KnownTypeAttribute (System.Runtime.Serialization/WCF), you can declare aspects outside of code since DataContract recognizes that you may not own the code that you need to serialize.

Ditto for exposing a COM+ service over WCF.

See the following:
KnownTypesAttribute: http://msdn.microsoft.com/en-us/library/ms730167.aspx

Details on COM+ and how to integrate with WCF via the SvcConfigEditor tool: http://msdn.microsoft.com/en-us/library/ms732009.aspx


Wednesday, May 14, 2008 12:51 PM by Oran

# re: The Power of Programming With Attributes

I wonder if you can use PostSharp and/or Laos to post-compile the attributes in after the fact based on external config.
Wednesday, May 14, 2008 2:23 PM by Jared Parsons

# re: The Power of Programming With Attributes

The best way I can think of is not actually removing them from the class but instead adding them to a partial class. That would take away them being directly on the class you are editing but won't actually remove them from the class.

The other downside is this will only work for class attributes and won't help for methods/properties/fields/etc ...
Friday, May 30, 2008 8:56 AM by Ben Scheirman

# re: The Power of Programming With Attributes

This solution would likely be full of gotchas, but they could extend the partial syntax to work for class members as well.

[Awesome]
public partial class Foo
{
[UberMethod]
[SecurityAction...]
public string GetStuff();

[ValidateNotEmpty]
public string Yuck
{
get;
}
}

this would all be syntactic sugar over our current CLR.