3层:跨层传输数据

时间:2014-05-29 08:58:26

标签: c# oop 3-tier

我有几个与特定产品相关的视觉工作室项目。该产品是3层应用程序,例如拥有数据库,工作流服务器和应用服务器+ Web服务。

现在让我们说这个产品或软件与水果和水果箱的交易有关。我正在做的是,当用户创建或组装一个水果箱时,我正在创建一个FruitCrate的对象,然后我不是调用带有几个参数的方法,而是创建FruitCrate对象并将其发送到DataAccess Project,工作流项目和Web服务而不是参数。

现在让我们说FruitCrate类或对象看起来像这样,

Public Class FruitCrate 
{ 
Public string NumberOfFruits {get;set;}
Public string NameOfFruits {get;set;}
Public string ClientName{get;set;}
Public string PaymentDetails{get;set;}
.... let say 20 - 30 more properties added
}

现在在dataAccess中我创建了FruitCrate的对象并添加了这样的所有值,

FruitCrate fc = new FruitCrate();
fc.NameOfFruits = "alksdjaslkdj";
...
fc.PropertyNumber30 = "asdasdasd";

但是,当我将此对象发送到工作流程时,就像这样,就像只发送10个属性一样

FruitCrate fc = new FruitCrate();
fc.NameOfFruits = "alksdjaslkdj";
...
fc.PropertyNumber10 = "asdasdasd";

作为中间开发者,我不确定这是否是正确的方法?也有人可以建议我任何资源,我可以了解更多关于这种类型的编码我只是困惑搜索什么来改进这种类型的编码或应用程序架构。

编辑

请尝试理解我只添加了FruitCrate作为一个例子,这不是我实际做的,因为公司隐私我无法透露具体情况。

我想知道的是,有没有更好的方法将对象发送到不同的项目而不是发送带参数的方法?

请不要与例子混淆。

2 个答案:

答案 0 :(得分:2)

从我们看到的小代码中,我认为您应该尝试将分区问题域分解为许多较小的类,例如:

  • 水果
  • 客户
  • 顺序
  • 付款

等..

然后将这些组合在一个Crate中,其中包含更少的属性和其他类的成员。这些其他课程应该包含他们的知识和责任。

此模式称为Composition,是建模问题域的推荐方法。

请勿尝试将所有数据放入一个FruitCrate类中。

请注意:这个答案都是关于问题的OOP设计方面的。既然您现在已经澄清了您的主要关注点是<层>边界上的通信,那么希望这部分问题的一些专家能够加入进来......!

答案 1 :(得分:1)

好的,你提出这个问题是对的,你做错了。

Go OO,让我们定义Fruit是什么,

public class Fruit
{
    public string Name { get; set; }
}

现在FruitCrateFruit的集合,我们可以实现的一个简单集合界面是IReadOnlyList<T>,所以

public class FruitCrate : IReadOnlyList<Fruit>
{
    // Where we will hold the fruit internally.
    private readonly List<Fruit> fruits;

    // A constructor that takes in some fruit.
    public FruitCreate(IEnumerable<Fruit> fruits)
    {
        this.fruits = fruits.ToList();
    }

    // The rest is the implementation of IReadOnlyList<T>

    public int Count()
    {
       return this.fruits.Count;
    }

    Fruit this[int index]
    {
        get
        {
            return this.fruits[index];
        }
    }

    IEnumerator<Fruit> GetEnumerator()
    {
        return this.fruits.GetEnumerator();
    }
}

现在,您可以使用索引器将FruitCrate视为Fruit的只读集合。它将与foreach之类的迭代器一起使用,并且它具有Count属性,可以告诉您它包含多少Fruit

然后,您将FruitCrate特定属性添加到您的班级

public class FruitCrate : IReadOnlyList<Fruit>
{
   // ...

   public string ClientName { get; set; }

   public string PaymentDetails { get; set; }

   // ...
}