NHibernate Mapping In-Depth from Ayenda
Thank for infoq site to write this out. I keep the link in my blog in order to preference it whenever needed.
Nhibernate mapping in depth from infoq site
Software Developer, Architect, and Personal Development
Thank for infoq site to write this out. I keep the link in my blog in order to preference it whenever needed.
Nhibernate mapping in depth from infoq site
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:
Let says a little bit about them before continuing:
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:
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
So far i have read many articles about web application development. So general speaking, i have tools in hand to build a website in no time with less effort. Just need to structure code and deal with business logic.
Here are some:
That’s quite enough! However i have never interated them in one application at all. That’s why i am going to make a simple web application to demonstrate my understanding.
The web is about a blog site with:
Assuming that, all data will be added into DB directly. What i want to archive are:
Along with the development process, i will update my blog. I am sure there will be more fun while doing it, yeah with difficulties as well.
ELMAH stands for Error Logging Modules And Handlers for ASP.NET.
Wondering in Scott Hanselman blog i know about this one. Scott Hanselman ELMAH
The tool helps us monitoring the exceptions have been happening during the life of web application. Why do you need that? Stack trace and its suffs are useful information in order to fix bug. With this tool, you do not need to modify, or recompile your application. Just a few steps with web.config then you get it in hand.
You might also want to read about rule of exception handling here
On Friday last week, while i was in my room, in the company, watching movie ( since out of work already and that was relaxing time). On of my friend on yahoo chat list buzzed me. Yeah, i had a chat without any doubt about him.
Things went a litte bit then he suddenly asked me: “Can you help me buy 10 vinagame cards for his little brother, just came from country side to HCM for fun? 60.000vnd/each”. Sure i said: “Yes”. I told him i would call him when i got back to my home, we live quite closely. I would called him afterward. However, he said that, his phone was out of battery and proposed me a solution: Got out of the company and bought the cards, went in the internet shop and sent the code via chat. Ok, we agreed on that.
Popped in an internet shop, asked for 10 cards, not paid yet. I went online and confirmed him again about type of card, since i did not know about them. Buzzed 1, 2, 3 times, no reply. Picked up my phone and tried to catch him. Oh No, the phone was ringing. The real person was talking with me. What i got was: he did not ask for any cards and he was drinking with his friends. Shit! i shouted out. Someone hacked his account and cheated me.
As a basic of thinking, i recalled about that friend, about his type of chat with me. Totally strange! However i did not realize. My friend got confirmed and changed the password.
I got a useful lession: DO NOT TRUST ONLINE, YOU SHOULD DO SOME BASIC CONFIRMATION BEFORE ACCEPTING, BELIEVING SOMETHING!