【问题标题】:Update a class property based on another property's value in the setter根据 setter 中另一个属性的值更新类属性
【发布时间】:2017-04-27 16:57:22
【问题描述】:

我有两个模型类:

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int AddressId { get; set; }
    public Address AddressInfo { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string streetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

如果Person.AddressInfo.AddressId 中的任何值得到更新,如何自动更新Person.AddressId

【问题讨论】:

  • 如果Person 已经引用了Address,为什么还需要AddressId
  • 一个更好的问题可能是您为什么要将此 AddressId 属性存储两次?
  • 为什么不让AddressId 返回AddressInfo.AddressId 的值?
  • 您的设置看起来很奇怪。您同时拥有地址 ID 和地址信息。除非这是针对实体框架...以及更改 any 属性如何影响地址 ID?
  • 您希望如何处理AddressInfonull 并且有人试图设置Person.AddressId 的情况?

标签: c# getter-setter automatic-properties


【解决方案1】:

这个呢?

public class Person
{
    public int PersonId { get; set; }
    public string Name { get; set; }
    public int AddressId 
    { 
        get{ return AddressInfo?.AddressId ?? 0 } 
        set{ AddressInfo?.AddressId = value; }
    }
    public Address AddressInfo { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string streetName { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

这使用 AddressInfo 作为后台存储

【讨论】:

    【解决方案2】:

    您可以简单地将以下内容写入 Person 类:

     public int AddressId{
        get{return this.AddressInfo?.AddressId ?? 0;}
        set{this.AddressInfo?.AddressId= value;}
     }
    

    或者写得更好:

     public int? AddressId{
        get{return this.AddressInfo?.AddressId;}
        set{this.AddressInfo?.AddressId= value;}
     }
    

    【讨论】:

    • 可能不应该允许属性获取器抛出异常...(如果AddressInfo 为空)
    【解决方案3】:

    下面的代码可以帮助你,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                Address test = new Address();
                test.AddressId = 0;
                test.City = "xyzzzzzzzzzzzzzzz";
                test.streetName = "xyz";
                test.State = "xyzzzzzzzzzzzzzzzxxxxxxxxxxxxxxxxxxx";
    
                Person ptest = new Person
                {
                    PersonId = 1,
                    Name = "test1",
                    AddressInfo = test,
                    AddressId = 5,
                };
            }
        }
    
        public class Address
        {
            public int AddressId { get; set; }
            public string streetName { get; set; }
            public string City { get; set; }
            public string State { get; set; }
        }
        public class Person
        {
            public int PersonId { get; set; }
            public string Name { get; set; }
            public int AddressId { 
    
                get{ return AddressInfo != null ? AddressInfo.AddressId : 0;}
                set { AddressInfo.AddressId = value; }
        }
            public Address AddressInfo { get; set; }
        }
    }
    

    在给addressid赋值之前,确保addressinfo不为null,如果它为null,则将数据赋值给addressinfo,那么你可以更新值,否则你会得到对象引用错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-06
      • 2020-03-05
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多