简体   繁体   English

如何通过PRESENTER将数据从数据库传输到VIEW?

[英]How to transfer the data from Data Base to VIEW via PRESENTER?

I'm trying to create app that implements MVP pattern using WinForms. 我正在尝试创建使用WinForms实现MVP模式的应用程序。

Wherein I'm also using EF+CodeFirst+Linq. 其中我也使用EF + CodeFirst + Linq。

On the VIEW there is DataGridView control, that need filling a data. VIEW上DataGridView控件,需要填充数据。 The VIEW call a method SELECT() of PRESENTER class, which in turn call a method SELECT() of MODEL class. VIEW调用PRESENTER类的方法SELECT() ,后者又调用MODEL类的方法SELECT()

How to transfer the data from Data Base to VIEW via PRESENTER ? 如何通过PRESENTER将数据从数据库传输到VIEW

I'm trying to use return but it not work because i'm using the USING block. 我正在尝试使用return但是因为我正在使用USING块,所以它不起作用。

internal void Select()
{
    using (GoodsContext context = new GoodsContext())
    {
        var items = from Items in context.Goods
                    select Items;
    }
}

Quite interesting question. 非常有趣的问题。 Of course one can materialize the query and return it as IEnumerable , but I was wondering what is the way to return it as IQueryable , to allow further filtering/sorting etc. The only way I see is to not dispose the DbContext (apparently the returned queryable keeps reference to it), but is it safe? 当然,可以实现查询并将其返回为IEnumerable ,但是我想知道以什么方式将其返回为IQueryable ,以允许进一步的过滤/排序等。我看到的唯一方法是处理DbContext (显然返回queryable一直引用它),但是安全吗? Then I've googled and found this Do I always have to call Dispose() on my DbContext objects? 然后,我在Google上搜索了一下,发现是否总是需要在DbContext对象上调用Dispose()? Nope . 不行 The explanation inside sounds reasonable to me, and we already have a disposable object ( Task ) that we are not supposed to Dispose . 对我来说,内部的解释听起来很合理,并且我们已经有一个不应该Dispose的可抛弃对象( Task )。

Shortly, you can remove the using statement and return the IQueryable . 不久,您可以删除using语句并返回IQueryable

You should change type of method Select from void to IEnumerable<Good> to be able to return something. 您应该将方法Select类型从void SelectIEnumerable<Good> ,以便能够返回某些内容。 Also use .ToList to materialize result to a List : 还可以使用.ToList将结果.ToList List

internal IEnumerable<Good> Select()
{
    using (GoodsContext context = new GoodsContext())
    {
        var items = (from Items in context.Goods
                     select Items).ToList();
        return items;
    }
}

Change return type of Select method to List<Good> Select方法的返回类型更改为List<Good>
Then "materialize" result to the List of data, and you will not depend on the DataContext 然后将结果“物化”到数据列表中,您将不必依赖于DataContext

internal List<Good> Select()
{
    using (GoodsContext context = new GoodsContext())
    {
        return context.Goods.Select(items => items).ToList();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM