Reputation: 84
I'm currently reading through the self-paced training kit for the 70-516 exam (accessing data) and I'm in the chapter about Entity Framework.
I'm at a point where it talks about the ObjectContext and it inheriting IDisposable for cleanup. This brought up a few questions that I can't seem to get an exact answer on when researching. I'm just trying to understand how things work.
SCENARIO: I've created a Silverlight Application using WCF Ria and Entity Framework and use MVVM Lite pattern. I made my EDMX and created a domain service called FamilyService, which I use as FamilyContext in the silverlight application. I create a method called GetFamilyMembers that returns data from the FamilyMember table.
[EnableClientAccess()]
public partial class FamilyService : LinqToEntitiesDomainService<FamilyEntities>
{
public IQueryable<FamilyMember> GetFamilyMembers()
{
return this.ObjectContext.FamilyMembers;
}
}
In my ViewModel I create an instance of the FamilyContext when it constructs and null it on cleanup.
1) When I null the FamilyContext object, does it do anything with the ObjectContext on the server? I ask this because I never manually create or dispose of it in the domain service.
2) I understand that when I run a query in Entity Framework, it caches the data. In the RIA scenario, does the data cache on FamilyContext on the client application, or does it still reside on the ObjectContext on the server? I'm just curious if the ObjectContext holds the cache since each viewmodel in my application creates its own instance of the FamilyContext, wondering if the viewmodels are lightened by the cleanup but can still take advantage of the caching.
Thanks in advance.
Upvotes: 1
Views: 1050
Reputation: 35544
You need to differentiate a little more.
The DomainService (your FamilyService) is part of your server logic and an instance of the service will be created each time a request/method call (such as Query
, Update
, Insert
, Delete
, Invoke
) is made on your service. This is done by the ASP.NET / WCF RIA Services runtime.
If you want to control how instances of DomainServices are created you need to implement your own DomainServiceFactory. But this is an advanced scenario.
So each request gets its own instance of your DomainService
and so will the DomainService
have its own instance of the ObjectContext
. When the call is finished the ObjectContext gets disposed. Caching in this scenario only occurs when you query the ObjectContext multiple times during the request.
A DomainContext is a stateful client-side representation of a domain service, providing access to all the functionality of the service. It provides a cache (aka. EntityCollections) that contains the entites loaded from the DomainService
. The cache is also used to update or delete loaded entites and to insert new entities. Changes on the cache need to be submitted, so that the corresponding methods are called on the DomainService
to update, delete or to insert your entities.
Instances of your DomainContext are created using the constructors of the class and its normally up to you to create and cleaning up instances of the DomainContext. So setting the DomainContext to null does nothing on the server side.
Upvotes: 1