Archive for August, 2009
LINQ queries over a LightSpeed EntityCollection
Tagged as LightSpeedIn LightSpeed, you can define a LINQ query over the entire database using the Query method, or more conveniently using a strong-typed unit of work. LightSpeed pulls back only the entities or aggregates (e.g. count or sum) that you need.
Suppose, though, that you have an entity, say a Customer, and this entity has a child collection, say Orders. Loading that child collection is an all-or-nothing thing: as soon as you reference the Orders property, LightSpeed goes and fetches all the orders in the database associated with the customer in question. What if you just want to get the top ten orders by value? Or the number of orders? Of course, one way is to figure out a suitable database query:
int orderCount = unitOfWork.Orders.Where(o => o.CustomerId == customer.Id).Count();
With recent nightly builds, however, we’ve added a new LINQ Query method that encapsulates this for you. To use this, you apply the Query method to an entity, passing an expression representing the collection:
IQueryable<Order> orders = customer.Query(c => c.Orders);
This doesn’t load the Orders collection: like the normal LINQ Query methods, it just sets up a query. So now you can write:
int orderCount = orders.Count();
This will perform a COUNT query against the database; you still don’t incur the overhead of loading the Orders collection. Similarly, this query:
List<Order> topTenByValue = orders.OrderByDescending(o => o.Value).Take(10).ToList();
retrieves only the ten orders specified.
If you want to add or remove orders, or you want to data bind to the Orders collection, you’ll still have to reference the Orders property and load all the customer’s orders. But if all you want to do is perform queries against the child collection, this enables you to do so more efficiently.
I like queries. How can I make the syntax nicer?
You might find that expression syntax a bit cluttered. If you’re planning to make significant use of this feature, you’ll probably want to encapsulate it in a nice property or extension method. For example, you could write an extension method:
public static class CustomerExtensions { public static IQueryable<Order> Orders(this Customer customer) { return customer.Query(c => c.Orders); } }
and then you could perform customer order queries like this:
int orderCount = customer.Orders().Count(); List<Order> topTenByValue = customer.Orders().OrderByDescending(o => o.Value).Take(10).ToList();
Notice the brackets after Orders(): this tells C# to call the Orders() extension method rather than accessing the Orders property.
Take it for a spin
Want to try it out? Grab the latest build of the free LightSpeed Express Edition, or if you’re a licensed customer download the nightly from the store, and give it a go!
MegaPack: Save up to 30% on developer tools
I’m pleased to officially announce our full product line subscription offering: The Mindscape MegaPack.
To celebrate, we’re offering a fantastic discount for everyone!
The Mindscape MegaPack contains the following per developer:
- LightSpeed Professional
- WPF Property Grid
- WPF Elements – 13 essential WPF controls
- WPF Flow Diagrams
- WPF Star Diagrams
- SimpleDB Management Tools
- All new products released by Mindscape for the duration of your subscription
- Source code (Premium edition only)
- Priority support (Premium edition only)
We offer two editions: Professional and Premium. The difference between the two is that the Premium Edition includes source code for the products as well as Priority Support.
Purchase today!
To celebrate the launch of these suites we’re offering a sale of up to 30% off the retail price of the Mega Pack editions. This stacks up to tremendous savings for you: not only will you be saving with our launch special but further saving by virtue of the packs already being a discount on purchasing individually.
The sale strictly ends at the end of Friday the 4th of September.
Mega Pack Premium: Normally $749, sale price $499 per developer
Mega Pack Professional: Normally $599, sale price $399 per developer
Don’t delay – lock in the savings and improve your development productivity today.
Questions and Answers
How much will subscription renewal cost?
Typically the renewal rates on the subscription will be approximately 40% of the retail price. This will provide an additional 12 months of benefits. If you elect not to renew you may continue to develop with the versions that were available as part of your subscription however you will not have new releases made available to you.
I’ve seen this in the store – is it really new?
We setup the Mega Pack products in the store some time ago due to the demand for all inclusive suites however we have not publicly announced or advertised the availability until now.
If you have any questions please leave a comment or contact me directly: jd@mindscape.co.nz
Take advantage of the special launch sale prices here.
Buy Visual Studio and MSDN online in New Zealand
Great news for folks living in New Zealand – today we launched the New Zealand Visual Studio Store at www.getvs.net.nz.
Now New Zealand developers and companies can purchase Visual Studio licenses and MSDN subscriptions completely online and in New Zealand dollars. This is a big step forward over the existing approach to purchasing Microsoft software for developers in New Zealand. You can also buy Mindscape products there in NZ dollars and we plan to offer further Microsoft and third-party products over time.
What’s available in the store?
We’ve started the store with VS 2008 / MSDN options (1 year, 3 year subscriptions) and the entire Mindscape catalogue. We’ll be growing the lineup over time so if there is a product you’d like to get your hands on that is not listed then please let us know – we’ll help make it happen.
One thing we’ve tried to do is to make it easy for you to identify the editions of products that make the most sense to you – no more needing tensor calculus to work out what licenses you need! We’ve also tried to make pricing a little simpler by pricing everything in New Zealand dollars.
However, if you know you need a licensing option that’s not listed, or you’re not sure what products you need, just get in touch, we’re happy to help.
I’m not a New Zealander!
Just ignore this post! Our main outlet for Mindscape products continues to be our own online store: there’s no change there!
For Visual Studio and MSDN licences, you’ll need to contact a local retailer just as you do at the moment. Due to how Microsoft licensing is divided up amongst countries and regions, we’re not able to sell Microsoft products to customers outside New Zealand.
Additional services
Through the Visual Studio Store we are also providing service options to help development teams in New Zealand. The initial offerings include helping larger organisations ascertain what their licensing requirements are as well as more technical workshops on getting the most from your software infrastructure. As with everything, we’re open to feedback on any support and service options you would like to see.
Go and check it out and let us know what you think!
Tips for rich client development
Tagged as LightSpeedA common question that comes up for almost every object relational mapper is how to use it effectively in a desktop client environment. I wanted to share some of the tips we have in achieving successful desktop client development with LightSpeed.
What’s the problem?
There tends to be two specific issues that make developing connected desktop applications a challenge:
- Completely stateful environment
- Often in a client/server setup
In this post I’ll explore some tips for working more easily with stateful applications.
By a stateful environment, I mean that you’re not limited to the stateless request/response situation of web development. Despite the challenges of web development, the request/response type approach to building applications means there are environment imposed boundaries. For example, the start of a request, the end of the request, etc.
These boundaries initially may have seemed like pain points for developers moving to web development for the first time. However, in my opinion, we have subsequently found that this imposed application design turned out to be a blessing in disguise. Web applications are almost forced to work in atomic change sets, while desktop applications can be far more fluid due to their statefulness. In a desktop application it is therefore tempting to work in a non-transactional manner. But this doesn’t lend itself the Unit of Work pattern because the pattern is transactional in nature.
So in the development of desktop applications it is important to try and identify or create the boundaries that your application will use. In a sense, you’re creating the same type of fundamental work patterns as the request/response type nature of the web but based on your own rules. The benefits of doing this is that your application code is maintainable and your entity life cycle management is simple to work with.
Three tips to get you started:
Tip 1: Design patterns

While all applications should use a higher level design pattern for structure (MVP/MVVM/MVC/Whatever) it’s more important for desktop applications as they otherwise do not enforce any natural boundaries (such as managing the request/response life cycle). It takes discipline to take the freedom of the desktop and impose your own boundaries and stick to them.
Why labour this point? The Unit of Work design pattern works best when tied to a certain section of actions to be undertaken. This means in an MVC structured application that while the rest of your application is doing various things you are likely to only need to start and finish a unit of work in your controller – usually in very short lived manner. A short lived unit of work is the best way to use the pattern.
Tip 2: Dialogs
![]()
One natural boundary in a desktop application is a pop up window or dialog. Take for example an application where you may wish to edit an employees details. The user double clicks an employee, a dialog appears and they can edit any of the employee details. They can then elect to save those changes or cancel the operation.
Here’s how to structure this type of interaction:
- Controller uses a unit of work to fetch the employee to edit
- Controller closes the unit of work
- Controller creates the view and exposes a model which the view can bind against (in this case, we could bind directly to our employee entities properties)
- At some later stage, after editing, the user clicks “Cancel” then the dialog closes.
- Or, at some later stage, after editing, the user clicks “Save” then the controller creates a new short lived unit of work, attaches the entity, and saves the changes.
- Everything is nicely and neatly done!
This represents a nice and easy way to work with short lived units of work along with your higher level design pattern enforced boundaries to ensure you have a nice and clean way of working with entities. Obviously application specifics may be different (edit inline, certain users can edit only certain fields etc.); however hopefully this will provide a basis for how you can work with entities cleanly.
One word of warning – if you’re developing a multi-user system you will want to think about concurrency. For example, you may enable optimistic concurrency and trap that exception on save if another user has edited the same entity between you loading it and saving it. How you deal with such a concurrency violation would be application specific but it’s worth keeping this scenario in mind.
A second word of warning – if you’re representing a complex graph of objects that are being edited then you’ll need to be wary of items that could be lazy loaded, or otherwise require an active unit of work to be associated with the entity. If you wish to make use of these capabilities you’ll need to look at using a longer running unit of work so the entity can maintain a connection to the database.
Tip 3: Refresh buttons
![]()
Often when building a desktop application there is the presumption that you can maintain a live data feed in the UI because you are removed from the request/response process. Avoiding this situation is generally wise because rebinding entire grids or views means that you end up fighting the nuances of the users state (“they had field three in row 97 for the employee entity in edit mode – return to that state”).
The best solution here is to make liberal use of “refresh” buttons in your application. You grabbed the data once and wait until the user has decided they wish to refresh the information before you actively go looking for changes.
Again, this is where the design patterns help out. Having a controller able to call a generic Refresh() method which rebinds the primary UI has the benefit of being easily callable from elsewhere in the controller. For example, if a user elects to edit an employee then when the controller has saved those changes it could also call the Refresh() method to ensure that as the dialog closes the primary UI is also updated to show the changes (and any other changes that other users may have made since the last refresh).
What tips do you have?
These are just a few tips but every developer has experiences to share. What are some of the tips and tricks you have found? We would all love to hear them!
SimpleDB Select support in LightSpeed
Tagged as LightSpeedWhen we first shipped SimpleDB support in LightSpeed, the only available query interface to SimpleDB used the Query APIs and Amazon’s own query expression syntax (technically known as “Amazon’s crazy-ass query language”).
Needless to say, about ten minutes after we shipped, Amazon announced a new Select API with a simpler, more expressive and almost SQL-like expression syntax, and decreed that henceforth this was the preferred query interface and that this was where all the new cool toys would be turning up and that anyone who continued to use Query and ACAQL was a Commie mutant traitor and would be tracked down and “deprecated.”
We immediately sprang into action and decided to put off supporting Select until it had enough cool toys to make it worthwhile. And I’m pleased to announce that the day has finally come. Okay, most people wouldn’t consider the IS NULL operator a particularly cool toy, but trust me, when you’re doing database programming, you take what pleasures you can get. (Actually, IS NULL is cool, because it means you can use the LightSpeed soft delete feature over SimpleDB. See? This isn’t just internal technical noodling after all.)
So from the 21 August nightly, LightSpeed will have experimental support for the Select API, and will use the Select API by default. Since this support is experimental, we’ve provided an option to revert back to the Query API if it all goes pear-shaped or if you’d rather wait for Service Pack 1: just add “API=Query;” to the SimpleDB connection string.
You’ll be able to download Select support from about 1430 GMT — Express edition from here, retail editions from the store. As always, please let us know about any bugs or issues you run into.
![]()
BrainDump (1)
Community Code (1)
Events (6)
General (27)
Lab Samples (2)
LightSpeed (112)
MegaPack (3)
News (37)
Products (51)
Projects (4)
Screencast (6)
Silverlight (1)
Silverlight Elements (1)
SimpleDB Management Tools (8)
Visual Studio (4)
VS File Explorer (4)
WPF (28)
WPF Diagramming (11)
WPF Elements (15)
WPF Property Grid (20)
![]()
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007




Posted by Ivan Towlson on 30 August 2009 



