Android Addicted Apps Creator

September 18, 2009

How to make a business rule in BLL and use it wisely

Filed under: WPF — alinberce @ 21:08
Tags: , , , , ,

Based on previous tutorial we will try to make a business rule for user name. The simplest rule possible is that the Username must not be empty. We must be able to check for this in the BLL and, in case of error, display a message in UI. Sounds simple ? Now let’s make it.

In this case it’s a simple rule, but just imagine that for our method GetUser(…) we can have many validations (Username must be at least 5 characters, Password can’t be blank… etc) so, after checks are being done a list of errors could appear, this means we must have possibility to store all errors and make them available to the UI. For achieving this purpose we’ll create a new Class called BusinessRule in which we will store some information regarding the failed checks:

public class BusinessRule
        private String _RuleType;
        public String RuleType
            get { return _RuleType; }
            set { _RuleType = value;}

        private String _Message;
        public String Message
            get { return _Message; }
            set { _Message = value; }

        private String _Property;
        public String Property
            get { return _Property; }
            set { _Property = value; }

        public BusinessRule()

        public BusinessRule(String _RuleType, String _Message, String _Property)
            RuleType = _RuleType;
            Message = _Message;
            Property = _Property;

We could have different type of checks which will return different type of messages: Warnings, Errors or Info. This will be stored in RuleType. The message is pretty self explanatory, the message displayed on rule fail. The property will store the property name that failed the checks (Username in our case) and will help the UI to focus the problematic controls.

Now let’s get back to the userBo class and make some changes to use the BusinessRule class.

public List<BusinessRule> BrokenRules = new List<BusinessRule>();  

        public User GetUser(string UserName)

            if (string.IsNullOrEmpty(UserName))
                BrokenRules.Add(new BusinessRule("Error","User name cannot be empty","UserName"));
                BrokenRules.Add(new BusinessRule("Warning", "You have 3 more retries", "UserName"));
                BrokenRules.Add(new BusinessRule("Info", "User name has a minimum length of 5 characters", "UserName"));

            if (BrokenRules.Count.Equals(0))
               UserDO usrDO = new UserDO();
               return  usrDO.GetUser(UserName);
                return null;

If check fails, all the errors will be added to the BrokenRules list. This list is Public, so it can be used by the UI.

Making the UI use the BrokenRules list

Imagine that a list of BrokenRules will be available for all classes in UserBO, so we’ll need to make a window and provide a datasource to it programatically. The layout of the window will be as follow:


<Window x:Class="NoiaUI.Message"
    Title="Message" Height="300" Width="300" WindowStartupLocation="CenterScreen">
        <CollectionViewSource x:Key="MessagesSource"/>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        <TextBlock Grid.Row="0" Text="Atentie" FontSize="14" Margin="5"/>
        <ListBox Name="lstMessages"  ItemsSource="{Binding Source={StaticResource MessagesSource}}" Grid.Row="1">
                    <StackPanel Orientation="Horizontal">
                        <Image Margin="5"  Source="{Binding Path=Image}" Stretch="Fill" Width="16" Height="16"/>
                        <TextBlock Margin="5" Text="{Binding Path=Message}"/>
        <Button Name="btnContinue" Grid.Row="2" MaxWidth="100" MaxHeight="25" Click="btnContinue_Click" Margin="4">Continue</Button>


We declare a CollectionViewSource as a Window Resource that will be the ItemsSource of the listbox. Here you can see something interesting, in the ItemTemplates I added a StackPanel in which we have 2 controls, a Image and a Textbloc. To make the window better looking, will add some icons for RuleTypes, it’s much nicer to see a Error icon instead of the text “Error”.

In the cs file we take care of everything

public partial class Message : Window
        public Message()

        private class MessageFormat
            public BitmapImage Image { get; set; }
            public string Message { get; set; }
            public string Property { get; set; }

        public void SetMessagesSource(List<BusinessRule> lstMessages)
            List<MessageFormat> finalMessage = new List<MessageFormat>();
            DirectoryInfo ImageDir = new DirectoryInfo(@"..\..\Images");

            foreach (BusinessRule br in lstMessages)
                FileInfo fi = new FileInfo(@"..\..\Images\"+br.RuleType+".png");
                Uri uri = new Uri(fi.FullName);
                finalMessage.Add(new MessageFormat { Image = new BitmapImage(uri), Message=br.Message, Property=br.Property });

            CollectionViewSource MessagesSource = (CollectionViewSource)this.Resources["MessagesSource"];
            MessagesSource.Source = finalMessage;


        private void btnContinue_Click(object sender, RoutedEventArgs e)

A class, MessageFormat is used to store the ruleType as BitmapImage. A public methon, SetMessageSource, is used to read the image files, add the entire content to the finalMessage list and set the CollectionViewSource source. In this way, we instantiate the windows, call the SetMessageSource method and then display it on the screen.

Let’s see how to use it in real life enviroment. In the Login window, btnLogin click event will look like this:

private void btnLogin_Click(object sender, RoutedEventArgs e)

            UserBO usrbo = new UserBO();
            User usr = usrbo.GetUser(txtUserName.Text);

            if (usr == null)
                if (usrbo.BrokenRules.Count.Equals(0))
                    MessageBox.Show("Wrong username");
                    Message msg = new Message();
                MessageBox.Show("Wellcome " + txtUserName.Text);

Run it, don’t type anything, just press the login button. The window should look like this:


The app must not allow to continue until the BrokenRules list is empty. Furthermore, based on property field in list, we could set focus to controls that failed checks. I hope you enjoyed this tutorial. Until next time, Happy coding.

And of course  the video, this time in HD 😉


1 Comment »

  1. Did you look at LHOTKA CSLA?

    Comment by Ignat Andrei — September 22, 2009 @ 12:26 | Reply

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: