Android Addicted Apps Creator

September 18, 2009

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

Filed under: WPF — alin b. @ 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 😉

September 11, 2009

NOIA – Planning and analyze. The beginning…

Filed under: WPF — alin b. @ 20:03
Tags: , ,

    What does NOIA stand for ? NOIA is “Not Another Invoice Application”. Pretty explanative what it should do: a application for invoices and stocks which will deal with input and output, in a small level CRM, goods stock and payments.  The reason why I mention this is because while developing it, I will make a chain of tutorials describing each important step in development. As I will learn I will share knowledge.

Technical specifications

     Because it’s not either big size or complex the platform used for development will be Visual C# 2008 Express edition. The data sources will be two, SQL Server 2008 Express Edition and SQL Server Compact Edition 3.5. Those two database systems are suitable for most cases, for small to medium clients.

Thank you Microsoft for providing a free tools to developers.


The architecture will be layered designed, so long live the n-tier. Main goals to achieve will be:

  • Changing one layer of the system should have a minimal impact over the rest of the layers. This will help maintenance, bug fixing and unit testing. It also makes the architecture more flexible
  • Unrelated components should be loosely coupled
  • Separation between User interface, Business objects and Database


Each layer will be developed as follow:

– Presentation Layer: Windows Presentation Foundation (WPF) and C#

– Business Objects Layer: C#

– Data Access Layer: C# and Linq   The database will contain only a few tables needed for fresh start (like Users, Companies etc.). The rest of the tables will be added as the development progresses.

The interface

     I am planning to make a extremely user friendly interface. I give a big deal about user pleasing, this makes the app sale. Just imagine that by my design flaw, a user must lose one second each time he saves an invoice (a freaking’ message box maybe), for a number of 3000 invoices/month there will be a lose of time around 1 hour. As I am a big fan of n-tier architecture, also, I am a big fan of outlook interface. This means the interface will have a very outlookish style. Plus some other enhancements as they come along.


         As a friend used to say, I’ve made a map to the user so it won’t get lost. This isn’t really true, but the workflow could help and it would be easier to go to a certain operation if remembered visually. The New/Save/Delete won’t be as in the image, there will be a combo box so that the user could select company or date or stuff like this. The upper image is a WIP (Work in Progress) so it can be changed. It may seem very colored now, at the end will have a more professional look.

This was a pretty boring post, even if I tried to make it as short as possible. If you have any suggestions please send them.

Until next time we “meet”, Happy coding.

P.S. – I would like to take a moment and THANK Cristi for help, support and ideas. So, thank you man.