这是一个更详细的例子问题,我问:Best Practice with classes and the database传输数据库表类/类到数据库表
我使用C#与SQL Server和使用小巧玲珑作为我的ORM。
这里是我的类,将组件表加载到应用程序:
class DBComponent
{
public int ID { get; set; }
public string Component { get; set; }
public int Material_ID { get; set; }
public int Color_ID { get; set; }
}
然后,我需要我的其他类,将具有实际值:
class Component
{
public int ID { get; set; }
public string Component { get; set; }
public string Material { get; set; }
public string Color { get; set; }
public Component(DBComponent C)
{
ID = C.ID;
Component = C.Component;
Material = //queries the Material Table passing in C.Material_ID and returning the Material Value
Color = //queries the Color Table passing in C.Color_ID and returning the Color Value
}
}
我这样做的原因是,我可以使用控件(组合框)的WinForm应用程序的值,和其他需求。此外,“DBComponent”类将具有一个方法,该方法将采用“Component”对象并创建一个“DBComponent”对象,该对象将作为新记录发送回数据库。
这是处理这种情况的最好方法吗?还是有更好的方法?在我的另一篇文章中有人提到,小巧玲珑可以自己做到我不需要创建2个类的地方,只需要1个类。这是怎么回事?
您可以使用隐式运算符来执行此操作。看看这里:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/implicit – Egbert
@Egbert我可以看到如何使用隐式可以在这里使用,我不认为我我会在这种情况下使用它,但我喜欢这个概念,我会在我的项目的其他部分使用它,谢谢! – jediderek