.NET MVC——在视图模型中使用组合
.NET MVC -- Using Composition in a View Model
我正在努力思考构图的概念。以前从未使用过它。我有一个 class 看起来像这样(变薄):
public class AccountProfile
{
public string AccountNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public void GetAccountProfile()
{
AccountNumber = "123456"; // eventual these will become values from the database
FirstName = "John";
LastName = "Smith";
}
}
然后,在我的视图模型中,我想要访问 AccountNumber、FirstName 和 LastName。我不想使用继承,因为这个视图模型需要访问多个外部的、不相关的 classes。到目前为止模型很简单:
public class AccountProfileViewModel
{
public AccountProfileViewModel() { }
}
这是我到目前为止所尝试的方法,none 是正确的:
public class AccountProfileViewModel
{
AP= new AccountProfile();
public AccountProfileViewModel() { }
}
那个(上面的)抛出多个错误并且无法编译。我也试过这个:
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() { }
}
这个(上面的那个)编译得很好,但是当我尝试使用它时它在控制器中抛出 运行 次错误:
model.AP.GetAccountProfile();
错误:{"Object reference not set to an instance of an object."}
我没主意了。谢谢!
你至少要初始化对象。
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() {
AP = new AccountProfile();
}
}
我认为你想要实现的是这样的:
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() { }
}
或者如果AccountProfileViewModel
真的需要AccountProfile
你可以
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel(AccountProfile profile) {
this.AP = profile;
}
}
在你的控制器中你可以做这样的事情
public class controller {
public ActionResult Index(){
var vm = new AccountProfileViewModel();
var ap = //Get accountProfile
vm.AP = ap;
return View(vm);
}
}
或者在您需要 AccountProfile
的例子中
public class controller {
public ActionResult Index(){
var ap = //Get accountProfile
var vm = new AccountProfileViewModel(ap);
return View(vm);
}
}
你想要 AccountProfileViewModel
有一个 AccountProfile
的实例,但你想在控制器中设置它。
那么在你看来你可以Model.AP.AccountNumber
例如
如果您在此 class 中需要对象引用,那么我个人的偏好是仅在需要时创建对象,如下所示:
public class AccountProfileViewModel
{
private AccountProfile _ap;
public AccountProfile AP
{
get { return _ap ?? (_ap = new AccountProfile()); }
set { _ap = value; }
}
}
如果您实际使用 yourObject.AP
那么它将创建一个引用/ return 现有的但是如果没有使用则没有创建引用。
我正在努力思考构图的概念。以前从未使用过它。我有一个 class 看起来像这样(变薄):
public class AccountProfile
{
public string AccountNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public void GetAccountProfile()
{
AccountNumber = "123456"; // eventual these will become values from the database
FirstName = "John";
LastName = "Smith";
}
}
然后,在我的视图模型中,我想要访问 AccountNumber、FirstName 和 LastName。我不想使用继承,因为这个视图模型需要访问多个外部的、不相关的 classes。到目前为止模型很简单:
public class AccountProfileViewModel
{
public AccountProfileViewModel() { }
}
这是我到目前为止所尝试的方法,none 是正确的:
public class AccountProfileViewModel
{
AP= new AccountProfile();
public AccountProfileViewModel() { }
}
那个(上面的)抛出多个错误并且无法编译。我也试过这个:
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() { }
}
这个(上面的那个)编译得很好,但是当我尝试使用它时它在控制器中抛出 运行 次错误:
model.AP.GetAccountProfile();
错误:{"Object reference not set to an instance of an object."}
我没主意了。谢谢!
你至少要初始化对象。
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() {
AP = new AccountProfile();
}
}
我认为你想要实现的是这样的:
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel() { }
}
或者如果AccountProfileViewModel
真的需要AccountProfile
你可以
public class AccountProfileViewModel
{
public AccountProfile AP { get; set; }
public AccountProfileViewModel(AccountProfile profile) {
this.AP = profile;
}
}
在你的控制器中你可以做这样的事情
public class controller {
public ActionResult Index(){
var vm = new AccountProfileViewModel();
var ap = //Get accountProfile
vm.AP = ap;
return View(vm);
}
}
或者在您需要 AccountProfile
public class controller {
public ActionResult Index(){
var ap = //Get accountProfile
var vm = new AccountProfileViewModel(ap);
return View(vm);
}
}
你想要 AccountProfileViewModel
有一个 AccountProfile
的实例,但你想在控制器中设置它。
那么在你看来你可以Model.AP.AccountNumber
例如
如果您在此 class 中需要对象引用,那么我个人的偏好是仅在需要时创建对象,如下所示:
public class AccountProfileViewModel
{
private AccountProfile _ap;
public AccountProfile AP
{
get { return _ap ?? (_ap = new AccountProfile()); }
set { _ap = value; }
}
}
如果您实际使用 yourObject.AP
那么它将创建一个引用/ return 现有的但是如果没有使用则没有创建引用。