这是一个合适的MonoState设计吗?

时间:2022-09-25 07:37:04

I have a Person Class and based on some help I received in this post.

我有一个人类,并根据我在这篇文章中收到的一些帮助。

MonoState, Singleton, or Derived Forms: Best approach for CRUD app?

MonoState,Singleton或Derived Forms:CRUD应用程序的最佳方法?

I have a CurrentPerson class around(??) it and I access the data thru that.

我有一个CurrentPerson类(??)它,我通过它访问数据。

I think I understand how this is supposed to work but it seems that I don't have it quite right.

我想我明白这应该如何工作,但似乎我没有这么做。

Questions:

  1. For starters, shouldn't Person and all it's member variables be private?

    首先,不应该将Person及其所有成员变量设为私有?

  2. Why is Person, when declared in CurrentPerson, static? Is that right?

    为什么Person在CurrentPerson中声明时是静态的?是对的吗?

  3. Person actually has child objects Address, Enrollment, and CaseNote. How do I incorporate them? A CurrentEnrollment wrapper around Enrollment?

    Person实际上有子对象Address,Enrollment和CaseNote。我如何加入它们?注册周围的CurrentEnrollment包装器?

I am fairly new to OOP as an applied science so some of these concepts are hard for me to visualize.

作为一门应用科学,我对OOP相当陌生,因此我很难想象其中一些概念。

The code is long, I apologize.

代码很长,我道歉。


class Person
{
    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 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 BirthDate { get; set; }
    public DateTime DeathDate { get; set; }
    public DateTime InsertDateTime { get; set; }
    public DateTime UpdateDateTime { get; set; }



}

class CurrentPerson
{
    public static Person Person { get; set; }

    public string FirstName
    {
        get { return CurrentPerson.Person.FirstName; }
        set { CurrentPerson.Person.FirstName = value; }
    }

    public string LastName
    {
        get { return CurrentPerson.Person.LastName; }
        set { CurrentPerson.Person.LastName = value; }
    }

    public string MiddleName
    {
        get { return CurrentPerson.Person.MiddleName; }
        set { CurrentPerson.Person.MiddleName = value; }
    }

    public string SuffixID
    {
        get { return CurrentPerson.Person.SuffixID; }
        set { CurrentPerson.Person.SuffixID = value; }
    }

    public string TitleID
    {
        get { return CurrentPerson.Person.TitleID; }
        set { CurrentPerson.Person.TitleID = value; }
    }

    public string SocialSn
    {
        get { return CurrentPerson.Person.SocialSn; }
        set { CurrentPerson.Person.SocialSn = value; }
    }

    public string Gender
    {
        get { return CurrentPerson.Person.Gender; }
        set { CurrentPerson.Person.Gender = value; }
    }

    public string DlNumber
    {
        get { return CurrentPerson.Person.DlNumber; }
        set { CurrentPerson.Person.DlNumber = value; }
    }

    public string DlStateID
    {
        get { return CurrentPerson.Person.DlStateID; }
        set { CurrentPerson.Person.DlStateID = value; }
    }

    public string PrimaryRace
    {
        get { return CurrentPerson.Person.PrimaryRace; }
        set { CurrentPerson.Person.PrimaryRace = value; }
    }

    public string SecondaryRace
    {
        get { return CurrentPerson.Person.SecondaryRace; }
        set { CurrentPerson.Person.SecondaryRace = value; }
    }

    public string EmailAddress
    {
        get { return CurrentPerson.Person.EmailAddress; }
        set { CurrentPerson.Person.EmailAddress = value; }
    }

    public string MaritalStatus
    {
        get { return CurrentPerson.Person.MaritalStatus; }
        set { CurrentPerson.Person.MaritalStatus = value; }
    }

    public string InsertProgram
    {
        get { return CurrentPerson.Person.InsertProgram; }
        set { CurrentPerson.Person.InsertProgram = value; }
    }

    public string InsertUserID
    {
        get { return CurrentPerson.Person.InsertUserID; }
        set { CurrentPerson.Person.InsertUserID = value; }
    }

    public string UpdateProgram
    {
        get { return CurrentPerson.Person.UpdateProgram; }
        set { CurrentPerson.Person.UpdateProgram = value; }
    }

    public string UpdateUserID
    {
        get { return CurrentPerson.Person.UpdateUserID; }
        set { CurrentPerson.Person.UpdateUserID = value; }
    }

    public string LockID
    {
        get { return CurrentPerson.Person.LockID; }
        set { CurrentPerson.Person.LockID = value; }
    }

    public int PersonID
    {
        get { return CurrentPerson.Person.PersonID; }
        set { CurrentPerson.Person.PersonID = value; }
    }

    public int ClientID
    {
        get { return CurrentPerson.Person.ClientID; }
        set { CurrentPerson.Person.ClientID = value; }
    }

    public int ResidencyCountyID
    {
        get { return CurrentPerson.Person.ClientID; }
        set { CurrentPerson.Person.ClientID = value; }
    }

    public int ResponsibilityCountyID
    {
        get { return CurrentPerson.Person.ResponsibilityCountyID; }
        set { CurrentPerson.Person.ResponsibilityCountyID = value; }
    }

    public bool HispanicOriginFlag
    {
        get { return CurrentPerson.Person.HispanicOriginFlag; }
        set { CurrentPerson.Person.HispanicOriginFlag = value; }
    }

    public bool CitizenFlag
    {
        get { return CurrentPerson.Person.CitizenFlag; }
        set { CurrentPerson.Person.CitizenFlag = value; }
    }

    public bool VeteranFlag
    {
        get { return CurrentPerson.Person.VeteranFlag; }
        set { CurrentPerson.Person.VeteranFlag = value; }
    }

    public DateTime BirthDate
    {
        get { return CurrentPerson.Person.BirthDate; }
        set { CurrentPerson.Person.BirthDate = value; }
    }

    public DateTime DeathDate
    {
        get { return CurrentPerson.Person.DeathDate; }
        set { CurrentPerson.Person.DeathDate = value; }
    }

    public DateTime InsertDateTime
    {
        get { return CurrentPerson.Person.InsertDateTime; }
        set { CurrentPerson.Person.InsertDateTime = value; }
    }

    public DateTime UpdateDateTime
    {
        get { return CurrentPerson.Person.UpdateDateTime; }
        set { CurrentPerson.Person.UpdateDateTime = value; }
    }


}

P.S. If Daniel Brückner happens to read this please take no offense as I am not second guessing your answer; I just need deeper clarification on some items in order to properly understand the use of monostate in my app.

附:如果丹尼尔布鲁克纳碰巧读到这个,请不要冒犯,因为我不是第二次猜测你的答案;我只需要对某些项目进行更深入的澄清,以便在我的应用中正确理解monostate的使用。

1 个解决方案

#1


Regarding whether the fields should be private, that's just the new (C# 3.0?) way of declaring properties without having a backing variable. A common variation is

关于字段是否应该是私有的,这只是新的(C#3.0?)方式来声明属性而没有支持变量。一个常见的变化是

public string SomeProperty { get; protected set }

'Course, as soon as you want to do something like validation in the setter this doesn't work and you have to go back to writing setters and getters, and declaring a backing variable.

“当然,只要你想在setter中做一些像验证这样的事情就不行了,你必须回去编写setter和getter,并声明一个支持变量。

I'm not sure what the intent of the static CurrentPerson.Person() method is, or how it gets set.

我不确定静态CurrentPerson.Person()方法的意图是什么,或者它是如何设置的。

For the third question, if I understand you right you'd give the Person class a property of type Address and access it something like this:

对于第三个问题,如果我理解你正确的话,你会给Person类一个Address类型的属性并访问它,如下所示:

Console.WriteLine(somePerson.HomeAddress.City);

#1


Regarding whether the fields should be private, that's just the new (C# 3.0?) way of declaring properties without having a backing variable. A common variation is

关于字段是否应该是私有的,这只是新的(C#3.0?)方式来声明属性而没有支持变量。一个常见的变化是

public string SomeProperty { get; protected set }

'Course, as soon as you want to do something like validation in the setter this doesn't work and you have to go back to writing setters and getters, and declaring a backing variable.

“当然,只要你想在setter中做一些像验证这样的事情就不行了,你必须回去编写setter和getter,并声明一个支持变量。

I'm not sure what the intent of the static CurrentPerson.Person() method is, or how it gets set.

我不确定静态CurrentPerson.Person()方法的意图是什么,或者它是如何设置的。

For the third question, if I understand you right you'd give the Person class a property of type Address and access it something like this:

对于第三个问题,如果我理解你正确的话,你会给Person类一个Address类型的属性并访问它,如下所示:

Console.WriteLine(somePerson.HomeAddress.City);