implementing default values in a Model
asp.net-mvc, c#
Solution
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 */
}
Problem
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?