【问题标题】:Unsure how to model and architecture this one不确定如何建模和架构这个
【发布时间】:2013-05-02 02:31:08
【问题描述】:

我在这里遇到了问题,我正用头撞墙。

我有一个对象是我模型的一部分。让我们称它为 MyObject。它具有以下属性:

  • 属性1
  • 属性2
  • 属性3
  • Property4.1
  • Property4.2
  • Property4.3

前三个属性是独立的。 4th,5th,6th同属一个集合,我们称之为Properties4Set。

Properties4Set 依赖于 property1、2 和 3。 因此,如果这些值中的任何一个发生变化,则必须重新计算整个集合。

计算方式如下: 我有一个文件,其中有 Property1 和默认 Properties4Set 之间的映射。 所以基于 Property1 我必须加载这个默认集,然后应用 Property2 和 3 的规则。

我这里有一个 mvvm 应用程序。我创建了一个存储库,可以创建我的域对象并使用正确的值返回它。 一旦我想更改 Property1,我的问题就开始了。因为这意味着我需要一个新的默认 Properties4Set。

目前我看到了两种处理方法。

选项1: 在存储库中实现“GetNewDefaultSet(property1)”方法并将存储库注入 MyObject。一旦 Property1 发生变化,从存储库中加载一个新集合,并根据 2 和 3 重新计算值。

选项2: 在存储库中实现“GetAllDefaultSets()”方法,并将 Properties4Sets 的整个集合注入 MyObject。一旦 Property1 发生变化,从列表中加载适当的集合。

我首先选择了选项 1,但后来读到如果您将存储库注入到您的域对象中,这是一个糟糕的设计。他们不应该关心如何获取他们需要的数据。 我对选项 2 的问题是,如果对象一次只需要一个集合,那么注入整个集合列表似乎有点过头了。

那么,如何处理呢?你看到其他选择了吗?

编辑

那里没有具体的实现示例,这很糟糕。

好的,这里有一些代码:

/// <summary>
/// Just a DTO for how the set is actually stored, can be fetched from the repository
/// </summary>
public class MySetMapping
{
    private int value1;
    private float value4;
    private float value5;
    private float value6;

    public MySetMapping(int value1, float value4, float value5, float value6)
    {
        this.value1 = value1;
        this.value4 = value4;
        this.value5 = value5;
        this.value6 = value6;
    }

    public int Value1
    {
        get { return this.value1; }
    }

    public float Value4
    {
        get { return this.value4; }
    }

    public float Value5
    {
        get { return this.value5; }
    }

    public float Value6
    {
        get { return this.value6; }
    }
}

他是集合类:

public class MySet
{
    private float value4;
    private float value5;
    private float value6;

    public MySet(float value4, float value5, float value6)
    {
        this.value4 = value4;
        this.value5 = value5;
        this.value6 = value6;
    }

    public float Value4
    {
        get { return this.value4; }
    }

    public float Value5
    {
        get { return this.value4; }
    }

    public float Value6
    {
        get { return this.value4; }
    }
}

这里是我需要的实际对象:

/// <summary>
/// the main business object, has actually more properties
/// </summary>
public class MyObject
{
    private int value1;
    private int value2;
    private int value3;
    private MySet mySet;

    public MyObject(int value1, int value2, int value3)
    {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;

        //needs something to get the correct set for the three values
    }

    public int Value1
    {
        get { return this.value1; }
        set
        {
            this.value1 = value;
            //adjust set
        }
    }

    public int Value2
    {
        get { return this.value2; }
        set
        {
            this.value2 = value;
            //adjust set
        }
    }

    public int Value3
    {
        get { return this.value3; }
        set
        {
            this.value3 = value;
            //adjust set
        }
    }

    public float Value41
    {
        get
        {
            return this.mySet.Value4;
        }
    }

    public float Value42
    {
        get
        {
            return this.mySet.Value5;
        }
    }

    public float Value43
    {
        get
        {
            return this.mySet.Value6;
        }
    }

也许你现在可以更好地理解它。

【问题讨论】:

  • 如果您有多个相互依赖且有些相关的属性,这对您来说是不是敲响了警钟?它不应该是一个单独的类吗?一个很好的例子是地址 - 街道、城市等......您不会将其单独存储为用户的字段,对吗?是不是和你的场景很像?
  • 实际上我已经将 4.1、4.2、4.3 分成了另一个类。但这并没有改变我的问题。因为现在我仍然需要从某个地方为 property1 获取正确的对象。

标签: c# architecture


【解决方案1】:

那里没有具体的实现示例,这很糟糕。很难想象什么是 Property4Set,或者它是如何从存储库中获取数据的。当 Property2 或 3 改变时会发生什么。您对默认值的期望是什么。

从目前的信息来看,还是看情况,我觉得builder模式对你有好处。

例子:

public class MyObjectBuilder{
  // inject the repository
  public MyObjectBuilder(IMyObjectBuilderRepository repository) 

  private Property1 property1;
  public void SetProperty1(Property1 prop){}

  public MyObject GetMyObject(){
    MyObject result = new MyObject(); // optional to use constructor injection
    result.Property1 = this.property1;

    // based on this.property1, set the Property4Set object
    Property4Set property4 = repository.GetProperty4Set(this.property1);

    result.Property4 = property4;
    return result;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    相关资源
    最近更新 更多