简体   繁体   中英

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

I'm trying to create app that implements MVP pattern using WinForms.

Wherein I'm also using EF+CodeFirst+Linq.

On the VIEW there is DataGridView control, that need filling a data. The VIEW call a method SELECT() of PRESENTER class, which in turn call a method SELECT() of MODEL class.

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

I'm trying to use return but it not work because i'm using the USING block.

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? Then I've googled and found this Do I always have to call Dispose() on my DbContext objects? Nope . The explanation inside sounds reasonable to me, and we already have a disposable object ( Task ) that we are not supposed to Dispose .

Shortly, you can remove the using statement and return the IQueryable .

You should change type of method Select from void to IEnumerable<Good> to be able to return something. Also use .ToList to materialize result to a 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>
Then "materialize" result to the List of data, and you will not depend on the DataContext

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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