18

In a C# MVC application, I have a Model, Customer, which can be retrieved from the database, or created as new.

If I am creating a new Customer what is the proper way to set default values (such as CusotmerLevel defaults to 1)?

Should I have different constructors for a new employee and an employee retrieved from the database, or some other way?

6 Answers 6

46

Assuming it's a POCO, I've always found the constructor is fine for establishing default values. I usually take that opportunity to declare things like CreatedDate. When it's retrieved from the database, the public properties will be overridden by the database values anyways.

public class Customer
{
    public Int32 Id { get; set; }
    public Int32 CustomerLevel { get; set; }
    /* other properties */

    public Customer()
    {
        this.CustomerLevel = 1;
    }
}

Update

And, if you're using C# 6.0 check out auto-property initializers:

public class Customer
{
    public Int32 Id { get; set; } = 1;
    public Int32 CustomerLevel { get; set; }
    /* other properties */
}
Sign up to request clarification or add additional context in comments.

2 Comments

Great answer and great example! Thanks Brad!
Just as a matter of maintainability I'd define constants at the top of the class and use those in the setter defaults - saves a lot of hunt and peck for changes if the class is large.
2

You can use the Factorypattern to create a new customer. This way you can give the Factory the responsability of setting the correct default values.

If not usable i would set it in the default constructor.

Comments

2

In the constructor is fine, and you can give default values also as arguments in the constructor, as mentioned above.

If you're using nullable values, however, which I find is very helpful with model-first database work so far, you can also use this:

private int? workWeekInHours;

public int? WorkWeekInHours
{
    get { return workWeekInHours ?? 40; }
    set { workWeekInHours = value; }
}

This should always give the Database value, unless no database value exists. But once the new object is persisted to the database, it should have the default value in the database from then on.

Comments

1

Use the default constructor, all the ORMs I've tried set values after the constructor is run so it should not interfere.

One alternative method would be:

 var customer = new Customer() { CustomerLevel=1 };

Comments

1

you should have a viewmodel for the create new view

so you can do it like this:

public ActionResult Create()
{
     return View(new CustomerInput{ CustomerLevel = 1});
}

1 Comment

Now you're relying on every action that creates a customer to establish default values.
1

Use a common constructor for creating new employee and retrieving the existing ones.

For default values use optional arguments.

http://msdn.microsoft.com/en-us/library/dd264739.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.