Async Calls to Entity Framework - wpf

Anyone found a good way to make asynchronous calls to the db via entity framework? Use background worker thread? This is a WPF application.
I know in RIA services there is a loadcompleted event that does not seem to exist in entity framework's object context.
Thanks!

I've written a blog post about async calls via commanding in a WPF application
http://devblog.terminto.com/2010/11/03/threading-as-command-in-mvvm/
At work I use the same technique and do calls to DB (EF4).
Is this useful, or are you asking about more entity framework-specific async methods?

Related

Wpf configuration service like global.asax

I am working on WPF Application.I am using Auto Mapper in WCF services. In My project I have different kind of layer like Service layer, Business Layer , Presentation Layer , I want to work on Service Layer that is (WCF).
In Auto Mapper there is creation of map
Mapper.Initialize(Item=> Item.CreateMap<Model.Person, PersonInfo>());
Mapper.Initialize(Item=> Item.CreateMap<Model.Bank, BankInfo>());
Mapper.Initialize(Item=> Item.CreateMap<Data, Model.DataInfo>());
When ever I call function at that every time it will call as per application perfomance.
I want to make as per global in application.That is Create once in application and use as many time as they can
How to Implement this concept of globalization at service layer in WPF application.
There is no concept of global.asax is WPF Application.then how to implement that concept of globalization of mapper initilization
Thank you.

Entity Framework Context lifetime

I have a WPF desktop app that using EF orm for mapping. My app have several pages
and each page has several windows. EF context is created per form. Pages in app using stateless entity only to show data the user.
My problem is that a page should update an entity for a single task and for this reason there should be a context living on this page. Therefore where I open window on this page for update other entities, there are 2 contexts active on this page.
How I can prevent this condition?
Create a new class "ClientRepository" in your project. This class will contain all entity framework access logic your client has.
Use this class in all your windows and pages.
If you configure this class to behave as singleton, you will only have one entity framework context.
Ideally this can be done using a DI framework like Microsoft Unit or Ninject, but of course you can also do it manually.
Singltons?
You might ask yourself, whether its a good idea to you singletons here.
I had a similar question once and thus asked Brian Noyes (Microsoft MVP) on a "MVVM" course on the pluralsight website.
In his response he wrote: "...most of my client services are Singletons anyway and live for the life of the app."
So, for a client service, its ok to have a singleton.

Enhancing my WCF service with asynchronous calls?

I am currently developing a WCF service in a windows form application. There would be a simple GUI screen with 2 buttons, "Start" and "Stop". Whenever I attempt to start the service, the UI of the application would temporary hangs until it loads finish. Is it possible to add a loading effect to it? And how can I do that.
Still pretty new in this area. Would welcome any comments!
Is there any good tutorials out there that teaches how can I enhance this issue?
I have read the MSDN blog tutorial from Rick Rainey Making asynchronous service calls from a WinForm client but it is not really that of a good tutorial.
Just to clarify, my WCF service is a publish subscribe framework and I think I do not need to be able to call the operations asynchronously. All I want to know is how do I initiate the connection asynchronously.
Is it possible to add a loading effect to it? And how can I do that.
For this you can use duplex service contract.
Also, since your application is a subscription application you might want to look into pushing data to your application from the WCF service. Here's a nice example and although it's for Silverlight I think it might be useful to you:
http://weblogs.asp.net/dwahlin/archive/2008/06/16/pushing-data-to-a-silverlight-client-with-wcf-duplex-service-part-i.aspx

WCF Silverlight Service Reference

I have added a ServiceReference to my Silverlight app. I have called it, WcfServiceReference. I can code the following
using ( var client = new WcfServiceReference.WcfDataServiceClient( ) )
{
client.GetSpendDataTotalAsync( Guid.NewGuid( ) );
}
I was thinking I should be able to reference client.GetSpendDataTotal (not Async), but it isn't available. However, it is in the app - I can find it in my service... Probably something obvious?
Silverlight only allows you to consume WCF services using async methods. The general idea is that synchronous calls to external services tie up the UI thread and leave your app unresponsive until they return. Take a look at this question and this blog post for more discussion on Silverlight and why you're guided into using async service calls. To quote from the blog post, "the whole purpose of the plug-in architecture only permitting async requests is so that plug-ins would not be able to lock up the browser".
Eric Lippert gives an excellent explanation/discussion of the problem of background work tying up the UI thread in this MSDN Magazine article about the upcoming async-await pattern. Definitely worth reading.
The main thing to remember here is that regardless of how much useful work might be happening in the background, if the UI is unresponsive the app is doing nothing as far as the user is concerned. Using asynchronous WCF service calls in Silverlight allows you to do something else with the UI whilst waiting for the result rather than leaving the user wondering why they can't do anything.
Silverlight doesn't allow non-asynchronous calls to services. When you code Silverlight access to WCF services, think Asynchronous.

Updating service reference in silverlight application not working.. Please Help!

I'm building a silverlight LOB application which uses both RIA services and a vanilla .ASMX service..
The data is wired up using entity framework..
I have 1 entity which i recently added a 1 to many relationship to a collection of a new type of object..
Using RIA services i can get to that in my silverlight application fine by saying:
instanceOfEntity.NewEntities
However in the vanilla service which is using the exact same object context class even after updating, deleting and recreating the service reference in the silverlight application.. the property is still not being exposed..
Someone Plleeaeeeeease tell my how i can get this dang thing to show up via this service!
Thanks
Daniel
Ended up switching to a WCF service instead..
Seems to work fine,
Not sure if this is a limitation in vanilla ASMX services?

Resources