【发布时间】:2016-06-14 15:44:08
【问题描述】:
一个关于 DTO 的简单问题,我有一个 DTO 类 Cars,其中还有其他一些汽车模型的子类。
public class Cars
{
public Ferrari FerrariModel { get; set; }
public Porshe PorsheModel {get; set; }
public Mustang MustangModel { get; set; }
}
public class Ferrari
{
public string collor{ get; set; }
public int year{ get; set; }
public double price{ get; set; }
}
和 Porshe 和 Mustang 是完全一样的法拉利。问题是我现在不知道如何进行。我尝试这样的事情
Cars cars = new Cars();
FerrariModel fm = new FerrariModel();
cars.FerrariModel.collor = txtCollor.Text;
而且它不起作用,因为我在 cars.FerrariModel.collor ->“对象引用未设置段落 Hum 对象的实例。嗡嗡声对象声明”中收到以下错误。 我必须承认,我什至不知道这是“可能的”,或者我是否正在“发明编程”,所以任何帮助都会非常有用。
- 为什么只使用一个类?因为需要在参数中传递单个 DTO:save(Cars car);更新(汽车汽车)
- 使用第二个单独的类会迫使我“重载”该方法:save(Cars car);保存(法拉利法拉利);
- 如果我使用单个班级(没有 Ferrari、Porshe 和 Mustang)程序可以工作,但我的 InteliSense 中有很多变量,超过 50 个。
谢谢。
【问题讨论】:
-
代码做了太多事情,最终破坏了 Single Responsibility Principle。如果模型具有相似的属性,它们可以从 interface 或 abstract 类继承。然后你就可以使用通用列表了。
-
谢谢,我还是个初学者,对接口和抽象类不太了解,最近在研究泛型列表,甚至我用这个方法就像一个传输数据对象,你喜欢吗它“重”吗?因为我真的不希望有很多方法来完成几乎相同的操作
标签: c# asp.net data-access-layer dto bll