正确使用对象属性

时间:2009-04-20 15:43:17

标签: c# .net oop properties

下面是我创建的一个类,用于跟踪我的美化数据输入和检索应用程序中的当前Person。一旦他们选择了一个人,它就会调用constrtuctor,然后调用数据库来填充所有其他信息。此外,在整个计划中,他们将能够改变各个领域。

考虑到这一点,我是否正确设置了以下设置?我对属性并不熟悉,并且使用对象来存储多个表单中的数据,并且非常感谢任何见解。

谢谢!

class CurrentPerson
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public string SuffixID { get; set; }
    public string TitleID { get; set; }
    public string SocialSn { get; set; }
    public string BirthDate { get; set; }
    public string Gender { get; set; }
    public string DlNumber { get; set; }
    public string DlStateID { get; set; }
    public string PrimaryRace { get; set; }
    public string SecondaryRace { get; set; }
    public string EmailAddress { get; set; }
    public string MaritalStatus { get; set; }
    public string InsertProgram { get; set; }
    public string InsertUserID { get; set; }
    public string UpdateProgram { get; set; }
    public string UpdateUserID { get; set; }
    public string LockID { get; set; }

    public int PersonID { get; set; }
    public int ClientID { get; set; }
    public int ResidencyCountyID { get; set; }
    public int ResponsibilityCountyID { get; set; }

    public bool HispanicOriginFlag { get; set; }
    public bool CitizenFlag { get; set; }
    public bool VeteranFlag { get; set; }

    public DateTime DeathDate { get; set; }
    public DateTime InsertDateTime { get; set; }
    public DateTime UpdateDateTime { get; set; }

    // Put the default Constructor back in
    public CurrentPerson(){}

    // Custom Constructor that needs the PersonID
    public CurrentPerson(int pID)
    {
        PersonID = pID;

        // Methods to get rest of data here
    }
}

3 个答案:

答案 0 :(得分:3)

是的,看起来不错。 你可以,顺便说一下,在get / set上设置访问权限,使其只能公开读/写

public DateTime DeathDate
{
    get;
    private set;
}

答案 1 :(得分:1)

这在技术上很好。他们都被宣布完美。

然而,通常,对于数据库应用程序,您不希望使用自动属性,因为属性设置器通常是进行某些验证的好地方,并且可能将属性/对象标记为“脏”并且需要保存某种。

答案 2 :(得分:1)

auto属性始终是get和set,因此您无法控制属性集(将实例标记为脏或其他)。因此,虽然这只是一个可接受的类作为数据实体,但我通常发现自动属性很少真正适用。