Home
  Latest posts
  My Writings
  My Code
  My Gallery
  About me
 
  rssfeed Syndication
 
Bloggtoppen.se
 
 
Links
  Cornerstone
  SweNug
 
Post categories
  misc (48)
  Architecture (21)
  C# (19)
  Asp.Net (2)
  Vb.Net (2)
  Training (7)
  Data (19)
  Events (40)
  Platform (2)
  Orcas (4)
  Updates (3)
  Methods (10)
  Tools (6)
  Announcements (14)
  Languages (1)
  Patterns (6)
  Opinions (11)
  Fun (3)
  Ineta (1)
  Opinion (0)
  Practices (2)
  WCF (5)
 
 
 

Composing entities in a SOA using LINQ.

Tuesday, January 22, 2008

Recently I was put in front of a scenario where data from two different services had a relation and needed to unite to form a meaningful reply to consumers. The scenario included an enrollment service and a student service, the enrollment contained the id of the student and the consumers needed a list of enrollments with meaningful student details.

By using LINQ?s capability of joining two IEnumerables and the capability of creating projections "on the fly", what I first thought would take some time to build actually turned out to be a short but sweet pleasure.

This is (kind of) what I did:

List<EnrollmentItem> enrollments; 
List<StudentItem> students; 

using (EnrollmentServiceClient client = new EnrollmentServiceClient()) 
{ 
	enrollments = client.ListBy(date, testCenterId); 
} 

List<string> studentIds = (from item in enrollments 
				  select item.StudentId).ToList(); 

using (StudentServiceClient client = new StudentServiceClient()) 
{ 
	students = client.ListFor(studentIds); 
} 

List<StudentEnrollment> response = (from enrollment in enrollments 
						join student in students 
						on enrollment.StudentId equals student.Id 
						select new StudentEnrollment () 
						{ 
							FullName = student.FirstName + " " + student.LastName, 
							Code = enrollment.CertificationCode, 
							Certification = enrollment.CertificationName, 
							Status = enrollment.Status.ToString(), 
							InvoiceStatus = enrollment.InvoiceStatus.ToString() 
						}).ToList(); 

return response;

This is where I went from loving LINQ to worship it, it just solved an enterprise grade challenge for me with very little and very beautiful code.

In SO terms what it just created was an Entity Composition Service (ECS) which often are needed after applying the "decomposition principle" on your enterprise services. Basically what the principle says is that data should be tied to the service which implements the process that the data belongs to. Like in the above scenario where the services of handling student information and that of creating enrollments are logically and physically separated in the enterprise, the data then naturally lives in different corners of the enterprise and when creating lists like this it needs to be united.

This can be kind of hairy to pull off; luckily LINQ saved the day!

Technorati-taggar: ,,


kick it on DotNetKicks.com
 

Comments
1/29/2008 3:37:00 PM   http://www..thedatafarm.com/blog   -   Julie Lerman
 
This is such a great post, Patrik. I had to blog about it! [http://blogs.devsource.com/devlife/content/net_general/when_linqs_power_really_sinks_in.html]
 
2/6/2008 11:24:00 AM   http://ljusberg.se/blogs/smorakning   -   Anders Ljusberg
 
That's so funny, I got exactly the same feeling when I did something incredibly similar a couple of months ago. I spent the rest of the day walking around the office with a grin on my face showing everyone the wonders of LINQ..!


Comment
Title:
Your name:
Your url:
Text:
Please enter the text from the image: