C# 在模型中实现默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15901892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
implementing default values in a Model
提问by A Bogus
In a C# MVC application, I have a Model, Customer, which can be retrieved from the database, or created as new.
在 C# MVC 应用程序中,我有一个模型 Customer,它可以从数据库中检索,也可以创建为新的。
If I am creating a new Customer what is the proper way to set default values (such as CusotmerLevel defaults to 1)?
如果我正在创建一个新客户,设置默认值的正确方法是什么(例如 CusotmerLevel 默认为 1)?
Should I have different constructors for a new employee and an employee retrieved from the database, or some other way?
我应该为新员工和从数据库中检索的员工使用不同的构造函数,还是以其他方式?
采纳答案by Brad Christie
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.
假设它是一个 POCO,我总是发现构造函数可以很好地建立默认值。我通常会借此机会宣布诸如CreatedDate
. 当它从数据库中检索时,公共属性无论如何都会被数据库值覆盖。
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:
而且,如果您使用的是 C# 6.0,请查看自动属性初始值设定项:
public class Customer
{
public Int32 Id { get; set; } = 1;
public Int32 CustomerLevel { get; set; }
/* other properties */
}
回答by AlexanderBrevig
Use the default constructor, all the ORMs I've tried set values after the constructor is run so it should not interfere.
使用默认构造函数,在构造函数运行后我尝试过的所有 ORM 都设置了值,因此它不应该干扰。
One alternative method would be:
一种替代方法是:
var customer = new Customer() { CustomerLevel=1 };
回答by Omu
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});
}
回答by JMan
You can use the Factory
pattern to create a new customer. This way you can give the Factory the responsability of setting the correct default values.
您可以使用该Factory
模式来创建新客户。通过这种方式,您可以让工厂负责设置正确的默认值。
If not usable i would set it in the default constructor.
如果不可用,我会将它设置在默认构造函数中。
回答by SMS
Use a common constructor for creating new employee and retrieving the existing ones.
使用通用构造函数来创建新员工并检索现有员工。
For default values use optional arguments.
对于默认值,使用可选参数。
回答by Arwin
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.
这应该总是给出数据库值,除非不存在数据库值。但是一旦新对象持久化到数据库中,从那时起它就应该在数据库中具有默认值。