The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 3,824,578
since: 19 Jan 2005

Using CLINQ 2.0 as a Continuous Model-to-Model Bridge

posted Fri 09 Jan 09

When building desktop applications that consume data from some external source, be it a Web Service or some messaging middleware pipe or even monitored flat files, a few things commonly happen. The first is that we realize that the format of the information we're consuming is almost never exactly the way we'd like it to be when we rig up our UI. Secondly, we don't want our UI to depend on an external service contract, so we usually decide we're going to build a separate ViewModel that is a binding-friendly, domain-specific representation of exactly what we want our GUI bound to. A few minutes later after scratching our heads a bit, most of us end up realizing, "Hey, I need to build an anti-corruption layer here." We then busily go about writing some semi-reusable code that will perform the task of consuming the service model and translating it into view model, and potentially go the other way as well though that's usually less important.

Let's take a look at a concrete example. Let's say you are writing a very rich, WPF-based desktop application that communicates with a Web Service for virtually all of it's data. The service contract has been split into a shared Assembly containing WCF-attributed interfaces so that you can still have strong typing at the same time as loose coupling. So far so good. The problem is you don't want to bind your GUI directly to the service data and the service data doesn't really lend itself all that well to what you're looking to do.

Let's say your WPF application is consuming market data ticks. Your service model gives you the following properties for a tick (bid ticks):

  • Price
  • Side
  • Quantity
  • Time Stamp

Your view model is designed to be useful to a WPF developer, not to a service developer. As a result, you've got a generic graphing control that just plots a time series data and only cares about the decimal value, the color of the plotted point, and the time stamp. Using CLINQ, you can just write a CLINQ query that will automatically, and continuously, translate from the service model to the view model any time anything in the service model changes.

ReadOnlyContinousCollection<ViewModelTick> ticks =
    from serviceTick in ServiceModel.AllTicks
    where serviceTick.TimeStamp >= marketOpenTimeToday
    select new ViewModelTick()
    {
        TimeStamp = serviceTick.TimeStamp,
        Value = serviceTick.Price,
        Color = serviceTick.Side == TickSide.Buy ? Color.Blue : Color.Red
    };

The thing that I like about this is that now ticks can be exposed as a property and bound directly to a WPF GUI. You could rig up a converter that converts a collection of ViewModelTicks into a PointCollection so that you could convert it into a Polyline (something similar to this is done in the CLINQ 2.0 stock monitor demo). You are declaring your intent and then simply letting CLINQ take care of making sure that both models are 100% synchronized in a real-time, asynchronous manner.

What I find surprising is that CLINQ was originally built for one very specific purpose - Continuous LINQ queries... But since then, it has taken on a life of its own and become such an invaluable part of my daily life that I am pretty sure I wouldn't know how to write a WPF application without it.

tags:            

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Joe left...
Fri 09 Jan 09 9:06 am

Hi Kevin, totally agree. We're eagerly waiting on the v.2 upload to codeplex.


2. Kevin Hoffman left...
Fri 09 Jan 09 11:14 am

I already uploaded the ZIP file but apparently it didn't go public. I'll take care of that this weekend.


3. Anthony left...
Mon 12 Jan 09 7:27 am

I think I would like to check out your CLinq library, but I find your license to restrictive, i.e. this license doesn't allow any use of your code in any commercial work that I don't want to release with source code. Can you please think about a BSD license for v2.0?


4. Kevin Hoffman left...
Tue 13 Jan 09 6:19 am

The license allows for unlimited commercial use.


5. Anthony left...
Tue 13 Jan 09 12:44 pm

Maybe I am misunderstanding something but you release using the Common Development and Distribution License (CDDL) http://www.codeplex.com/clinq/license

And while this license does allow for commercial use it also says in section 3.1 and 3.2:

""" 3.1. Availability of Source Code.

Any Covered Software that You distribute or otherwise make available in Executable form must also be made available in Source Code form and that Source Code form must be distributed only under the terms of this License. You must include a copy of this License with every copy of the Source Code form of the Covered Software You distribute or otherwise make available. Yo must inform recipients of any such Covered Software in Executable form as to how they can obtain such Covered Software in Source Code form in a reasonable manner on or through a medium customarily used for software exchange.

3.2. Modifications.

The Modifications that You create or to which You contribute are governed by the terms of this License. You represent that You believe Your Modifications are Your original creation(s) and/or You have sufficient rights to grant the rights conveyed by this License.

"""

Thus, by using your your code, the way I am reading it, means that I must release under the CDDL and thus release both a link to your source code and mine...

This is what I was saying.. BSD allows use of code without this viral side effect.

Thanks for listening..


6. Anthony left...
Wed 14 Jan 09 12:47 pm

Hi I looked into the issue and it seems that as long as I don't modify any of your code that I can call into it and can keep my code proprietary

http://www.opensolaris.org/os/about/faq/licensing_faq/#CDDL-combo


7. Kevin Hoffman left...
Thu 15 Jan 09 6:30 am

I modified the license and it's now using the "New BSD" license, which is pretty much as non-restrictive as you can get. About the only thing necessary is to keep the BSD license in the code.


Tag Related Posts

Smart, Deep Property Notifications in CLINQ v2.0

Tue 07 Oct 08 1:15 P GMT-05
tags:          

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

CLINQ v1 Demo - Network Message Filtering

Wed 09 Jan 08 7:47 P GMT-05
tags:        

Continuous LINQ v1.0.0.0 Released

Tue 08 Jan 08 4:53 P GMT-05

Building a Ledger Style for WPF Grids

Tue 21 Aug 07 3:30 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Exploring the MVC Pattern in WPF

Tue 10 Apr 07 12:51 P GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags: