Fluent NHibernate and Spring.net

http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/

http://comments.gmane.org/gmane.comp.windows.dotnet.nhibernate.user.general/21840

http://codegur.com/2049968/configuring-asp-net-mvc-2-with-spring-net-and-fluentnhibernate

SQL:

CREATE TABLE StaffManager
(
	ManagerId INT IDENTITY(1,1) PRIMARY KEY,
	ManagerStaffIdKey INT FOREIGN KEY
				REFERENCES StaffMember(StaffId),   --职员ID 外键
	ManagerIs BIT DEFAULT(1),						--账号是否有效
	ManagerName NVARCHAR(50) NOT NULL,				--登录账号
	ManagerPassWord VARCHAR(50) NOT NULL,	        --密码
	ManagerMail VARCHAR(100) NOT NULL,				--找回密码邮箱
	ManagerDate DATETIME DEFAULT(GETDATE())
	--管理人员ID 外键
)
GO

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate; //4.0
using NHibernate.Cfg;
using FluentNHibernate.Automapping;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System.Reflection;
using FluentNHibernate;
using NHibernate.Tool.hbm2ddl;
using NHibernate;

namespace BasicProject.NHibernateInfra.Implementation {
    public class FluentNhibernateLocalSessionFactoryObject : LocalSessionFactoryObject{
        ///// <summary>
        ///// Sets the assemblies to load that contain fluent nhibernate mappings.
        ///// </summary>
        ///// <value>The mapping assemblies.</value>
        //public string[] FluentNhibernateMappingAssemblies {
        //    get;
        //    set;
        //}

        public string[] FluentNhibernateMappingAssemblies { get; set; }
        public string ConnectionStringName { get; set; }
        static readonly object factorylock = new object();

        protected override void PostProcessConfiguration(Configuration config) {
            ConnectionStringName = "Server=geovindu;Database=geovindu;User ID=root;Password=geovindu";
            base.PostProcessConfiguration(config);
            FluentConfiguration fluentConfig = Fluently.Configure(config)
                .Database(MySQLConfiguration.Standard.ConnectionString(ConnectionStringName))
                .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));

            Array.ForEach(FluentNhibernateMappingAssemblies,
                           assembly => fluentConfig.Mappings(
                                                    m => m.FluentMappings.AddFromAssembly(Assembly.Load(assembly))
                                                        .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
                                                    )
                         );
            fluentConfig.BuildSessionFactory();
        }
    } //class
}

  

using System;
using System.Text;
using System.Collections.Generic;
using NHibernate.Validator.Constraints;

namespace Domain.Entities {

    public class StaffManager {
        public StaffManager() {
			CardCancellations = new List<CardCancellation>();
			CardRecords = new List<CardRecord>();
			JobRightsAssignments = new List<JobRightsAssignment>();
			StaffManagerLogs = new List<StaffManagerLog>();
			StaffUserChecks = new List<StaffUserCheck>();
			ViolateRegulations = new List<ViolateRegulation>();
        }
        public virtual int ManagerId { get; set; }
        public virtual StaffMember StaffMember { get; set; }
        public virtual bool? ManagerIs { get; set; }
        [NotNullNotEmpty]
        [Length(50)]
        public virtual string ManagerName { get; set; }
        [NotNullNotEmpty]
        [Length(50)]
        public virtual string ManagerPassWord { get; set; }
        [NotNullNotEmpty]
        [Length(100)]
        public virtual string ManagerMail { get; set; }
        public virtual DateTime? ManagerDate { get; set; }
        public virtual IList<CardCancellation> CardCancellations { get; set; }
        public virtual IList<CardRecord> CardRecords { get; set; }
        public virtual IList<JobRightsAssignment> JobRightsAssignments { get; set; }
        public virtual IList<StaffManagerLog> StaffManagerLogs { get; set; }
        public virtual IList<StaffUserCheck> StaffUserChecks { get; set; }
        public virtual IList<ViolateRegulation> ViolateRegulations { get; set; }
    }
}

  

using System;
using System.Collections.Generic;
using System.Text;
using FluentNHibernate.Mapping;
using Domain.Entities; 

namespace Domain.Mappings {

    /// <summary>
    ///
    /// </summary>
    public class StaffManagerMap : ClassMap<StaffManager> {

        public StaffManagerMap() {
			Table("StaffManager");
			LazyLoad();
			Id(x => x.ManagerId).GeneratedBy.Identity().Column("ManagerId");
			References(x => x.StaffMember).Column("ManagerStaffIdKey");
			Map(x => x.ManagerIs).Column("ManagerIs");
			Map(x => x.ManagerName).Column("ManagerName").Not.Nullable().Length(50);
			Map(x => x.ManagerPassWord).Column("ManagerPassWord").Not.Nullable().Length(50);
			Map(x => x.ManagerMail).Column("ManagerMail").Not.Nullable().Length(100);
			Map(x => x.ManagerDate).Column("ManagerDate");
			HasMany(x => x.CardCancellations).KeyColumn("CancelManagerIdKey");
			HasMany(x => x.CardRecords).KeyColumn("CardManagerIdKey");
			HasMany(x => x.JobRightsAssignments).KeyColumn("RightsManagerIdKey");
			HasMany(x => x.StaffManagerLogs).KeyColumn("ManagerIdKey");
			HasMany(x => x.StaffUserChecks).KeyColumn("CheckManagerIdKey");
			HasMany(x => x.ViolateRegulations).KeyColumn("ViolateManagerIdKey");
        }
    }
}

  

 public abstract class Repository<T> : ILongKeyedRepository<T> where T : class{
        private ISessionFactory sessionFactory;
        /// <summary>
        /// Session factory for sub-classes.
        /// </summary>
        public ISessionFactory SessionFactory {
            protected get { return sessionFactory; }
            set { sessionFactory = value; }
        }

        /// <summary>
        /// Get‘s the current active session. Will retrieve session as managed by the
        /// Open Session In View module if enabled.
        /// </summary>
        public ISession CurrentSession {
            get { return SessionFactory.GetCurrentSession(); }
        }

        public T FindBy(long id) {
            return CurrentSession.Get<T>(id);
        }

        public IQueryable<T> All() {
            return CurrentSession.Query<T>();
        }

        public T FindBy(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
            throw new NotImplementedException();
        }

        public IQueryable<T> FilterBy(System.Linq.Expressions.Expression<Func<T, bool>> expression) {
            return All().Where(expression).AsQueryable();
        }

        public void Add(T entity) {
            CurrentSession.Save(entity);
        }

        public void Add(IEnumerable<T> entities) {
            foreach (var entity in entities) {
                CurrentSession.Save(entity);
            }
        }

        public void Update(T entity) {
            CurrentSession.Update(entity);
        }

        public void Delete(T entity) {
            CurrentSession.Delete(entity);
        }

        public void Delete(IEnumerable<T> entities) {
            foreach (var entity in entities) {
                CurrentSession.Save(entity);
            }
        }
    } //end class

  

    public class StaffManagerRepository : Repository<StaffManager> {

    } /

  

时间: 2024-10-07 22:31:05

Fluent NHibernate and Spring.net的相关文章

[Fluent NHibernate]第一个程序

目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴趣,想对它一探究竟,这里也将Fluent Nhibernate写成一个系列,记录自己的学习过程,关于这东东,也是第一次接触,也只能边摸索,边记录了.如果有描述错误的地方,还望多多包涵. 通过Nhibernate的学习,都觉得Nhibernate的使用映射文件的编写很麻烦,这里通过Fluent Nhi

Fluent NHibernate and Mysql,SQLite

http://codeofrob.com/entries/sqlite-csharp-and-nhibernate.html https://code.google.com/archive/p/csharp-sqlite/downloads https://github.com/davybrion/NHibernateWorkshop MySQL /// <summary> ///MySQL 创建ISessionFactory /// </summary> /// <retu

[Fluent NHibernate]一对多关系处理

目录 写在前面 系列文章 一对多关系 总结 写在前面 上篇文章简单介绍了,Fluent Nhibernate使用代码的方式生成Nhibernate的配置文件,以及如何生成持久化类的映射文件.通过上篇的学习你会发现,Fluent Nhibernate仍然需要引用Nhibernate的两个程序集(Nhibernate.dll和Iesi.Collections.dll),所以与Nhibernate最大的区别就在生成配置文件的方式上面,这里关于Nhibernate的特性方面就不再多赘述,可以参考Nhib

【翻译】Fluent NHibernate介绍和入门指南

英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs.com/13yan/p/5685307.html 入门指南 Fluent NHibernate 概述 Fluent NHibernate 提供一个替代 NHibernate 的标准 XML 映射文件的方法.而不是编写 XML 文档 (.hbm.xml 文件),Fluent NHibernate 可

Fluent Nhibernate 数据库配置

一.Access数据库配置 NHibernate本身不支持Access数据库,一开始看网上各种文档,捣敲浪费了N分钟.还是祭起Nuget神器引用NHibernate.JetDrive.代码如下,搞定收工... private string FilePath { get; set; } private ISessionFactory BuildSessionFactory() { var connectionString = string.Format("Provider=Microsoft.Je

NHibernate+SQLite 学习笔记(二)+使用FLuent NHibernate 建立会话工厂(session factory) 并对数据库进行操作(open Session)

1.创建一个会话工厂(session factory) #region //********************创建一个会话工厂---Create Session Factory*******************// private void btnCreateSessionFactory_Click(object sender,RoutedEventArgs e) { var factory = CreateSessionFactory(); } private ISessionFac

Fluent Nhibernate and Stored Procedures

sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepName VARCHAR(50), PhoneNumber VARCHAR(50) ) GO CREATE PROCEDURE [dbo].[GetDepartmentId] ( @Id INT ) AS BEGIN SELECT * FROM Department WHERE Department.Id

NHibernate框架与BLL+DAL+Model+Controller+UI 多层架构十分相似--『Spring.NET+NHibernate+泛型』概述、知识准备及介绍(一)

原文://http://blog.csdn.net/wb09100310/article/details/47271555 1. 概述 搭建了Spring.NET+NHibernate的一个数据查询系统.之前没用过这两个框架,也算是先学现买,在做完设计之 后花了一周搭建成功了.其中,还加上了我的一些改进思想,把DAO和BLL之中相似且常用的增删改查通过泛型T抽象到了DAO和BLL的父类中,其DAO 和BLL子类只需继承父类就拥有了这些方法.和之前的一个数据库表(视图)对应一个实体,一个实体对应一

[转]ASP.NET MVC Spring.NET NHibernate 整合

请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需要对 ASP.NET,Spring.NET,NHibernate有所了解,最好是有一些经验,因为平白无故的就把3个技术融合到一起,会闹些小别扭,其实就像配置化学药品一样,如果3中原料有一点出路,那么整个实验也就失败了. 在网上有很多高人都写过类似的文章,可能是鄙人不才,看了很多文章都没有把我的问题解