Click here to Skip to main content
Click here to Skip to main content

Simple "Contact Book" web application using VisualJS.NET, Fluent NHibernate and ASP.NET Web Forms on MS SQL Server 2012

, 5 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Simple "Contact Book" web application using VisualJS.NET, Fluent NHibernate and ASP.NET Web Forms on MS SQL Server 2012

Editorial Note

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

RAD technique is one of many methodologies of developing software. It has its pros and cons, but is useful in some scenarios.

Let's take a look at the process of creating web applications using RAD methodology offered by the VisualJS.NET library, using the most popular .NET ORM - NHibernate on the ASP.NET framework with MS SQL Server as data backend.

Background

Basic C# and ASP.NET Web forms skills are required

Using the Code

We will keep the code simple, without any additional layers of abstraction not to obscure the general view.

Let's start with creating a database. We are using a single table, although in a real life scenario the schema would be more advanced. Using SQL Server Management Studio we create the following table:

We create ASP.NET web application project using Visual Studio 2010.

That was easy and fast. Now let's put Fluent NHibernate into our project. Using NuGet package directly from Visual Studio takes us no more than a minute. Fluent NHibernate is a library making core NHibernate much more... fluent (http://en.wikipedia.org/wiki/Fluent_interface). It uses fluent coding methodology to work with configuration, mappings, schemas and many more aspects involved in using NHibernate. Additionaly it forgets about xml mapping files and move that functionality right into a code. Fluently of course.

Let's take a look at some code:

Our single entity class (to have a clean representation of our database table "Contact")

    public class Contact
    {
        public virtual int ID { get; protected internal set; }
        public virtual string Firstname { get; set; }
        public virtual string Surname { get; set; }
        public virtual string PhoneNumber { get; set; }
        public virtual string Gender { get; set; }
        public virtual string Address { get; set; }
    }

backed up with mapping class to let know NHibernate what are the roles of properties in our entity class. This mapping can be made in automated way as well.

    public class ContactMap : ClassMap<Contact>
    {
        public ContactMap()
        {
            Id(x => x.ID);
            Map(x => x.Firstname);
            Map(x => x.Surname);
            Map(x => x.PhoneNumber);
            Map(x => x.Gender);
            Map(x => x.Address);
        }
    }

Session object that is our base object for interaction with NHibernate and database. It also incorporates configuration of our database. Here in simple form:

 public class SessionFactoryManager
    {
        public static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
            .ConnectionString(c => c
            .FromConnectionStringWithKey("connectionString")))
            .Mappings(m => m
            .FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))            
            .BuildSessionFactory();
        }       
    }

Again using nuget package makes downloading needed library very fast and simple. After installation we need to add VisualJS.NET support to our project. With single right button click on our project name and choosing "Add VisualJS.NET support" we add references and sample pages to our project.

Now let's create some UI.

We vreate visualjs form by right clicking on our project and selecting new appeared option "Add form". Let's name it "ContactBook". First we want to embed our future contact book.

VisualJS.NET control in existing aspx page. Let's put it on already created by ASP.NET project template default.aspx page. To do this we add to default.aspx VisualJSControl right from the Toolbox. Then we fill FormName and HandlerUrl properties of VisualJSControl with appropriate values:

Now our default.aspx page will display our Contact Book. Let's create it. We will use one view - ContactBook for general links and listing of contacts and second reusable view - AddEditContact for creating and updating our contacts:

On the ContactBook view we put VisualJS.NET ListView control for listing our contacts. We add appropriate colums programmatically or in designer and we fetch data from database:

using (var session = sessionFactory.OpenSession())
{
      var contacts = session.CreateCriteria(typeof(Contact)).List<Contact>();
 
      lstContacts.Items.Clear();
 
      foreach (var c in contacts)
      {
         ListView.ListViewItem lvi = new ListView.ListViewItem() { Tag = c.ID };
         lvi.SubItems.Add(new ListView.ListViewSubItem(c.Firstname) { Name = "Firstname" });
         lvi.SubItems.Add(new ListView.ListViewSubItem(c.Surname) { Name = "Surname" });
         lvi.SubItems.Add(new ListView.ListViewSubItem(c.Gender) { Name = "Gender" });
         lvi.SubItems.Add(new ListView.ListViewSubItem(c.PhoneNumber) { Name = "PhoneNumber"});
         lvi.SubItems.Add(new ListView.ListViewSubItem(c.Address) { Name = "Address" });
 
         lstContacts.Items.Add(lvi);
       }                
}

We add three links for adding, editing and deleting contact. For all of those actions we use easy to do MessageBox and Dialog. The rest of CRUD actions agains database are similar to above code and you can find it in the source. You should use transactions in real life application. Here is the sample of add contact link handler showing windows forms style of displaying the dialog window:

private void lnkAddContact_LinkClicked(object sender,  System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
        {
            var f = new AddEditContact();
            f.FormClosed += new System.Windows.Forms.FormClosedEventHandler(f_FormClosed);
            f.ShowDialog();
        }

Now aforementioned AddEditContact form shaped to show as a window. We use controls from VisualJS.NET toolbox including JButtons. We reuse that form for both adding a new and editing an existing contact. We use the built in VisualJS.NET validation and error displaying for textboxes which further makes our project a real RAD driven.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Michał Maniewski
Software Developer
Poland Poland
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberOshtri Deka2-Oct-12 10:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 5 Oct 2012
Article Copyright 2012 by Michał Maniewski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid