简体   繁体   English

如何在C#Winforms中使用MVP模式将多个表中的数据显示到我的视图中?

[英]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: 说我有下表:

  1. Fruit 水果
  2. 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.

相关问题 在Winforms(C#)中使用带有MVP模式的backgroundworker - using backgroundworker in Winforms (C#) with MVP pattern 在MVP设计模式中,一个视图可以有多个演示者吗? - In MVP design pattern, can a view have more than one presenter? 使用C#从html表中插入多行数据? - insert more than one row of data from html table using C#? 如何使用C#WinForms在组合框中选择多个项目 - How to Selecting a More than one items in Combobox using C# WinForms 如何从 SQL 表转换二进制数据(检查其图像类型)并在 Winforms C# 上显示 - How to convert binary data from SQL table (check if its image type) and display it on Winforms C# C#Winforms MVP被动视图,在哪里处理模型? - C# Winforms MVP-Passive View, where to handle the Model? C#使用ThreadPool时,我可以将多个数据传递给我的目标方法吗? - C# Can I pass more than one data into my target method when using ThreadPool? 多次使用 Matrix.Rotate C# Winforms - Using Matrix.Rotate more than once C# Winforms 如何在WinForms C#中将数据从一种形式传输到另一种形式? - How to transfer data from one form to another in winforms c#? 如何从表中获取SELECT * FROM TABLE结果到多个变量中 - how to get SELECT * FROM TABLE results into more than one variable in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM