Main Screen consideration

As purpose of my application: I want to make an application to input word definition, work like input words into dictionary. And also want to see
word definition picked up randomly in some criteria: Like category, dictionary.
So the question is what the main screen should look like?

Obviously, the most common actions will be:

  1. Adding words
  2. Displaying random words
  3. Might want to search for some words
  4. Then come to category, dictionary management

Still thinking ….
After a while, i decided to have these things in the main view, i will use View instead of Screen.
As every software has, it has Logo and menu.

  1. Place to add new word directly.
  2. Place to display random words.
  3. A set of commands to be called from main view, like: create new word, edit word, create new dictionary, create new category,
  4. Ability to search word by keyword, and call up an advanced search

Based on those stuffs, i can come up with building MainModelView class. Quite simple, just contains all information needed to display in View. Remember that, we are in MVVM model, so the ModelView only contains data NOT any specific GUI implementation.

(I will put code here when i finish )

From the specification above, i come up with 2 user controls:

  1. User control for adding new word
  2. User control for displaying random words

One thing lefts, how to position them in main view?

Re-done with NHibernate

As written in previous post, i do re-read the article from Ayende. Yes, to make sure, i fully understand what he is talking about.
OK, i come up with these in my application:

  1. Session’s lifetime is inside a ModelView life: Created automatically when it is first called. And disposed when the ModelView is disposed. These will be done in ModelViewBase class
  2. Keep the idea of DatabindingFactory and Interceptor. However, there is no need for PublisherEvent here. Since i do not need it now.

And of course, borrow all code to initialize NHibernate :p

Next will be MVVM in detail and GUI of the application. From each GUI, i can define scenario for it clearly. Have a better chance of right implementation

First fail with WL

As i mentioned in my plan: Building a desktop application, quite simple for the first version. I also mentioned about technologies usage: NHibernate + WPF with MVVM model.

I also did some researches on that model, also have done some projects with NHibernate ( web application).  Therefore, i am quite confident to start up. And i did start up. Put all framework code and prototype of MVVM in place, in fact i copied and pasted from others. You can find them in my should read page.

Well, things were not that easy. I ran the project, it was fine for the first look. However, it did not do any db action. In fact, i did not know why NHibernate did not contact with DB when i made some query. Even wrap them up in a transaction. I have been working on finding out why. BUT the most important thing i found out is that: I DO NOT UNDERSTAND THEM CLEARLY.

So what they actually are???

  1. I did not define my application clearly. I mean the workflow of each action. For instance, what happen when i create a new word. Since it will help me define the scope of unit of work
  2. I did not understand the article from Ayende clearly. Not read it carefully enough.
  3. Wow reading MVVM one time does not mean i understand it completely. I know it but NOT GOT it.
  4. Even though, in MVVM, the view is separated from others. However, designing a good looking view will inspires you a lot. And i am terrible at making GUI

And, what i am going to do now are:

  1. Design the GUI. It is as a part of learning XAML.
  2. Re-read 2 articles from should read.
  3. Start defining the lifetime of session. Define workflow for each scenario.

After all these fails, am i sad? NO. I am happy to find them out.

AjaxPro – first try

As i mentioned in the previous post, i now start with my code base for stories of version 1.0. In general, it contains 3 layers as other software. Data access, Business, and Presentation. Let see what i have in my mind:

  1. Data access: wow i can do all the stuffs with NHibernate. So just ignore it for now. I have tried NHibernate before, so it is not so difficult to get started.
  2. Business layer: As you can see all user stories in this version. Simple, right? Yes, it is. So just build the objects i need first, however, no need to say them here.
  3. Presentation: I want to keep it as simple as possible. However, it should be a rich web page which means that there is no real postback. Yeah THIS IS THING I TALK IN THIS POST.

AJAX! sure i should use AJAX to communication between client and server. The thing is which one? There are some options:

  1. ADO with data service: implement as webserver when you want to ask something from server.
  2. AjaxPro: Call method in the current page without posting back. I vote for this.

Basically, this technique was not new to me since in the company i have been working with a technique called ServerMethods, developed by our talent developers, except me. It has been there sine i went in. However, AjaxPro is much better which many types supported and it is constantly developed by the community, go in ajaxpro for more detail.

All you need to do to get it works is:

  • Add ajaxpro.dll into web reference.
  • Register type of class you are using ajaxpro.
  • And then call it from the client site.

Here what i did, simple:

Server side:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using AnhDuc.BookStore.BusinessLayer;

namespace AnhDuc.BookStore
{
   public partial class Default : Page
   {
      protected void Page_Load(object sender, EventArgs e)
      {
         AjaxPro.Utility.RegisterTypeForAjax(typeof (Default));
      }

      [AjaxMethod]
      public Book GetResult(string searchKeyWord)
      {
         return new Book() {Name = searchKeyWord};
      }
   }
}

And from client side:

   function startSearch() {
          try {
             var _book = AnhDuc.BookStore.Default.GetResult($("#_txtKeyname").val()).value;
             alert(_book.Name);
          }
          catch (e) {
             alert(e.message);
          }
       }

That’s enough! I am going to finish the user story 1 soon.

NHibernate first attemp in blog system

As mention in the previous post, i started my application with first try in NHibernate. Wow it was sweat try. Eventually i got it works.

After nearly 4 hours fighting with NHibernate and my application, i had these in general:

general structure
general structure

Let says a little bit about them before continuing:

  • AnhDuc.Blog: Web site application
  • AnhDuc.Blog.Business: Project contains all business logics for the site
  • AnhDuc.Common: Project contains common things which can be used widely or in other projects.

Since i was using NHibernate then there was not DataAccess project.

Time to talk about using NHibernate. I have read some article and NHibernate In Action book to accomplish the usage. Basically here are steps:

Required Dlls:

Download NHibernate dlls and other required dlls. You can easy get them by using your friend: Google, thus i do not mention here.

Configuration:

First, you need these in webconfig. All you need to do is copy this part and paste in your config then change the connection string to the right one: 

< hibernate-configuration xmlns=urn:nhibernate-configuration-2.2 >
< session-factory>
<property name=connection.driver_class>NHibernate.Driver.SqlClientDriver</property>
<property name=connection.connection_string>Server=(local);initial catalog=anhduc_blog;Integrated Security=SSPI</property>
<property name=adonet.batch_size>10</property>
<property name=show_sql>false</property>
<property name=dialect>NHibernate.Dialect.MsSql2000Dialect</property>
<property name=use_outer_join>true</property>
<property name=command_timeout>60</property>
<property name=query.substitutions>true 1, false 0, yes ‘Y’, no ‘N’</property>
<property name=proxyfactory.factory_class>NHibernate.ByteCode.LinFu.ProxyFactoryFactory,

NHibernate.ByteCode.LinFu</property>

<mapping assembly=AnhDuc.Blog.Business />

</session-factory>

</hibernate-configuration>

Then i came with code to initialize the configuration: NHibernateSessionManager class.

public sealed class NHibernateSessionManager
{
private static readonly ISessionFactory _sessionFactory;
private const String CurrentSessionKey = “anhduc.blog.nhibernate.currentsession”;
static NHibernateSessionManager()
{
Configuration cfg = new Configuration();
//cfg.AddAssembly((typeof (BlogItem)).Assembly);
cfg.Configure();

_sessionFactory = cfg.BuildSessionFactory();
}

public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession session = context.Items[CurrentSessionKey] as ISession;
if(session==null)
{
session = _sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = session;
}
return session;
}

public static void CloseCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession session = context.Items[CurrentSessionKey] as ISession;
if (session == null)
return;
session.Close();
context.Items.Remove(CurrentSessionKey);
}

public static void CloseSessionFactory()
{
if (_sessionFactory != null)
_sessionFactory.Close();
}

public static void CommitTransaction()
{
GetCurrentSession().Transaction.Commit();
}

public static void AbortTransaction()
{
GetCurrentSession().Transaction.Rollback();
}
}

It contains some methods to deal with transaction.

The configuration is done, and ready to use. Come to your business class.

Business classes:

They are all under AnhDuc.Blog,Business project. Each class has its own mapping file. All mapping files are put under hbm folder. I am not going to explain in detail about mapping here. However, it is the most important thing when you work with NHibernate.

Be aware of inheritance when designing your domain. In many cases, inheritance is not encouraged.

Recommended book: NHibernate In Action June 2008