using CSLA business objects with Silverlight and ASP.NET MVC

using CSLA business objects with Silverlight and ASP.NET MVC

Old forum URL: forums.lhotka.net/forums/t/10987.aspx


artsd posted on Wednesday, December 14, 2011

I am currently using CSLA with Silverlight and everything is great. I now have a need to build an ASP.NET MVC application. I read the other threads on this forum about this same issue and didn't see my particular question answered.

With Silverlight's client side bus obj and excellent xaml data binding, we can do things like instant field validation, bus rules that cause property changes, property changes that cause other property changes, dynamically changing property security with CanWriteProperty overrides, etc -- and the xaml ui responds as expected.

Here is a simple example. Two radio buttons A and B. When A is selected, need to set property X to something and property Y to something else. When B is selected, need to do different property changes.

It seems like in MVC I would need to do postback for every single field change in the html ui to get the same effect. Plus I would have to do server side caching of bus obj so postback doesn't have to fetch obj from db every time because the order of property changes might be important to the way the bus obj enforces rules so I cannot start with a freshly loaded or created obj instance every time,

How are people handling this situation?

Similarly, Csla.Web.Mvc.HasPermission() looks great except that it only is used when the view is rendered -- not on property change.

I am not an HTML/JavaScript developer so maybe I am missing something obvious.

RockfordLhotka replied on Wednesday, December 14, 2011

You are correct, anything but the simplest validation requires either a postback, an AJAX call, or that you duplicate your business logic by writing javascript in the page.

Typically people use a postback, though that's changing as the demands for more Windows-like behavior in web pages increases. The postback solution works fine with CSLA objects - the CslaModelBinder helps ensure the user gets a consistent result based on the rules in the business objects. I discuss and demonstrate this in the Using CSLA 4: ASP.NET MVC ebook.

The AJAX approach is largely unworkable for anything but the simplest objects/forms. As soon as you have interdependent properties (or worse, interdependent objects) then the complexity of sending the data server-side for validation gets pretty ugly.

But as html 5 becomes more accepted, perhaps we'll see more people write validation and business logic in javascript. Having all this duplicate code to maintain in js and C#/VB both seems costly to me - but people keep telling me html 5 is the future, so perhaps we're ready to accept this cost?

artsd replied on Wednesday, December 14, 2011

Thanks Rocky. Nothing compares to the power and ease of development of Silverlight for rich browser based LOB apps. I wish Microsoft would be more vocal in its support because my customers are now questioning the choice of Silverlight even though MS has promised at least 10 years of support. Sorry I missed you last weekend in Orlando. Cory (Magenic Atlanta) invited me to the Sunday dinner but I have too many December deadlines so I couldn't attend.

asinc replied on Wednesday, December 14, 2011

One alternative that I am testing now is to use a combination of CSLA with an MVC REST API and then an MVVM JavaScript implementation known as Knockout JS. Check it out as it is very nice and has great potential within HTML5 implementation. It still means an alternate implementation of the UI and client validation but works well with jquery validation and the MVVM design handles complex dependent objects and does data binding etc. As you suggest there are no silver bullets but seeing good potential with this. Currently I am looking at potential to code generate some HTML5 UI screens using Knockout / MVVM / CSLA / JSON / REST. I am hoping to codegen a good portion of basic screens and validation to see how close I can get to a generated HTML5 'CSLA-like' MVVM UI. Worth looking into. Shawn Wildermuth recently blogged on Knockout with ASP.NET MVC/jquery too which is a good intro.

artsd replied on Wednesday, December 14, 2011

Thanks. I will take a look at Knockout. It sounds like you are adding a REST API on top of your CSLA bus objects, correct? I am afraid that for me that will be a huge burden. My application has hundreds of CSLA business objects. The majority of the code is generated using a custom T4 generator I wrote whose input is some XML metadata. We use optional partial classes to implement non-standard bus obj functionality.

I am actually working with a Magenic consultant on building a proper consumable web service API (SOAP in my case) but this API is really designed to expose a small subset of my application's functionality for specific purposes; it is not intended to expose all functionality which is pretty much what I would need for a pure HTML based app.

gyob00 replied on Tuesday, February 14, 2012

asinc: wow I've been thinking the same thing and I just ran across your post. I'm going to start working on this. How is your work progressing and are you still working on this?

Copyright (c) Marimer LLC