簡體   English   中英

C#依賴屬性,依賴於setter執行的正確順序

[英]C# Dependent properties, dependency on correct order of setter execution

我想知道如何處理C#中的依賴屬性。 我有以下簡化的類(我正在使用DevExpress XAF ):

public class Company
{
    public ContactMethods? PreferredContactMethod { get; set; }
    // Missing: Collection of Employees...
}

public class Employee
{
    private Company company;

    public Company Company
    {
        get
        {
            return this.company;
        }
        set
        {
            company = value;
            if (company != null && PreferredContactMethod == null)
            {
                PreferredContactMethod = company.PreferredContactMethod;
            }
        }
    }

    public ContactMethods? PreferredContactMethod { get; set; }
}

將公司分配給員工時,我將Employee.PreferredContactMethod設置為公司的PreferredContactMethod(為方便起見,以后可以更改)。


更新:

我想在初始化新員工時將Company.PreferredContactMethod用作默認值。 每個員工都獨立於公司存儲自己的ContactMethod。 以后對Company.PreferredContactMethod的更改不應更新Employee.PreferredContactMethod。 Employee.PreferredContactMethod為null是完全合法的(例如,如果顯式設置為user)。


非常簡單的代碼,當然這很好。 但是我認為這違反了Microsoft的《財產設計指南》

允許以任何順序設置屬性,即使這會導致臨時無效的對象狀態。

Company = A, PreferredContactMethod = null給出另一個結果,而不是PreferredContactMethod = null, Company = A

我認為我不能依靠屬性設置器的“正確”順序(例如,如果使用Automapper / Reflection),您如何處理這種情況? 我認為這並不少見。

謝謝!

您要確保不改變雇員與該公司本人具有相同的PreferredContactMethod,除非他本人具有指定的PreferredContancMethod。 擴展您的解決方案的一種方法是實施此方法,以像這樣更新此值:

public class Employee
{
    private Company company;
    public Company Company
    {
        get { return this.company; }
        set
        {
            this.company = value;
            UpdateCompanyPreferredContactMethod();
        }
    }

    private ContactMethods? preferredContactMethod;
    public ContactMethods? PreferredContactMethod 
    {
        get { return this.preferredContactMethod; }
        set
        {
            this.preferredContactMethod = value;
            UpdateCompanyPreferredContactMethod();
        }
    }

    private void UpdateCompanyPreferredContactMethod()
    {
        if (PreferredContactMethod == null)
        {
            PreferredContactMethod = company != null ?company.PreferredContactMethod : null;
        }
    }
}

但是,此解決方案非常脆弱,因為每次更改其中一個值時都必須更新此值。 相反,我實際上會這樣做:

public class Employee
{
    public Company Company { get; set; }

    private ContactMethods? preferredContactMethod;
    public ContactMethods? PreferredContactMethod 
    {
        get 
        { 
            if (this.preferredContactMethod != null)
            {
                return this.preferredContactMethod;
            }
            else if (this.Company != null)
            {
                return this.Company.PreferredContactMethod;
            }
            else
            {
                return null;
            }
        }
        set { this.preferredContactMethod = value; }
    }
}

您說要在初始化新Employee時將PreferredContactMethod用作默認值。 在這種情況下,您應該將Company傳遞給Employee的構造函數,然后進行設置:

public Employee(Company company) {
    // Null checks as appropriate
    this.company = company;
    this.PreferredContactMethod = company.PreferredContactMethod;
}

如果您還想按照自己的方式更改PreferredContactMethod,而您唯一的擔心是違反了設計准則,則可以使用Employee.SetCompany方法來表明該操作除了更改Company屬性之外還具有副作用。

我認為您需要在保存Employee對象之前分配它,因此任何順序都可以得到相同的結果。

嘗試這個;

if (IsNewRecord && Company != null && PreferredContactMethod == null)
    PreferredContactMethod = Company.PreferredContactMethod;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM