Silverlight DataForm

Silverlight DataForm

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


Brad posted on Saturday, July 25, 2009

Has anyone successfully used a DataForm with CSLA objects?

Would you be able to share any tips to hook everything up?

decius replied on Thursday, August 20, 2009

I too am also interested in CSLA + DataForm.  It seems to me that a lot of the DataFrom functionality mimics a chunk of what CSLA solves (validation, etc).  I would love to hear some thoughts on using the 2 together. Is there anyway to combine the power of the DataForm validation attributes + Csla validation? Or is it more of "Choose one or the other". 

One of the key benefits to CSLA validation has been the server-side validation it provides.  Now that we have silverlight, we can also execute that on the client even easier.  However now we're given this DataForm control with all these goodies and it's hard to decide which to use! Out of the box, the DataForm looks great and is easy to use and includes things like validation with the CustomValidation Attribute, but I don't want to end up duplicating a lot of my validation logic if I don't have to.... So I'm torn. Is there a way to incorporate CSLA with this functionality?

RockfordLhotka replied on Sunday, August 23, 2009

I'm not sure yet.

The attribute scheme they've developed is a way of externalizing business rules into the UI, but at the request of the object. In other words, the rules are all running outside the object, but the rules run because of the attributes.

CSLA does the reverse. The rules are encapsulated in the object, and one of the drawbacks is that they aren't easily exposed to the UI (which would be useful for the web more than anywhere else).

I'm considering making CSLA pick up on the attributes and automatically attach appropriate rules to the properties inside the object based on the attributes. This would allow the use of the attributes, and still get normal CSLA behaviors.

I think this would allow you to bind the object to a dataform and get the dataform behaviors (but not the richer CSLA behaviors), or bind the object to controls with PropertyStatus to get the rich CSLA behaviors.

Either way, the rules would run on the server as they do today.

Basically this would support the dataform for people who want simplicity and don't need the more powerful CSLA features like

  1. info/warn severities
  2. async rules
  3. more than one broken rule per property

And yet people who do want the richer CSLA behaviors could use them like they do today, but could also use the standard attributes defined by Microsoft.

decius replied on Tuesday, August 25, 2009

I would definitely love to see this in a next release.  But perhaps the DataForm attribute topic is not as hot as I thought. I'm surprised not to see anyone else in the discussion.

Jack replied on Tuesday, August 25, 2009

I have been following but I'm so underwater with trying to get my application out I'm too busy to make any useful comments.

 

It is a fairly new feature though so I'm not sure too many people will have had time to investigate.

 

Something that might be worthwhile is looking at how Telerik has integrated it into their third party controls.  I think they have jumped through a few hoops to tie into the MS design with their controls.  Not sure if there is anything there to leverage or if it really is just implementing the basics from MS.

 

I think I would like to try and implement something where the basic visuals are using the MS format but it you narrowed down to a field specific or actually clicked somewhere to get more info then I would pull out the CSLA validation info... not sure though since most of my basic CRUD windows are only for admin work so I've only delivered the basics via SilverLight.

 

I'm just getting sick of writing the UI so any freebies I can get plug-in the better.

 

From: decius [mailto:cslanet@lhotka.net]
Sent: August-25-09 12:21 PM
To: jaddington@alexandergracie.com
Subject: Re: [CSLA .NET] Silverlight DataForm

 

I would definitely love to see this in a next release.  But perhaps the DataForm attribute topic is not as hot as I thought. I'm surprised not to see anyone else in the discussion.



RockfordLhotka replied on Tuesday, August 25, 2009

The question is exactly what would you like to have?

 

If answer is to dumb down CSLA to fit into the DataForm model is that what you want? I rather suspect you can have that today – just put the attributes on your properties and don’t connect any business rules in AddBusinessRules() and I bet it works great.

 

I suspect, however, that you at least want the rules to run on the server – in which case just put AddBusinessRules() into an #if !SILVERLIGHT block and you get that too.

 

But then I suspect it would be preferable to have the server-side code automatically pick up on the attributes and do the AddRule() calls for you based on those attributes – and this is where I’m leaning at the moment as far as CSLA doing anything in this area. And I may do this in 3.8 if I get time. I think the challenge will be determining when to actually add CSLA rules, and when to leave the object as a dumb data container, because otherwise the rules would get run once by the UI code and once by the business object and that’d not be terribly useful.

 

Rocky

 

 

From: decius [mailto:cslanet@lhotka.net]
Sent: Tuesday, August 25, 2009 1:21 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Silverlight DataForm

 

I would definitely love to see this in a next release.  But perhaps the DataForm attribute topic is not as hot as I thought. I'm surprised not to see anyone else in the discussion.



louis replied on Tuesday, September 08, 2009

I am currently learning Silverlight. I bought the video series for Csla for Silverlight and am quite impressed with what Csla for Silverlight has to offer. (Excellent video series Rhocky).

I too, am torn between csla and new DataForm Control.

Coming from a web background, I am familar with the Csla framework and how "UI" and "DataAccess." We use the same business objects for several different applications (Web, WPF, and WinForms).

 It is nice to see a control like the DataForm and DataGrid in the Silverlight 3.0 toolkit. It makes development easier transition from Web to Silverlight.

We would like to be able to reuse some of the objects with the new DataForm control. The new attibute system seems to be only a "Silverlight" implementation that is not currently in RTM mode. 

Too bad there is nothing like "Parital" Properties where you can add the attributes on the "Silverlight" Business.Client side and leave them out on the Business.Server side.

zhengokusa replied on Wednesday, September 09, 2009

CSLA object can work very well with DataForm. Actually, I just moved from Silverlight2 to Silverlight3 during the transition I also implemented DataForm into my application.

Here is some code snapshot that I did, hope it will help you.

<dataFormToolkit:DataForm
            Background="{StaticResource GlossyBlue_RectangleGlossGradient}"
            CancelButtonStyle="{StaticResource BlackGlossyButton}"
            CommitButtonStyle="{StaticResource BlackGlossyButton}"
            CommitButtonContent="Save"
            Margin="5"
            Grid.Column="0"
            CurrentItem="{Binding Data}"
            AutoEdit="False"
            AutoCommit="False"
            AutoGenerateFields="False"
            EditEnded="DataForm_EditEnded">
            <dataFormToolkit:DataForm.EditTemplate>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="1">
                        <Grid Margin="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="300"/>
                                <ColumnDefinition Width="250"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Orientation="Vertical"
                                        dataFormToolkit:DataField.IsFieldGroup="True"
                                        Grid.Column="0">
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding LicensorName, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Status, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Comment1, Mode=TwoWay}" />      
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Comment2, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                            </StackPanel>
                            <StackPanel Orientation="Vertical"
                                        dataFormToolkit:DataField.IsFieldGroup="True"
                                        Grid.Column="1">
                                <dataFormToolkit:DataField Margin="0,1" >
                                    <TextBox Text="{Binding Phone1, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Phone2, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding PhoneMob, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding PhoneFax, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                            </StackPanel>
                            <StackPanel Orientation="Vertical"
                                        dataFormToolkit:DataField.IsFieldGroup="True"
                                        Grid.Column="2" HorizontalAlignment="Stretch">
                                <dataFormToolkit:DataField Margin="0,1" >
                                    <TextBox Text="{Binding Email, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Website, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Addr1, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <dataFormToolkit:DataField Margin="0,1">
                                    <TextBox Text="{Binding Addr2, Mode=TwoWay}"/>
                                </dataFormToolkit:DataField>
                                <StackPanel Orientation="Horizontal"
                                            Margin="-3,1,0,0">
                                        <dataFormToolkit:DataField >
                                            <TextBox Text="{Binding City, Mode=TwoWay}"/>
                                        </dataFormToolkit:DataField>
                                        <dataFormToolkit:DataField>
                                            <TextBox Text="{Binding State, Mode=TwoWay}" />
                                        </dataFormToolkit:DataField>
                                        <dataFormToolkit:DataField>
                                            <TextBox Text="{Binding Zip, Mode=TwoWay}" />
                                        </dataFormToolkit:DataField>
                                </StackPanel>
                            </StackPanel>
                        </Grid>
                    </Border>
                </DataTemplate>
            </dataFormToolkit:DataForm.EditTemplate>
        </dataFormToolkit:DataForm>

Code behind for handling Dataform event

 private void DataForm_EditEnded(object sender, DataFormEditEndedEventArgs e)
        {
            //this is the only way to know which button the user clicked
            if (e.EditAction == DataFormEditAction.Commit)
            {//Commit button
                if (LicensorSaveBtnClicked != null)
                {

                  //Call your dataprovider to save the change you have done on the CSLA editable BO   
                }
            }
            else
            { //Cancel button
                //Cancel event handler need to be added.!!!!
                if (LicensorCancelBtnClicked != null)
                {

                     //Call you dataprovider to cancel the changed 
                }
            }

        }

 

RockfordLhotka replied on Friday, October 09, 2009

One thing to note is that CSLA .NET 3.8 will include support for data annotation attributes

 

http://www.lhotka.net/weblog/LeveragingDataAnnotationAttributesInCSLANET.aspx

 

That should make the use of the DataForm somewhat simpler – though of course you still only get the validation behaviors of the DataForm, which aren’t as powerful as what you get with PropertyStatus.

 

But if you are happy with the limited UI behaviors of DataForm you’ll be able to use those, while still knowing that the business object itself is enforcing all the rules regardless of whether the DataForm is used at the UI level or not.

zhengokusa replied on Friday, October 09, 2009

Thanks Rock

That's huge.

zhengokusa replied on Wednesday, September 09, 2009

as for validation

This is what I did on the BO

in your editable BO you can put on the top

#if SILVERLIGHT
using System.ComponentModel.DataAnnotations;
#endif 

and then you can use whatever DataAnnotation provides to validate your BO property on the client side. 

for example :

#if SILVERLIGHT
        [Display(Name = "Licensor Name")]

       [Required(ErrorMessage="Phone 1 is required field!")]
#endif      
        public string LicensorName
        {
            get
            {
                return GetProperty(LicensorNameProperty);
            }
            set
            {
                SetProperty(LicensorNameProperty, value);
            }
        }

 

in this way we can make CSLA BO to encapsulate rules in the object, so it still has a clean separation between the UI and BO.

Hope it will help

 

 

 

 

 

RockfordLhotka replied on Thursday, September 10, 2009

If you have rules in the object that can't or aren't expressed in attributes, how do you show them to the user?

pondosinat replied on Thursday, September 10, 2009

One approach would be a hybrid of using PropertyStatus controls for the more complex rules that can't conform to attributes, such as async rules. Then you could get the best of both worlds: the very nice, built-in features of the dataform and the robustness of CSLA validation.

Because, from what I've seen, most validation rules are the simple per-field kind, and those rules are pervasive in most data entry forms.

zhengokusa replied on Thursday, September 10, 2009

I have not done any server side validation using Attributes, but that's what I think. Just a idea 

in the attribute, we have CustomValidation. We can create our own client side only class and static function to validate a property. In that static function can we do a CSLA Dataportal call to a CommandObject which does the validation and upon its completion, it return true or false back to client side of code?

I don't know. My application have not yet see this kind of situation. The Attributes so far satisfied all my needs for clients side data pre-screening.

 

 

 

 

Copyright (c) Marimer LLC