Friday, January 12, 2007

Lazy Loading in a SOA world.

As a "side effect" of my current project, I am helping my client build a SOA interface to another large system. This client has traditionally used datasets for most of their data access, and one of my goals is to get the harsh dataset monkey off their backs and wean them onto the smooth, mellow high of business domain objects. After all, all the really cool Enterprises are doing it! I'm also trying to champion the "One interface to rule them all" cause, so this (eventually) will be the ONE and ONLY way to get data from, or put data in this system.

In putting together a proof of concept, I came across a bit of a problem; my entity has several properties that return lists of items. Some of these lists can be quite large, so we want to lazy load them. For my example, I created a person, who had a list of four address (I’m just returning four hard-coded ones since I didn’t want to muck with the database for the proof of concept) which I wanted to lazy load. I wired everything up and it was working beautifully!

Except for one thing; I was getting eight addresses back.

Here’s the problem; when the web service was getting ready to send my person object back, it was serialized by the XML serializer. That serializer was looking at my address property, which caused a lazy load. So, my object was initially coming over the wire with all the lists populated. One the client side, the de-serialization was also calling the address property, which was again lazy loading the address right away. I had a check in the “get” to make sure the list was null before the loading occurred, so how BOTH lists got merged together is something I can only wildly speculate about at this point, but it was merely a side effect of a different problem, so I didn’t worry about it.

The solution was to find a way for the XML Serializer to ignore that property when it was sending it out.

The solution: the XMLIgnore attribute. Decorating my property with this attribute causes the serializer to skip it. It doesn’t get looked at during serialization, it’s ignored during de-serialization and my lazy load routine work beautifully.

And all was well in the kingdom of SOA (for now…)


Jordan said...

Nice post. I have to be honest and say that I did not know you could turn serialization on/off on various attributes like this but I can certainly think of a bazillion situations where it would need to be done.

Alexey Govorin said...

or you can solve this problem by de-coupling your object into: entity business object and data transfer object. You will control what dto sends over the wire.
There are plenty of tools that can do it for you, like Software Factory :)

James Bender said...

Alexey: You make a good point, and we are using Service Factory for this project. Service Factory, out of the box, does this decoupling for you, and I agree this is the way to go, in general.

A lot of people who are new to Service Factory, and to SOA best practices in general, are a little taken aback by the number of projects that Service Factory creates, and Service Factory does provide a mechanism to reassign project responsiblity as a means to consolidate projects in your solution a little. Generally, I'm not a fan of consolidating Business Entities and Data Types, but most users seem to point to that first as a place to do this. I have found that instead of giving them the 100's of reason why they shouldn't, let them work through a POC, on their own, with that consolidation. It doesn't take long for them to come back around to my way of thinking.

Sometimes, you just have to touch the stove. :)

In this case, all of our stuff is happening in one big federated family, so I was willing to open my mind to the idea a little, since with no truly public interface, and several months before we need to actually utilize this, the risk was on the low side.

Anonymous said...

Would you use this technique on singular dependent properties if you have a large object graph? If so, if you XMLIgnore a property, how does your getter know what to get? In other words do you have a property that holds a constraint? I.e. the ID of the dependent object?

Anonymous said...

Aston Villa rode their luck at Hull City where an 88-minute own goal from Kamil Zayatte saw them leapfrog three points clear of Arsenal and into fourth place in the Premier League wow gold with a 1-0 win.

Villa had to survive Hull penalty appeals for a handball against Ashley Young in time added on, television replays showing that referee Steve Bennett wow gold correctly rejected the claims after consulting a linesman.

Bennett had been involved in controversy after just five minutes when American goalkeeper Brad Friedel looked to have handed Hull the initiative and threaten Villa's return to the Champions League qualifying wow gold zone.

Friedel spilled wow gold the ball under pressure from Nick Barmby and stand-in right-back Nigel Reo-Coker turned it into his own net as he attempted to wow gold clear.

But Bennett cut short wow gold celebrations at the KC Stadium -- and let Friedel off the hook -- when he ruled out the score for an apparent infringement by Barmby.

Zayatte's intervention from a Young cross bound for wow gold Gabriel Agbonlahor then saw Villa leapfrog Arsenal and draw level with Manchester United on 38 points -- seven adrift of leaders Liverpool and four wow gold behind Chelsea.

Stung by an on-pitch dressing down wow gold by manager Phil Brown at Manchester City last week, Hull showed five changes and a vastly improved performance.

Promoted Hull were looking for only their second win in 11 games while wow gold Villa arrived unbeaten in seven and it looked to be heading for a goalless draw when the home side suffered a cruel late blow.

Anonymous said...

Premier League leaders Liverpool thrrew their "full support" world of warcraft gold behind Steven Gerrard after their captain was charged with assault on Tuesday following an incident at a local nightclub.

Gerrard, 28, and two other men were charged with assault occasioning bodily harm and affray, Merseyside police said.

They are scheduled to world of warcraft gold appear in court on January 23.

In the wake of the charges, Liverpool issued a wow power leveling statement on their official Web site.

"Steven has been an outstanding servant to world of warcraft gold Liverpool for the last ten years and the club will give him all the support he needs at this time," it read.

The offenses with which Gerrard have been charged carry cheap wow gold a maximum penalty of five years in prison.

Three other men, who were also arrested, were world of warcraft gold released without charge pending further inquiries.

Gerrard was arrested cheap wow gold in the early hours of Monday morning and eventually charged after over 20 hours in police custody.

His wife Alex Curran was seen near the police station where world of warcraft gold he was held but drove off without talking to reporters.

Gerrard had been celebrating his side's 5-1 buy wow gold win over Newcastle on Sunday when the alleged altercation occurred.

He had scored two goals in the victory at St James' Park which left world of warcraft gold Liverpool top of the English Premier League and on course for their first English title in 18 years.

He was released on bail to wow power leveling appear in court on January 23 with John Doran, 29, and Ian Smith, 19, who were charged with the same offenses.

Gerrard, who has spent his entire career with Liverpool, world of warcraft gold is one of the most celebrated players in the club's history and renowned as a model professional.

In 2005 he lifted the European Cup -- Liverpool's fifth -- wow gold kaufen after a thrilling penalty shooting victory over AC Milan and turned down the advances of cash-rich Chelsea to stay with his local club.

The midfielder has also made 70 appearances for England, dofus kamas captaining his country on four occasions.

He was made an Member of the British Empire (MBE) last year and received an honorary fellowship from Liverpool's John Moores University.

Liverpool's next match is against Preston in the kamas dofusEnglish FA Cup on Saturday, a game Gerrard was due to sit out because of suspension