Android Addicted Apps Creator

September 17, 2009

WPF N-Tier using Entity Framework

Filed under: WPF — alin b. @ 21:35
Tags: , ,

Noia development has begin with baby steps. Right now I am trying to make a application core that will support further changes very easy. Today we are going to learn how to make layer for application and how to make them communicate. As you’ve learned so far about me I won’t give many details and explanations, the main purpose is to get the result needed as fast as possible. This being said, let’s begin our tutorial.

Let’s start by creating a new solution called Noia. In this solution we will have 4 projects as follow:

  • NoiaDAL – represents the Data Access Layer. Project type Class Library
  • NoiaBLL – represents the Business Logic Layer. Project type Class Library
  • NoiaUI – will take care of User Interface layer. Project type WPF Application
  • NoiaEF – which will contain Entities. Project type Class Library

Also, we will make a new folder NoiaDB in which we will copy the NoiaDB.sdf (SQL Server CE database).

In order to make this layers communicate, we must create the references between them:

  • In NoiaUI create reference to NoiaEF
  • In NoiaBLL create references to NoiaDAL and NoiaEF
  • In NoiaUI create references to NoiaBLL and NoiaEF

In the NoiaED we will add a new item of type Ado.Net Entity Data Model named NoiaEntityModel.edmx. When ask the Model Content we will choose Generate from database and we’ll create a new connection string pointing to the NoiaDB.sdf file. The generated connection string will look like:

metadata=res://*/NoiaEntityModel.csdl|res://*/NoiaEntityModel.ssdl|res://*/NoiaEntityModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=C:\Noia\NoiaDB\NoiaDB.sdf"

After the model is generated we can see all the entities created. We will rename all the be singular. I don’t want to have a object Users, I want to have a object User. Also, in properties for each entity we will rename the Entity Set Name from UserSet to Users and so on, for all the entities that require it. At the end, it should look like this:


What I want to achieve in this tutorial is to be able to create a login screen and check if the user name provided by UI is valid or not. The information will travel between all layers forth and back. Please remember that each layer will have it’s unique characteristics and none of the layers will overlap.

NoiaDAL (Data Access Layer)

In DAL we will need to have a class for each entity we want to use. For our particular example we will add a new class and name it UserDO (from User Data Object). In this class we must have a method to search for a user based on the user named taped by the front user. In searching for the user we will use a Linq statement. Complete class code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NoiaEF;

namespace NoiaDAL
    public class UserDO
        public User GetUser(string UserName)
            NoiaDBEntities ne = new NoiaDBEntities();

            var usr = from u in ne.Users
                  where u.UserID == UserName
                  select u;

            return usr.FirstOrDefault();

In order to be able to use the Entities we must add a reference at the project to System.Data.Entities. This pretty much makes the job done in the DAL.

NoiaBLL (Business Logic Layer)

As in DAL, in here we will have another class for User, in this case it will be named UserBO (User business object). Because the UI layer must not access DAL directly, the BLL will be an intermediary, and after validations and checks, the BLL will decide if will ask the DAL for information or provide direct feedback to the UI if validations failed. Because validation is the scope of the next tutorial, the UserBo will only redirect the request to DAL. Again, we must add a reference at the project to System.Data.Entities.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NoiaDAL;
using NoiaEF;

namespace NoiaBLL
    public class UserBO
        public User GetUser(string UserName)
                UserDO usr = new UserDO();
                return usr.GetUser(UserName);

NoiaUI (User Interface Layer)

We will create the login window. It must contain two textboxes for user input (one for User name one for Password) and a Login button. Add a new Windows (WPF) in NoiaUI project and name it Login

<Window x:Class="Noia.Login"
    Title="Login" Width="370" Height="120" Background="#185d7c" WindowStartupLocation="CenterScreen">
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>

            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        <Label Name="lblUserName" Grid.Row="1" Grid.Column="0" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5" >User name</Label>
        <Label Name="lblPassword" Grid.Row="2" Grid.Column="0" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5">Password</Label>
        <TextBox Name="txtUserName" Grid.Row="1" Grid.Column="1" MinWidth="200" Margin="5"></TextBox>
        <PasswordBox Name="txtPassword" Grid.Row="2" Grid.Column="1" Margin="5"></PasswordBox>
        <Button Name="btnLogin"  Click="btnLogin_Click" Grid.Row="2" Grid.Column="2" MaxWidth="50" MaxHeight="25">Login</Button>

The login button click event must communicate with the BLL and ask for data.

  private void btnLogin_Click(object sender, RoutedEventArgs e)
            UserBO usrbo = new UserBO();

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

            if (usr == null)
               MessageBox.Show("Wrong username");
                MessageBox.Show("Wellcome " + txtUserName.Text);

There are a few more things that need to be done: add a reference at the project to System.Data.Entities. Copy the App.Config file from NoiaEF to NoiaUI because we need the connection string. Set in App.xaml the startup window to login

<Application x:Class="NoiaUI.App"


Run it and see the result… it should work like a charm


Next time we shall see how to make a business rule in BLL and how to send error list from BLL to UI. It will be interesting.

P.S. Very important note: it seems that there is a problem for Linq to Entity access the SQL CE Database file with the existing SP1. In order to make things work, you must uninstall SQL CE SP1 and install this hot fix:

And the videos… as you can hear I was pretty tired when making them, so a lot of ufffs and offfs 🙂


  1. Hey, I found your blog while searching on Google your post looks very interesting for me. I will add a backlink and bookmark your site. Keep up the good work!

    Comment by Tnelson — October 1, 2009 @ 02:54 | Reply

  2. Hey , this is great … I liked your post..its quite helpful. Only thing is tht you have used SQL DB, and I am using MySQL. WOuld look for more information as to how to use entities for MySQL DB.

    Aseem Chiplonkar

    Comment by Aseem Chiplonkar — May 18, 2010 @ 06:07 | Reply

    • I’m glad that you find it useful. I hope that I will have some free time in the future (after baby is growing up a little) and start doing some tutorial about Domain Driven Design, WPF and MVVM.

      Comment by alinberce — May 18, 2010 @ 07:31 | Reply

  3. Hello, good job, it was very fruitful for me to understand n-tiers architecture decoupling using entity framework but it will be more intersting to attach this article with source code.

    Comment by dhaker gouissem — August 10, 2010 @ 09:32 | Reply

  4. Hi, Great tutorial, simple and well explained. I’d like to see such a well explained example of MVVM with two way database binding, I find many examples overly complicated.

    Comment by Dicky — January 12, 2011 @ 00:27 | Reply

  5. Very interresting lecture!

    I have a question, if you retrieve a list full of properties from the data layer and you want to make a filtration to retrieve right property data from the list. Should this process take place in the data or business layer in relation to N-tier?

    Comment by Johan — May 29, 2011 @ 23:17 | Reply

  6. Hi, Its excellent. Simple and well explained… expecting more tutorials from you.

    Comment by Manjith — December 23, 2011 @ 09:46 | 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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: