注意到:
1. 接口命名,我没有加I,这是特意的。
2. 用到了Query<>接口, 这个是对查询的一个抽象。好处是,不需要像大多数的仓储实现,要为每个类建立一个仓储接口,膨胀的很厉害。
Quer接口很简单,没有任何方法和属性,只是为了使用强类型。它的实现类会根据需要, 越来越多。 因为,查询几乎就是数据层的主要功能。
查询接口的定义:
- namespace Skight.Demo.Domain
- {
- public interface Query<Item>{}
- }
view raw gistfile1.txt This Gist brought to you by GitHub.
持久层 (数据层)
考试映射类:
- using FluentNHibernate.Mapping;
- using Skight.Demo.Domain.Examination;
- namespace Skight.Demo.NHRepository{
- public class ExamMap:ClassMap<Exam>{
- public ExamMap(){Id(x => x.Id);Map(x => x.Code);
- Map(x => x.Name);}}}
view raw gistfile1.cs This Gist brought to you by GitHub.
Fluent nHibernate对仓储接口的实现:
- using System;using System.IO;
- using System.Reflection;
- using FluentNHibernate.Cfg;
- using FluentNHibernate.Cfg.Db;
- using NHibernate;using NHibernate.Cfg;
- using NHibernate.Tool.hbm2ddl;
- namespace Skight.Demo.NHRepository{
- public class SessionProvider
- {
- #region Instance for use outside
- private static SessionProvider instance;
- public static SessionProvider Instance {
- get {
- if (instance == null)
- {
- instance = new SessionProvider();
- }
- return instance;
- } }
- #endregion
- #region Set up database
- private const string DBFile = "SkightDemo.db";
- public bool IsBuildScheme { get; set; }
- public void initilize()
- { session_factory = Fluently.Configure()
- .Database(SQLiteConfiguration.Standard.UsingFile(DBFile).ShowSql())
- .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
- .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "thread_static"))
- .ExposeConfiguration(build_schema)
- .BuildSessionFactory(); }
- private void build_schema(Configuration configuration)
- { if (IsBuildScheme)
- { new SchemaExport(configuration)
- .Execute(true, true, false);
- } } #endregion
- private readonly object lock_flag = new object();
- private ISessionFactory session_factory;
- public ISessionFactory SessionFactory {
- get {
- if (session_factory == null) {
- lock (lock_flag) {
- if (session_factory == null) {
- initilize();
- } }
- }
- return session_factory;
- } }
- public ISession CreateSession() {
- ISession session = SessionFactory.OpenSession();
- return session;
- }
- public ISession CurrentSession
- {
- get { return SessionFactory.GetCurrentSession(); }
- }
- }}
v