一个关于 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 对象的实例。hum 对象声明”中收到以下错误。我必须承认我什至不知道这是“可能的”,或者我是否在“发明编程”,所以任何帮助都会非常有用。
- 为什么只使用一个类?因为需要在参数中传递单个 DTO:save(Cars car); 更新(汽车汽车)
- 使用第二个分离的类会迫使我“重载”该方法:save(Cars car); 保存(法拉利法拉利);
- 如果我使用单一课程(没有法拉利、保时捷和野马),程序可以工作,但我的 InteliSense 中有很多变量,超过 50 个。
谢谢你。