首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不知道该怎么建模和架构

不知道该怎么建模和架构
EN

Stack Overflow用户
提问于 2013-05-01 12:29:11
回答 1查看 60关注 0票数 1

我这里出了点问题,我的头撞到墙上了。

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

  • Property1
  • Property2
  • Property3
  • Property4.1
  • Property4.2
  • Property4.3

前三个属性是独立的。第四,第五和第六属于一起,他们是一个集合,让我们称之为Properties4Set。

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

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

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

目前,我看到了两种解决方案。

Option1:将"GetNewDefaultSet(property1)“方法实现到存储库中,并将存储库注入MyObject。一旦Property1更改,从存储库加载一个新集,并根据2和3重新计算值。

Option2:将"GetAllDefaultSets()“方法实现到存储库中,并将整个Properties4Sets集合注入MyObject。一旦Property1更改,就从列表中加载适当的集。

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

那怎么处理呢?你觉得还有别的选择吗?

编辑

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

下面是一些代码:

代码语言:javascript
复制
/// <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; }
    }
}

他是固定班:

代码语言:javascript
复制
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; }
    }
}

在这里,我需要的实际对象:

代码语言:javascript
复制
/// <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;
        }
    }

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-01 13:10:22

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

从目前的信息来看,仍然取决于情况,我认为构建者模式对你有好处。

示例:

代码语言:javascript
复制
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;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16318076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档