Ocean for Silverlight – Enables Sharing Business Objects With WPF

June 1, 2009

Ever since Silverlight version 1 was announced, I’ve been working with my WPF and .NET back-end libraries to ensure that I could reuse business entity objects between the tiers regardless of the UI technology. 

I wrote these demos in NYC while waiting for my flight home.  This application takes advantage of the OceanFramework that runs in WPF, Silverlight and .NET 2.0 libraries.  The two versions, WPF and Silverlight use the same exact files.  I use Project Linker to to keep the two projects in sync.  (FYI:  The OceanFramework can also be used with Windows Forms applications.)

Using file linking in Visual Studio, I’m sharing the same exact business entity object file, Person.vb between WPF and Silverlight projects.

Validation

Before diving into the application I want to share some thoughts on validation.

Real world business applications use validation libraries to validate their business objects.  Validation libraries contain rules and offer one or more techniques for calling their methods.  Examples of validation libraries are the Microsoft Patterns and Practices Validation Application Block, Rocky Lhotka’s CSLA, my Ocean library and validation rules written by developers for their projects. 

Well designed validation libraries are UI agnostic.

Validation libraries are used to validate business objects in the UI layer, Business Layer and Data Layers.

Follow this simple workflow:

  • UI loads Person object
  • User edits Person object
  • User clicks the Save button
  • Person is validated
  • If valid, Person passed to Business Layer
  • Business Layer checks to ensure the object is valid, if not an exception is thrown
  • Business Layer performs computations on Person
  • Person is validated again
  • If valid Person passed to Data Layer
  • Data Layer checks to ensure the object is valid, if not an exception is thrown
  • Person passed to data base
  • Database performs its own checking then writes the records

From the very simple above example, we can see that validation needed to occur in two layers, UI and Business.

Also very important to note is that when the object was validated in the above example, the same rules were applied against the Person object in both layers.

The italic text steps are the sanity checks performed by each layer as it receives an object.  I don’t allow invalid objects to cross a tier boundary.

Another example scenario is a business process that calculates bills for customers.  In the case, there is no UI interaction during the process but you know that those objects involved are all validated before being sent to the data base.

Surfacing Validation Errors

  • IDataErrorInfo
  • Throwing exceptions in property setters

The IDataErrorInfo interface provides a simple way for business objects to surface their validation errors to the UI.  WPF and Windows Forms support this interface.

Throwing exceptions from property setters is another way for business objects to surface their validation errors to the UI.  Silverlight and WPF support this method.

If you have not read my rant on throwing exceptions from property setters read Fort Knox Business Objects.

Important:  When we develop business systems, we must take into consideration the fact that, “business objects must exist and interact with other objects in many different layers of an application.”  This is one of the reasons I’m not in favor of throwing exceptions from property setters.  Business layer and data layer code would be much harder to write if the object was throwing exceptions when properties are changed.  It’s much easier to work with an object, then before the object is allowed to move from one tier to another, validate the object.  If valid let it travel to the next tier.  If not then either have the layer code attempt to correct it or throw an exception because something exceptional occurred..

Silverlight 3 and Surfacing Validation Errors

I love the new Silverlight 3 default validation states and the UI features when a control is invalid.  Nice job Silverlight Controls Team!

I want to use IDataErrorInfo in Silverlight 3, however it currently does not support it.

So on Friday night in NYC, I stayed up and wrote a Silverlight TextBox that cleanly supports IDataErrorInfo.  In fact I did a demo of it on Saturday in NYC.  On Saturday night I did much more testing and found some edge cases where my implementation could have problems.  I even took a stab at using an attached property and attached behavior too.  Still always running into a nagging snag.

My solution to the problem was to give the Ocean Validation code the “option” throw an exception in the property setter when the business object is bound to the UI.  When it’s not bound to the UI it will not throw an exception.

Using this technique, I can now use the same business objects across the tiers and still play nice with Silverlight 3 and take advantage of all the new Silverlight UI goodness.

Sharing Business Objects

If your validation library is small enough and can be compiled in Silverlight as well as WPF or .NET 2.0, you are ready to go.  Currently Ocean for Silverlight weighs in at a very light 56KB. 

LinkedFiles

The above image shows a Person file that is linked to a file in another project.

Frameworks

The above image shows the some of the referenced assemblies.  Silverlight can only reference other Silverlight assemblies.  I’ve solved the problem by keeping all the code in the OceanFramework and linking all files to the OceanFrameworkSilverlight project.

When the two business entity objects are compiled, they provide objects that can travel across the tiers while utilizing the same exact business object files and exhibiting the same logical behavior in all tiers.

The above MissingSilverlightInterfaces project contains the Sytem.ComponentModel.IDataErrorInfo interface since it’s not currently part of the Silverlight runtime.

Silverlight Application

SilverlightValidation 

The above image shows the UI surfacing a validation error.  Profession is a required field.  Notice that the Save button is disabled.  This is accomplished with a data binding to the business object’s base class. 

When a person is selected the Person Details form will animate into position.

The below image shows the code for this validation rule.

ProfessionProperty

The above Ocean StringLengthValidator requires a string length at least 1 and no more that 30 characters.

The CharacterCasingFormatting attribute causes the business layer to case correct this field.  This works in WPF and Silverlight.

I like applying rules this way because the code is very easy to read and generate.

Ocean provides many validation rules and case correcting rules.  Rules can be applied using attributes or adding them in code.

WPF Application

WPFValidation

In the above image, Profession is empty but is a required field.  The * denotes an invalid field.  Notice the button is disabled.  Again, a simple data binding to the business objects base class.

WPFApplication

The above image shows the details form in a valid state.  The button is enabled and the record can be saved.

I used this project in the WPF Line of Business Tour in the Layout session.  I showed how to use a WrapPanel as the ItemsPanel in a ListBox.  Then when the GridSplitter is used to give the ListBox more horizontal space, the WrapPanel kicks in.

Download

The download contains a single folder with the the 4 projects and an additional folder that contains the Ocean frameworks.  I’ve only supplied the Ocean frameworks in .dll format for this demo.  I’m in the process of updating the libraries and do not want anyone to have version problems.

Also I wanted to keep this application simple so that the “code sharing” would be the focus and readers not getting lost in a solution with too many projects.

FYI:  I’ll be posting another application soon that shows all the layers in action including the WCF projects that move the data back and forth.

For now, understanding that you “can easily” shared business objects and their validation rules across the tiers, is good news!

After downloading the code, change the file extension from .doc to .zip.  This is a requirement of WordPress.com.

Sharing Entity Objects Download (158KB)

Have a great day,

Just a grain of sand on the worlds beaches.


Ocean Framework Released – MVVM for WPF Line of Business Update

January 26, 2009

WPFLOBMVVMapplicaiton1

WPFLOBMVVMSolutionItems

As part of my new MVVM articles, I’m posting the Ocean Framework source code.  The Ocean Code Generation pieces will follow in February.

The M-V-VM Home Page has been updated.

The WPF Line of Business – Demo Application Source has been posted.  (Ocean Framework source is part of this.)

The WPF Line of Business – Introduction as been posted.

This series of MVVM articles targets those unfamiliar with MVVM or who are learning this wonderful WPF UI Design Pattern.

Close

I hope you find this material useful and can learn more about MVVM and WPF from it.

Have a great day!

Just a grain of sand on the worlds beaches.


MVVM Update

December 8, 2008

I know that many developers are waiting of me to post the MVVM code.  I have spent the last three weekends tracing memory leaks that I thought were in my libraries.  I just didn’t want to post code that I knew had a memory leak.  I finally tracked the problem down today to a control.  For now, I have removed that control from my projects.  I will do a blog post on the control as soon as the control vendor has validated my findings from today. 

I will post the WPF LOB MVVM code and article on Monday, 8 December 2008.  The MVVM applications are all ready, I just need to write the article and make a few videos.  The main application I’ll post tomorrow has the Unit Tests to run the application without the UI.

This three weekends of lost time has set my delivery of Ocean back too.  I hope to post Ocean by the 18th or 19th of December.  The documentation will take the most time.

Here are some things happening in the MVVM spaces for WPF LOB developers that I know about:

  • Jaime Rodriguez, possibly Josh Smith, possibly Beth Massi and myself will be doing an all day teaching event on WPF LOB MVVM in Los Altos near the end of January 2009.  As soon as registration opens I’ll post a message here.
  • Adam Kinney and I will be recording another Channel 9 video over the next 10 days covering WPF LOB MVVM.
  • I know of another MVVM video being recorded by an MVVM Rock Star next week.  Details to follow.
  • Josh Smith and I are getting together in New York City this weekend and will post some MVVM coolness next weekend.
  • The WPF Disciples had a long and interesting thread on MVVM that you can read here:  WPF Disciples Thread.

Hope you have a great day!

Just a grain of sand on the worlds beaches.


Learning WPF M-V-VM

November 8, 2008

For the last few months I’ve been studying M-V-VM and other WPF UI design patterns.

I have selected M-V-VM as my UI design pattern of choice for the soon to be released Ocean framework and code generation system.

I have read much of what Microsoft Partner Architect John Gossman, members of the WPF Disciples have published and specifically what my great friend and Microsoft & Code Project MVP Josh Smith has written on the UI design patterns.

Without any doubt, the best and clearest example of WPF M-V-VM I’ve seen is in Josh’s latest achievement, Crack.NETIf you want to learn M-V-VM from an expert, download Crack.NET source and study Josh’s masterpiece.

Applied

I’m sitting here at the Silicon Valley Code Camp having just taught a M-V-VM for WPF LOB session.   The applications I presented were all 100% testable.  The main application that has all the unit tests, can be run without ever displaying any UI.  It is amazing the power that M-V-VM delivers with such simplicity.  

I need to make a few videos and write up a blog post and I’ll share the code with you after next weekend.

I’m finishing up Ocean’s UI and it uses M-V-VM.

Below is a slide from the presentation.  It covers the layers found in an WPF LOB application.

WPFLOBMVVM

Raleigh Code Camp

Next weekend, 15 Nov 2008, I’ll be in Raleigh, NC for the annual fall RDU Code Camp.  I’ll be launching Ocean at this code camp and will be doing several other sessions as well as being available in the Open Spaces.  See you there!!

Close

Have a great day,

Just a grain of sand on the worlds beaches.


Ocean’s Birthplace

October 30, 2008

I’ve been blogging and speaking about Ocean and wanted to share the birthplace of Ocean with you.  Ocean was born in the beautiful waters off the Alaskan coast.

The middle two images were shot from my suite at sea.  For me, it was very relaxing to sit and hear the sounds of the ocean and view the breath taking scenery while writing the code for Ocean.

When we got in port, we took the time to do the normal cruise thing which is to go on excursions.

Humpback Whales Bubble Net Feeding

On our whale watching excursion we were very lucky to view humpback whales bubble net feeding.  I was able to capture this spectacular event of nature 4 times.  The whales just keep repeating the tactic for feeding themselves.  Thought I would share the first episode with you.

  Silverlight Video – Humpback Whales Bubble Net Feeding (1:30)

BubbleNetFeeding

Other Links To Bubble Net Feeding

http://search.live.com/results.aspx?q=bubble+net+feeding&go=&form=QBLH

http://www.arkive.org/humpback-whale/megaptera-novaeangliae/video-08a.html

http://www.alaskapassages.com/bubblnet.htm

Ocean’s Birthplace

OceanBirthPlace

Yes, that is Karl’s 24″ monitor in my suite.  After viewing the glacier on deck, I returned to my serene environment to work on Ocean.  Stress free and chilled out really turns out some cool and innovative code and it’s way better than therapy or medication (LOL).

OceanBirthPlaceTwo 

Any Caption or Words Would Just Take Away From This

Eagle

Have a great day,

Just a grain of sand on the worlds beaches.


Karl Why Did You Write XAML Power Toys and What is Ocean?

October 15, 2008

In the last few days I’ve been getting questions about XAML Power Toys like; why did I write it; what was the motivation behind the work, etc?

I was actually beginning the UI development in my soon to be released Ocean Platform (details below) and found myself looking at creating 8 forms for Ocean’s metadata maintenance and code generation.

I was thinking, if I lay Ocean down for a week or two, I can write a tool that will allow me to author Ocean’s UI in no time at all.  Ocean has it’s own built-in advanced UI generation but Ocean is designed to consume metadata.  Since I didn’t have metadata yet, I needed to tool to help me author the UI forms. 

You see, I really didn’t want to hand type 8 more forms if I could get the computer to do it for me.  So because I’m lazy, XAML Power Toys was born.

Time

Time is one resource that can not be replaced.  It goes by so fast and seems to slip between our fingers very easily.  It’s vulnerable and somehow “things” inject themselves into our time.

As developers, we can (should) take steps to maximize our time.  We can maximize our time by having the correct tools, proven workflow and an understanding how to use these. 

We developers need to manufacture time by accomplishing tasks faster and with laser surgery accuracy.  In our world the goal is to deliver defect free, maintainable, standards based code.

The best way I know to meet this goal and to manufacture time is to generate as much production code as possible. 

This gives the developer more time to allocate to a better application design, to learn new patterns and to keep up with the flow of new technologies that can only be compared to the water flowing at Niagara Fall’s.  I wrote Ocean to give me time back.

Ocean

Back in May of 2008 I wrote this blog post:  Metadata – a Voice Crying in the Wilderness, Hey! I’m Over Here.  This was a call to architects to consume the warehouses of metadata that most projects have but do not ever get into the hands of the developers.  You say that’s not entirely true.  OK, then why do developers type ToolTip text into their forms?  Why is a developer typing XML comments on business objects? 

These are two over simplified examples of metadata that is located in a design document somewhere, but is not in a format or physical location that makes it easily consumable by the developer or a code generation product.  The term disparate data storage comes to mind.

I strongly agree with one of my mentors, Bill Jones that developers should own their code generation system.  We live in a very fast and volatile world.  Things simply change a lot.  Today’s architects and developers need full access to the code generation system’s data model, templates, template engine and generation code.  Yes, I know this will raise a few eyebrows, especially if your company provides Code Generation software.  But I wrote Ocean because I couldn’t find an off the shelf Code Generation product that did what I needed, was easy to understand, manage and had examples for VB.NET developers.

Right behind code generation an application of any size requires a set of libraries that sit on top of .NET that provide the services a Line of Business (LOB) application requires.

Ocean provides a light weight, comprehensive framework that works in WPF and Silverlight.  This was one of the requirements of Ocean, that it support Silverlight. 

Ocean is divided into five assemblies and I’ll be adding one more that is specific to Silverlight.  Below are some of the highlights of each assembly.

OceanFramework

  • Ocean is the UI for maintaining the metadata the Ocean Code Generator consumes.  It also makes calls to the OceanCodeGenerator to generate an object like a stored procedure, trigger, DAL, BLL, entity object or UI object.  I’m going to also add support to generate MVVM ViewModels.
  • OceanCodeGenerator is an ASP.NET web that runs in a secondary AppDomain under Ocean’s control.  This assembly takes a request from Ocean and generates an object.
  • OceanFramework is the code that runs in WPF and Silverlight.  It requires no code changes between the two platforms.  This assembly provides declarative validation on business entity objects, UI case correction and business entity base classes.  The validation is attribute based, but can also be declared in code.  Compiled, this library weights in at only 62KB.  For Silverlight, that is a real winner.
  • OceanFrameworkSQL is a light weight and super fast replacement for the DAAB I loved in .NET 1.1.  This library weights in at 29KB.  In addition to enabling single line of code database calls from your DAL, this library also has built in concurrency checking and object building from a DataReader or DataRow.  I’ve run tests and was able to build 19,000 unique objects per second using this library.  This is on par with LINQ To SQL and other object loading techniques.  Check out my three blog posts on Performance to review the test data.
  • OceanFrameworkWPF provides WPF specific code, commands, converters, UserControls and Custom Controls.  Most of this code is already on my blog.
  • OceanFrameworkSilverlight to be written soon.  Will provide Silverlight specific code and controls.

Ocean’s Workflow

Ocean does not claim or attempt to be all things to all developers.  It is not a silver bullet.  It’s a metadata driven LOB application development and maintenance tool.

Note that the Ocean libraries can be used without the code generation pieces.  You have probably noticed the XAML Power Toys uses the OceanFramework library.

My current workflow is:

  • Design the data model using SQL Server Tools
  • Sync Ocean with that database
  • Enter in all metadata for the application
  • Press a button and all the stored procedures, DAL, BLL and Entity objects are immediately generated
  • Easily generate additional required stored procedures, DAL, BLL and Entity objects for subsets of a class.  For example, the customer class has 25 properties.  The customer search results only has 5 properties.  Ocean allows you to create everything for this search results class in a split second and assists you with creating the DataTemplate for the UI.
  • Use Ocean to generate each UI object as the developer requires
  • Ocean handles changes to the data model and application requirements with ease by allowing some or all objects to be recreated on demand.

Closing

So on nights and weekends, I’m back to working on Ocean and hope to ship, including detailed documentation by the end of October or early November.  When released, Ocean will appear on Code Project and my blog. 

I’ll be presenting Ocean and doing walk through’s in the open spaces at the Fall Raleigh North Carolina Code Camp on 15 November.  Register if you’re in the area or can make the trip.

Have a great day.

Just a grain of sand on the worlds beaches.