-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
55 lines (48 loc) · 1.57 KB
/
Database.cs
File metadata and controls
55 lines (48 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Configuration.Internal;
using System.Linq;
using System.Net;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using SimpleBlog.Models;
namespace SimpleBlog
{
public static class Database
{
private const string SessionKey = "SimpleBlog.Database.SessionKey";
private static ISessionFactory _sessionFactory;
public static ISession Session
{
get { return (ISession) HttpContext.Current.Items[SessionKey]; }
}
public static void Configure()
{
var config = new Configuration();
//configure the connection string
config.Configure();
//add our mappings
var mapper = new ModelMapper();
mapper.AddMapping<UserMap>();
mapper.AddMapping<Role.RoleMap>();
mapper.AddMapping<TagMap>();
mapper.AddMapping<PostMap>();
config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
//create session factory
_sessionFactory = config.BuildSessionFactory();
}
public static void OpenSession()
{
HttpContext.Current.Items[SessionKey] = _sessionFactory.OpenSession();
}
public static void CloseSession()
{
var session = HttpContext.Current.Items[SessionKey] as ISession;
if (session != null)
session.Close();
HttpContext.Current.Items.Remove(SessionKey);
}
}
}