Flex, .NET 3.5 with LINQ to SQL part 2 (synchronising from Flex to LINQ)

29 09 2008

March 2009: This post is outdated, please see the latest article on FLEX & LINQ:

https://justinjmoses.wordpress.com/2009/03/19/rich-clients-flex-air-silverlight-linq-data-across-the-pipe/

 

Right.

So, getting your objects from your .NET 3.5 Linq to SQL setup to Flex is fairly easy. Getting the changed data back and synchronised to your DB is a different story.

The great thing about LINQ is that the data you retrieve and modify in .NET will automatically sync back to the DB by calling the SubmitChanges() method on your context (Linq to SQL) object.

The problem is of course when this data comes back in from Flex, the system doesn’t know what exactly has changed.

Now, the first thing to realise is that your Value Objects in Flex should NOT have the foreign key IDs in them. Why? Because say in the Company/Employee example, if you submit a Company object back to the system, with each Employee in the list having both a CompanyID AND a Company object reference, how will the LINQ system know which is the correct reference?

These foreign IDs will try to get serialised on the remoting level and be ignored. Good. We’re dealing with object references now, and foreign key ids are useless. If I want the foreign key of some employee, I would say: EmployeeObj.Company.CompanyID

Now, in .NET, I have a “Save()” method that I want to essentially insert/update the object and all objects within in.

Using the example before, say I have a company object, and that has a list of Employees. Then if I have a Save(Company c) method, I want the company object to insert/update, and the list of employees to automatically synchronise.

You can try deleting the old object and then inserting the new

So,

SaveCompany(Company newCompany)
{

DataClassesDataContext db = new DataClassesDataContext();
if (newCompany.CompanyID > 0)
{

Company oldCompany = db.Companies.Single<Company>(c => c.CompanyID = newCompany.CompanyID);
db.Companies.DeleteOnSubmit(oldCompany);
}
db.Companies.InsertOnSubmit(newCompany);
db.SubmitChanges();
}

This will nicely handle an insert/update of the company object, but it won’t deal with your Employee objects that well. Especially when each Employee references anthor table that essentially depicts there type./

Say each Employee has a foreign key reference to EmployeeType. Employee type is a static naming table that has a set of 5 records: “Manager”, “Sales Coordinator”, “Receptionist”, “Consultant” and “Helpdesk”. Then my Employee object will have an EmployeeType property (loaded via LINQ). Now, when this goes through LINQ, I believe during the serialisation, the creation of a “new” object EmployeeType will mean that the code above will create a new EmployeeType record for the Employee. Ouch.

So even though you may have sent all the employees through LINQ to Flex via a Company object, when it comes back, even if unchanged, all these employees will have references to new EmployeeType objects.

The way to solve this is fairly easy:

foreach (Employee e in newCompany.Employees)
{
   EmployeeType type = db.EmployeeType.Single<EmployeeType>(t => t.TypeID == e.EmployeeType.TypeID);
   e.EmployeeType = type;
}

oldCompany.Employees = newCompany.Employees;

Unfortunately, it’s not an easy process to learn how exactly to handle each scenario in synching LINQ and Flex, but one thing’s for sure, it certainly makes code lighter, and a lot easier to update.

Advertisements

Actions

Information

2 responses

30 09 2008
MacMillian

Look at DataLoadOptions() and DataLoadOptions().AssociateWith

1 10 2008
Justin J. Moses

MacMillian,

So the load options can restrict or structure the data I get out of .NET in the first place, right?

This is useful to know, I must say. Though in the context of the post, I actually wanted all the data eagerly loaded. I wanted to get all Employee data into Flex, update one of those Employee objects, then send the Employee object back to .NET and get LINQ to manage what not just inserting/updating the Employee object, but also any foreign objects associated with it.

justin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: