[英]How to display data from more than one table to my view using MVP pattern in C# winforms?
How to display data from more than one table to my view using MVP pattern in C# winforms? 如何在C#Winforms中使用MVP模式将多个表中的数据显示到我的视图中?
Say I have the following tables: 说我有下表:
Color 颜色
Table definition: +----------+ | Fruit | +----------+ | Id | | Name | | Color_Id | +----------+ |∞ | |1 +----------+ | Color | +----------+ | Id | | Name | +----------+ Table contents: Fruits +-----+--------+----------+ | Id | Name | Color_Id | +-----+--------+----------+ | 10 | Apple | 70 | | 20 | Orange | 80 | | 30 | Grapes | 90 | +-----+--------+----------+ Colors +-----+--------+ | Id | Name | +-----+--------+ | 70 | Red | | 80 | Orange | | 90 | Violet | +-----+--------+
Then in my solution I have created classes for each tables namely, Fruit and Color class, having their own getters and setters and other details representing the tables in the database. 然后,在我的解决方案中,我为每个表创建了类,即Fruit和Color类,它们具有自己的getter和setter以及代表数据库中表的其他详细信息。
Then I also created two models for Fruit and Color. 然后,我还为“水果”和“颜色”创建了两个模型。
So if I wanted to create a view that will display a record about Color, I would use the model I created for Color to retrieve a collection of colors (eg List<Color>
). 因此,如果我想创建一个显示有关Color记录的视图,则可以使用为Color创建的模型来检索颜色的集合(例如
List<Color>
)。 If I'm using a DataGridView to display that record, I would get something similar like the following: 如果我使用DataGridView来显示该记录,则会得到类似以下的内容:
+-----+--------+
| Id | Name |
+-----+--------+
| 70 | Red |
| 80 | Orange |
| 90 | Violet |
+-----+--------+
For the Fruit records, I could use the same method above but I need to display the color name instead of the color id like the following: (Assuming I'm using a DataGridView or a custom control) 对于Fruit记录,我可以使用与上面相同的方法,但是我需要显示颜色名称而不是颜色ID,如下所示:( 假设我正在使用DataGridView或自定义控件)
+-----+--------+----------+
| Id | Name | Color |
+-----+--------+----------+
| 10 | Apple | Red |
| 20 | Orange | Orange |
| 30 | Grapes | Violet |
+-----+--------+----------+
I figured I could create another class for it and name it 'FruitColor' then give it properties same as the Fruit class above but instead of having a color id property, I'd replace it with a color name instead... But I'm not really sure if it's the right way of doings thing in MVP. 我想我可以为其创建另一个类并将其命名为“ FruitColor”,然后为其赋予与上面的Fruit类相同的属性,但是我没有颜色id属性,而是将其替换为颜色名称...但是我我不太确定这是否是MVP中正确的做法。 I thought I should ask you guys for any tips on how to do it right...
我以为我应该问你们如何正确做的任何提示...
I'm not using ORM's for my project because I want to understand how do it manually...But I would also appreciate any help related to using ORM's. 我没有在我的项目中使用ORM,因为我想了解如何手动进行操作...但是我也希望与使用ORM有关的任何帮助。 I also just started learning about MVP 3 weeks ago so I'm really hoping for any help.
我也刚刚在3周前开始了解MVP,所以我真的很希望能得到任何帮助。
When retrieving Fruit
, you should query the colours with them using a JOIN
in SQL: 检索
Fruit
,应使用SQL中的JOIN
来查询颜色:
RETRIEVE f.*, c.name as color_name
FROM Fruits f
INNER JOIN Colors c
ON f.Color_Id = c.Id
Then, transform the results you get into Fruit
as you're probably already doing. 然后,将可能已经完成的结果转换为
Fruit
。 This would, however, require you to expand your Fruit
class so it would either have a string
property for the color name
variable, or just add a property that contains the color as a whole: 但是,这将需要您扩展
Fruit
类,使其具有颜色name
变量的string
属性,或者仅添加包含整体颜色的属性:
public class Fruit
{
public int Id { get; set; }
public string Name { get; set; }
public Color Color { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.