【问题标题】:.NET MVC -- Use a class as the model?.NET MVC——使用类作为模型?
【发布时间】:2015-07-08 17:03:18
【问题描述】:

在我的 MVC 应用程序中,几个视图模型几乎相同。而不是每次都复制模型,我想我可以只创建一个类。我不确定的是如何在每个模型中包含该类。

例如,假设我的模型如下所示:

public class AccountProfileViewModel
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public AccountProfileViewModel() { }
}

但我知道 FirstName 和 LastName 将在许多模型中广泛使用。因此,我创建了一个包含 AccountProfile 的类库:

namespace foobar.classes
{
    public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
}

回到模型中,我将如何包含类,以便 FirstName 和 LastName 在模型中,但不是专门创建的?

【问题讨论】:

    标签: c# asp.net-mvc class asp.net-mvc-viewmodel


    【解决方案1】:

    创建一个基类,然后使用继承,您可以访问这些公共属性。

    public class AccountProfile
        {
            public string FirstName { get; set; }
            public string Lastname { get; set; }
        }
    
    public class OtherClass : AccountProfile 
        {
            //here you have access to FirstName and Lastname by inheritance
            public string Property1 { get; set; }
            public string Property2 { get; set; }
        }
    

    【讨论】:

    • 不知道为什么我没有想到继承。太完美了!
    【解决方案2】:

    除了使用继承之外,您还可以使用组合来实现相同的目标。

    Prefer composition over inheritance

    应该是这样的:

    public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
    
    public class AccountProfileViewModel
    {
        // Creates a new instance for convenience
        public AnotherViewModel() { Profile = new AccountProfile(); }
    
        public AccountProfile Profile { get; set; }
    }
    
    public class AnotherViewModel
    {
        public AccountProfile Profile { get; set; }
    
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
    

    【讨论】:

    • 好的,好的。这更多的是我的想法。我会阅读您链接的主题。有很多东西要消化。
    • 问题:在上面的第三个示例中,我将如何访问 FirstName? Profile.FirstName 不适合我。
    • 添加构造函数来初始化属性可能会为您省去一些麻烦public AnotherViewModel() { Profile = new AccountProfile(); }
    • @CaseyCrookston 这可能是 JamieD77 指出的情况,个人资料参考可能为空?
    【解决方案3】:

    您也可以实现类似IProfileInfo 的接口,这可能更可取,因为类可以实现多个接口但只能从一个类继承。将来,您可能希望在需要继承它的代码中添加一些其他统一方面,但您可能不一定希望某些类继承自具有 Firstname 和 Lastname 属性的基类。如果您使用的是 Visual Studio,它会自动为您实现接口,因此无需付出额外的努力。

    public class AccountProfile : IProfileInfo
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
    
    public interface IProfileInfo 
    {
        string Firstname {get;set;}
        string Lastname {get;set;}
    }
    

    【讨论】:

    • 哇,好的。很多很棒的答案。还有很多要学的。我要去了解更多关于类和接口之间的区别。非常感谢。
    • 这是对接口与类问题stackoverflow.com/a/1913185/1370442的一个很好的答案@
    【解决方案4】:

    这太长了,无法发表评论,所以这只是基于您已经收到的答案的评论。

    只有当您创建的新类基本上是相同类型的对象,只是略有不同时,您才应该使用继承。如果您尝试将 2 个单独的类关联在一起,则应使用属性方法。继承类似于

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
    }
    
    public class Teacher : Person 
    {
        public string RoomNumber { get; set; }
        public DateTime HireDate { get; set; }
    }
    
    public class Student : Person
    {
        public string HomeRoomNumber { get; set; }
        public string LockerNumber { get; set; }
    }
    

    组合应该这样使用。

    public class Address 
    {
        public string Address1 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
    }
    
    public class StudentViewModel
    {
        public StudentViewModel ()
        {
            Student = new Student();
            Address = new Address();
        }
        public Student Student { get; set; }
        public Address Address { get; set; }
    
    }
    

    【讨论】:

    • 谢谢 JamieD77。这很有意义。
    猜你喜欢
    • 2015-02-11
    • 1970-01-01
    • 2011-06-20
    • 2010-09-08
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多