CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Collections

Answered (Not Verified) This post has 0 verified answers | 7 Replies | 3 Followers

Not Ranked
14 Posts
San posted on Thu, Sep 15 2011 9:54 AM

Hi, All

I am new to CSLA. Actually I can access child collections in parent page but how can i access child collection in child validation class.

E..g., I have a requirement based on previous data entered i have to run some validations.

 

Thanks,

San

All Replies

Top 25 Contributor
238 Posts
Curelom replied on Thu, Sep 15 2011 10:10 AM

I'm not quite sure what you are asking.  Could you provide some more details?

Not Ranked
14 Posts
San replied on Thu, Sep 15 2011 10:20 AM

Actually I have a requirement where it should trigger some validation rules.

for e.g., user can enter multiple records for a month.

Let say user entered 3 records for jan month, each record has a unique id.

On 1st record user entered age as 67 and on 2nd record there is a drop-down list which user picked 1st record unique id and entered age as 34. so I should throw and error message saying age is not equal.

Thanks!

San

Top 25 Contributor
238 Posts
Answered (Not Verified) Curelom replied on Thu, Sep 15 2011 12:50 PM
Suggested by Curelom

I have done something similar to this. I had a scenario where the start date and end date values couldn't intersect. Don't know if my solution is the prefered way to do it. Structure of my objects is Transportation - TransportationRateList - TransportationRate. I made a object Business Rule like below. Note that it isn't a property business rule, but a rule to be used on the TransportationRate object. In the transportation Rate object I add the rule

public class DateSpansDoNotIntersectRule : BusinessRule {
        protected override void Execute(RuleContext context) {
            var target = (TransportationRate)context.Target;
            TransportationRateList targetList = target.Parent as TransportationRateList;
            DateSpan ds = new DateSpan(target.StartDate, target.EndDate);
 
            if (targetList != null) {
                //get list of datespans that we need to check
                var q = (from t in targetList
                         where t != target
                               && t.FreightMethod == target.FreightMethod
                               && t.Pallet == target.Pallet
                               && ds.Intersect(new DateSpan(t.StartDate, t.EndDate)) != null
                         select t).FirstOrDefault();
 
                if (q != null)
                    context.AddErrorResult("Effective Date spans cannot intersect.");
            }
        } 
    }

BusinessRules.AddRule(new DateSpansDoNotIntersectRule());
One of the problems with the PropertyStatus control is that it doesn't work for object Business Rules, only rules on properties.  
For me to get this to work I had to expose the accessiblity of Broken Rules Collection
        public IEnumerable<BrokenRule> BusinessObjectErrors {
            get {
                if (this.BrokenRulesCollection.Where(b => String.IsNullOrEmpty(b.Property)).Count() > 0) {
                    return this.BrokenRulesCollection.Where(b => String.IsNullOrEmpty(b.Property));
                }
                return null;
            }
        }
then I bound a column to the BusinessObjectErrors and set the data template for the column
   <DataTemplate>
        <ItemsControl ItemsSource="{Binding}" Margin="4" Padding="4">
            <ItemsControl.Template>
                <ControlTemplate TargetType="{x:Type ItemsControl}" >
                    <ItemsPresenter/>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="{StaticResource BorderErrorBrush}" BorderThickness="1"
                            CornerRadius="10"
                            Background="{StaticResource BackgroundErrorBrush}">
                        <StackPanel Orientation="Horizontal" Background="Transparent">
                            <Image Source="{StaticResource Horizon_Image_Error}" ToolTip="{Binding Path=Description}" MaxHeight="12" MaxWidth="12"/>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True" Orientation="Vertical" Margin="4" />
                    <!--Background="Yellow"-->
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
 
You will also have to take into account property status changes.
Not Ranked
14 Posts
San replied on Thu, Sep 15 2011 8:25 PM

I really appreciate your reponse, but i would like to get previous data through collections.

Top 25 Contributor
238 Posts
Curelom replied on Thu, Sep 15 2011 10:01 PM

You can't have business rules on the collections themselves.

Not Ranked
14 Posts
San replied on Mon, Sep 19 2011 11:04 AM

Is there any other way to implement the functionality.

 

-San

Top 25 Contributor
238 Posts
Curelom replied on Mon, Sep 19 2011 12:01 PM

I haven't found another way except for what I detailed earlier in these posts.

Page 1 of 1 (8 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems