码迷,mamicode.com
首页 > 编程语言 > 详细

Fluent NHibernate and Spring.net

时间:2016-04-21 18:02:54      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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> {

    } /

  

Fluent NHibernate and Spring.net

标签:

原文地址:http://www.cnblogs.com/geovindu/p/5417907.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!