Using a BO-Layer in a Windows Forms project

Topic updated 17-1-2007

This topic describes how to use a BO-Layer generated with the GenWise NHibernate Templates inside a Visual Studio 2005 Windows Forms project.

Once you have successfully built and compiled the project you have a BO-Layer DLL and a VS.NET 2005 project file. To create a Windows Forms application using the BO-Layer follow these steps :

1.Open VS.NET 2005.
2.Create a new windows application
3.Attach an existing Project to the solution.
4.Search the project on the disk
5.Add a new reference to your windows project.

 

Graphically the steps are as following:

Step 1 and 2.:

ExamNHWinformCP

 

Step 3: Add an existing project to the solution. (The project resides in your BO directory)

ExamNHWinformAEP

 

and add the  .cs project from your BO-Layer directory:

ExamNHWinformBOA

 

Now lets build a quick form with a region data grid. Build a form with a datagridview control on it: Add 'using Northwind;' to the source code in the namespaces, if northwind is your BO-Layer name.

Add the following code to Load from method:

       RegionFactory _rFact = new RegionFactory();

       RegionCollection _regions = _rFact.GetAll();

       this.dataGridView1.DataSource = _regions;

 

That's it. Run the form, it should look like this:

ExamNHWinformRB

 

The complete code for the form (minus the designer code looks as following):

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using Northwind;

 

namespace Test_Small_BO_Layer

{

       public partial class BrowseRegions : Form

       {

               public BrowseRegions()

               {

                       InitializeComponent();

               }

 

               private void BrowseRegions_Load(object sender, EventArgs e)

               {

                       RegionFactory _rFact = new RegionFactory();

                       RegionCollection _regions = _rFact.GetAll();

                       this.dataGridView1.DataSource = _regions;

               }

       }

}