0

Is it possible to default mutiple value by only one related attribute? I find setting it row by row is a bit boilerish.

public class FooBarCounter{
        [Key]
        public int id{ get; set; }
        [DefaultValue(0)]
        public int FooCounter1 { get; private set; }
        [DefaultValue(0)]
        public int FooCounter2 { get; private set; }

        [DefaultValue(5)]
        public int BarCounter1 { get; private set; }
        [DefaultValue(5)]
        public int BarCounter2 { get; private set; }        
    }

Edit:

So the [Attrib] is in every second row, i would rather like something like this:

[Authorize]
public class MyFullyAuthorizedClass : Controller {
   //props, methoods etcetc
}

Here the [Authorize] affects the entire class, like if i would put it before every methood, now thats not the best either, since i have multiple defaults, but it's only 1 line, not a boiler in every secont line. Hope i'm clearer now.

Any ideas?

MrKekson
  • 720
  • 6
  • 18
  • What do you mean by "one related attribute"? – DavidG Oct 09 '15 at 11:10
  • afaik, you need to construct an instance. In case you want 0 for int, no need for the attribute. but if you want to have various values for various properties, you will have to do it one by one. if you are looking for a single place where you can change the all the values in one go, Perttu's answer would hold. Also i would like to mention, if you don't like attributes, you can use c# 6's property initializer. – blogbydev Oct 09 '15 at 11:11
  • I'm not entirely sure what was asked; the question title is "default value for multiple properties" and I tried to answer that. – Perttu Haliseva Oct 09 '15 at 11:31
  • i mean like how you can define [Authorize] before the class definition, and it effect the entire class. My point would be that in a larger class, i could define them together, so i dont need to have the [DefaultValue(x)] in every second line. – MrKekson Oct 09 '15 at 11:31
  • 1
    Have you considered setting defaults in a constructor instead of Attributes? – Indregaard Oct 09 '15 at 12:09
  • Yes, i also considered ef's fluent api( not in ef6 yet, 7 will has it tho ), but currently most of the configuration is in [attrib] form, so i would prefet to keep it together in a place, not in 2-3 separated places. – MrKekson Oct 09 '15 at 12:36

2 Answers2

1

The DefaultValue attribute is valid on all targets - so you can set it at the class level if you like, and write code that looks for it and reacts to it.

It's unlikely however that any Entity Framework implementations look for it and apply it - it's not part of the EF spec.

Community
  • 1
  • 1
mike mckechnie
  • 844
  • 8
  • 14
  • Thx for pointing it out, i thought thos come from ef but using System.ComponentModel.DataAnnotations would make no sense then. I'ts not a full answer, but i started to think that it's not possible to group props under an attrib, except if i do it on the class level, so i can't use it in a object to define mutiple default "groups". I'll accept it as an answer if there will be no better one til mondany tho. – MrKekson Oct 09 '15 at 13:39
  • 1
    If you are using code-first migrations with Entity Framework, you can configure defaults in the database as [here](http://stackoverflow.com/a/27920032/2831705). You could also use the class-level attribute and write a static helper that can be called from any constructor to apply any declared defaults. – mike mckechnie Oct 10 '15 at 17:57
  • 1
    I do solved if with 1 class wide default, and the rest from the constructor. Also we will be able to do it from the modelbuilder of ef from EF7. – MrKekson Oct 12 '15 at 13:31
-1
public class FooBarCounter{
    private const int DEFAULTVALUE = 0;

    [DefaultValue(DEFAULTVALUE)]
    public int FooCounter1 { get; private set; }
    [DefaultValue(DEFAULTVALUE)]
    public int FooCounter2 { get; private set; }
    // and so on ...

}
Perttu Haliseva
  • 530
  • 5
  • 16