Context vs EntityCollection

Posts   
 
    
TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 21-May-2008 13:49:06   

Hello,

We are writing a new application and would like to use the Context object. Meaning would be that we have only 1 instance of every object in our application so that when we change the name of an customer in one screen, all other form that display this name would follow.

I've been playing around with the Context and have some problems and some questions. I hope you can help me out.

First a question; in the manual there is stated: A context shouldn't be used as a cache, nor should it kept alive for a long time, just long enough for the semantic context to use unique objets in. Is it a problem if my semantic context is the entire lifecycle of my application?

Now the problems: I'm using a global static Context object to hold all entities fetched. I'm able to have multiple forms that work together on the same entity. I have code like this


         DepartmentEntity department = new DepartmentEntity(5);
         adapter.FetchEntity(department);
         department = (DepartmentEntity)Form1.Context.Get(department);

First I used the following code, but this didn't work...


         DepartmentEntity department = new DepartmentEntity(5);
         adapter.FetchEntity(department, Form1.Context);

Can you please explain me why it doesn't work or what I'm missing about the context?

The second problem is with a collection. I have following code:


         adapter.FetchEntityCollection(Departments, null);
         Form1.Context.Add(Departments);

When I first load my collection and then my separate department entity, the entities from the collection (shown in a grid) follow the entities from the separate forms. However, if I reverse the working order and first load a single department followed by all departments and add them to the context, they don't follow any more.... So it seams that I have 2 different kind of references. With single entities I can do the trick with Context.Get (which I don't like) but how to fix this with collections?

Again, can you please tell me what I'm missing or doing wrong?

Kind regards TomV

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 21-May-2008 18:06:14   

First a question; in the manual there is stated: A context shouldn't be used as a cache, nor should it kept alive for a long time, just long enough for the semantic context to use unique objets in. Is it a problem if my semantic context is the entire lifecycle of my application?

I don't think there would be a problem. I think it actually ment: not to keep it behind its usage.

First I used the following code, but this didn't work... Code:

     DepartmentEntity department = new DepartmentEntity(5);
     adapter.FetchEntity(department, Form1.Context);

Can you please explain me why it doesn't work or what I'm missing about the context?

The second problem is with a collection. I have following code: Code:

     adapter.FetchEntityCollection(Departments, null);
     Form1.Context.Add(Departments);

I find it hard to follow your scenario, and underdstand what exactly is not working. So would you please attach a simple repro solution based on Northwind database.

Also which LLBLGen pro runtime library version are you using?

TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 23-May-2008 10:15:06   

Hi Wallah,

Sorry that my explanation wasn't clear. I created a demo project as you requested. It's a MDI with a menu. You have 3 menu options:

Scenario3: Load 2 forms, change the company name on form 1, the name on the second form will also change. Perfect, this is what we wants...

Scenario4: Loads the same 2 forms but also a grid with all customers. Change the name of customer ANTON in the grid. When you leave the cell, the names on the small forms should change too, but it doesn't. Only 1 form is changed. ==> PROBLEM

Scenario5: Is the same code as Scenario 4, but the loading of the collection with customers is done before the load of the separate customers. Now change the company name of customer ANTON, when you leave the cell, none of the forms is changed ==> BIGGER PROBLEM

So why is the loading sequence is important? I think the synchronization (unique ness) of the objects by the Context is not well performed. Or as I said before, maybe I miss something about the context.

Can you please help me out on this one. Like always and all posts it's very urgent for us because this would be the base of our application smile

Kind regards TomV

Althought the Context is cleared when you select a new menu option, maybe close all forms before switching scenario's...

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 23-May-2008 11:44:22   

That's how you should do it:

         EntityCollection<CustomersEntity> Customers = new EntityCollection<CustomersEntity>();
         Context.Add(Customers);
         adapter.FetchEntityCollection(Customers, null);

         CustomersEntity customer1 = new CustomersEntity("ANTON");
         //Context.Add(customer1);
         adapter.FetchEntity(customer1);
         customer1 = (CustomersEntity)Context.Get(customer1);

         CustomersEntity customer2 = new CustomersEntity("ANTON");
         //Context.Add(customer2);
         adapter.FetchEntity(customer2);
         customer2 = (CustomersEntity)Context.Get(customer2);
TomV
User
Posts: 76
Joined: 31-Jan-2008
# Posted on: 23-May-2008 11:51:50   

Thanks a lot Walaa!

So I have to add the customers collection to the context before I fetch it... The only question I still have is why the overload to fetch an entity has the posibility to add a Context if that's not the way how it should be used...

But I'll use it like this! Again, thanks for the fast reply!

Kind regards TomV