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:
Adding words
Displaying random words
Might want to search for some words
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.
Place to add new word directly.
Place to display random words.
A set of commands to be called from main view, like: create new word, edit word, create new dictionary, create new category,
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:
User control for adding new word
User control for displaying random words
One thing lefts, how to position them in main view?
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:
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
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
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???
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
I did not understand the article from Ayende clearly. Not read it carefully enough.
Wow reading MVVM one time does not mean i understand it completely. I know it but NOT GOT it.
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:
Design the GUI. It is as a part of learning XAML.
Re-read 2 articles from should read.
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.
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:
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.
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.
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:
ADO with data service: implement as webserver when you want to ask something from server.
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.
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.