通过sqldatareader从数据库中获取相关数据并推送到ViewModel/ViewModelCollections中

发布于 2024-08-23 10:03:01 字数 368 浏览 3 评论 0原文

假设我有以下 3 个实体:Customer、Order、Product,它们在视图中与 CustomerOrderProductViewModel.cs 进行交互:

我有几个控件,如列表框、数据网格等。在我的视图中显示这些实体。

这些实体应以急切加载的方式获取。这意味着我的 DAL 中有 3 个 sqldatareader。每个sqldatareader从表Customer、Product、Order读取数据。我现在必须考虑的问题 如何将订单添加到产品中并将产品添加到客户列表中?将3个for循环中的所有相关数据相互读取?以及如何将相关数据放入我的 VMCollections 中,以便主详细信息保持完整。

MVVM 纯粹主义者和 alpha 极客对这个话题非常沉默。

Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View with the CustomerOrderProductViewModel.cs:

I have several controls like listbox,datagrid,etc.. to display those entities in my View.

Those Entities shall be fetched sort of eager loading. That would mean I have 3 sqldatareader in my DAL. Each sqldatareader read data from table Customer,Product,Order. What I have to consider now How do I get the Orders into the Products and the Products into the Customers List ? Read every related data in 3 for-loops into each other? And how do I get that releated data into my VMCollections so the Master Detail stays intact.

The MVVM purists and alpha geeks are very silent about that topic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-08-30 10:03:02

假设您在处理 DataReader 和要复制的对象的业务对象上设置了构造函数,则可以使用 LINQ 来实现此目的。虽然,我对你的查询的结构有点困惑,但我认为这就是你所说的。

public class ViewModel
{
    public ViewModel(DAL dal)
    {
        Customers = dal.GetCustomerFull().ToList();
    }

    public List<Customer> Customers { get; set; }
}

public class DAL
{
    public IEnumerable<Customer> GetCustomerFull()
    {
        var customers = GetCustomers().ToList();
        var products = GetProducts().ToList();
        var orders = GetOrders().ToList();

        var query = from c in customers
                    select new Customer(c)
                    {
                       Products = from p in products
                                  where p.Id = c.ProductId
                                  select new Product(p)
                                  {
                                     Orders = from o in orders
                                              where o.Id = p.OrderId
                                              select o;
                                  }
                   };

        return query;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // setup command
        var reader = new SqlDataReader(cmd);
        while (reader.Read())
        {
            yield return new Customer(reader);
        }
    }
}

You can use LINQ for this assuming you have constructors setup on your Business objects that handles a DataReader and an object to copy. Although, I'm a little confused about the structure of your query, but I think this is what your saying.

public class ViewModel
{
    public ViewModel(DAL dal)
    {
        Customers = dal.GetCustomerFull().ToList();
    }

    public List<Customer> Customers { get; set; }
}

public class DAL
{
    public IEnumerable<Customer> GetCustomerFull()
    {
        var customers = GetCustomers().ToList();
        var products = GetProducts().ToList();
        var orders = GetOrders().ToList();

        var query = from c in customers
                    select new Customer(c)
                    {
                       Products = from p in products
                                  where p.Id = c.ProductId
                                  select new Product(p)
                                  {
                                     Orders = from o in orders
                                              where o.Id = p.OrderId
                                              select o;
                                  }
                   };

        return query;
    }

    public IEnumerable<Customer> GetCustomers()
    {
        // setup command
        var reader = new SqlDataReader(cmd);
        while (reader.Read())
        {
            yield return new Customer(reader);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文