瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)(一)

时间:2023-03-10 06:28:23
瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)(一)

  在这炮火连天、技术更新迅猛的年代,不接触了解、甚至会用2~3种框架都不好意思说自己有多少年工作经验。况且出去面试也会有点吹牛的底子嘛。

  这次折腾了NHibernate、其实这些ORM框架封装好了都是给人用的。基本的也不会太难,深究就暂时不谈。主要是要有一双善于发现技术点的眼睛、和对新技术的渴望。或者通过一个平台去了解。比如说:我上次看了 金三银四,分享自己找工作的经历 这篇文章,里面讲到面试官问到了用过哪些ORM框架。楼主提到了Dapper,我就来了兴趣,开始查询有关资料。当然这篇文章不是折腾Dapper的、以后可能会继续折腾。

  所以,我这次写了这篇关于NHibernate的文章、也提到了Dapper,会不会也有人去了解去研究这些东西呢?

  习惯了EF、接触到NHibernate突然就觉得怎么这么多配置呢。好麻烦的说。

  这次我不只是提到单纯的NHibernate的运用。还加入了一点有关Repository仓储的东西,我也是懂一点而已。正好写出来让大家看看不足之处。希望别误导大家,有错误之处请指出。谢谢!

小型框架截图

  自己没事搭的小框架、适合自己用、方便就好。大家有什么指出的请在下方留言。也希望给一些意见和建议,比如好的文章和书。现在是学习的阶段,希望能得到好心人的帮助。谢谢大家!

源码在这里!源码(没有Ninject、正好可以自己试试)

瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)(一)

什么是NHibernate?

  NHibernate是一个面向.NET环境的对象/关系数据库映射工具。对象/关系数据库映射(object/relational mapping,ORM)这个术语表示一种技术,用来把对象模型表示的对象映射到基于SQL的关系模型数据结构中去。

园子里的文章:

西安_王磊http://www.cnblogs.com/stone_w/archive/2011/09/15/2177830.html

刘冬http://www.cnblogs.com/GoodHelper/tag/NHibernate/

逆心http://www.cnblogs.com/kissdodog/category/453550.html

按照教程配置应该不会有问题的

注意:

1、映射文件的属性-->生成操作-->嵌入的资源

2、teamcity-hibernate.cfg.xml数据库配置文件 -->复制到输出目录-->始终复制

配置映射文件

实体类和对应的实体映射

添加一个User_Info实体类

    public class User_Info
{
#region Model
public virtual int User_ID { set; get; }
public virtual string User_Name { set; get; }
public virtual string Password { set; get; }
public virtual string Mobile { set; get; }
public virtual string Source { set; get; }
public virtual float Lat { set; get; }
public virtual float Lng { set; get; }
public virtual string Weixin_NickName { set; get; }
public virtual string Weixin_OpenID { set; get; }
public virtual string Role_Code { set; get; }
public virtual int Login_Count { set; get; }
public virtual DateTime LastLogin_Date { set; get; }
public virtual string LastLogin_IP { set; get; }
public virtual string Create_IP { set; get; }
public virtual DateTime Create_Date { set; get; }
public virtual int Status { set; get; } #endregion Model
}

映射文件配置详情说明:NHibernate之映射文件配置说明

添加User_Info实体类对应的映射文件、和对应的字段属性一样即可,类型必须一样

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
<class name="LX.NHibernate.Data.Domain.User_Info,LX.NHibernate.Data.Domain" table="User_Info" lazy="true" >
<id name="User_ID" column="User_ID" type="int" >
<generator class="native"/>
</id>
<property name="User_Name" type="string">
<column name="User_Name" length="50"/>
</property>
<property name="Password" type="string">
<column name="Password" length="50"/>
</property>
<property name="Mobile" type="string">
<column name="Mobile" length="50"/>
</property>
<property name="Source" type="string">
<column name="Source"/>
</property>
<property name="Lat" type="float">
<column name="Lat"/>
</property>
<property name="Lng" type="float">
<column name="Lng"/>
</property>
<property name="Weixin_NickName" type="string">
<column name="Weixin_NickName" length="200"/>
</property>
<property name="Weixin_OpenID" type="string">
<column name="Weixin_OpenID" length="200"/>
</property>
<property name="Role_Code" type="string">
<column name="Role_Code" length="50"/>
</property>
<property name="Login_Count" type="int">
<column name="Login_Count"/>
</property>
<property name="LastLogin_IP" type="string">
<column name="LastLogin_IP"/>
</property>
<property name="Create_IP" type="string">
<column name="Create_IP"/>
</property>
<property name="Create_Date" type="DateTime">
<column name="Create_Date"/>
</property>
<property name="LastLogin_Date" type="DateTime">
<column name="LastLogin_Date"/>
</property>
<property name="Status" type="int">
<column name="Status" />
</property>
</class>
</hibernate-mapping>

配置数据库

teamcity-hibernate.cfg.xml 类似Config

配置说明详情:NHibernate之配置文件属性说明

数据库链接字符串核心:connection.connection_string

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<bytecode-provider type="lcg"/>
<reflection-optimizer use="true"/>
<session-factory name="LX.NHibernate">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider, NHibernate</property>
<property name="cache.use_query_cache">true</property>
<property name="query.startup_check">false</property>
<!--
The valid strings for Isolation can be found in the documentation for the System.Data.IsolationLevel
Enumeration documentation.
Use the member names - not the values.
-->
<property name="adonet.batch_size">10</property>
<property name="connection.isolation">ReadCommitted</property>
<property name="format_sql">true</property>
<!-- This is the System.Data.dll provider for MSSQL Server -->
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<!--<property name="dialect">NHibernate.Dialect.MySQLDialect</property>-->
<property name="connection.connection_string">
server=DIANSHISZH\DATA2008;database=test;initial catalog=test;user id=sa;password=123456
</property>
<property name="show_sql">false</property>
<property name="command_timeout">444</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="adonet.wrap_result_sets">false</property>
<mapping assembly="LX.NHibernate.Data.Domain"/>
</session-factory>
</hibernate-configuration>

需要的dll资源

dll在源码里面有提供

瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)(一)

底层到底是怎么访问的呢?

看看底层的写法

ISessionFactory sessionFactory = new Configuration().Configure(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "/Config/teamcity-hibernate.cfg.xml").BuildSessionFactory();

    public object Insert(T entity)
{
using (ISession session = sessionFactory.OpenSession())
{
var id = session.Save(entity);
session.Flush();
return id;
}
} public void Update(T entity)
{
using (ISession session = sessionFactory.OpenSession())
{
session.Update(entity);
session.Flush();
}
} public void Delete(T entity)
{
using (ISession session = sessionFactory.OpenSession())
{
session.Delete(entity);
session.Flush();
}
} public T Get(object id)
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Get<T>(id);
}
} public T Load(object id)
{
using (ISession session = sessionFactory.OpenSession())
{
return session.Load<T>(id);
}
} public List<T> LoadAll()
{
using (ISession session = sessionFactory.OpenSession())
{
return session.QueryOver<T>().List().ToList();
}
}

扩展

既然是瞎折腾嘛、那我加入Ninject 玩玩试试!

什么是Ninject ?为什么要用?

  Ninject是一个IOC容器用来解决程序中组件的耦合问题,它的目的在于做到最少配置。其他的的IOC工具过于依赖配置文件,需要使用assembly-qualified名称来进行定义,庸长且复杂常常因为打错字而破坏程序。这些是他的优点,也是为什么要选择它。Ninject同时不能进行热插拔。

园子里的文章学习借鉴:

Tyler‘s Bloghttp://www.cnblogs.com/tylerdonet/p/3297915.html

Liam Wang :http://www.cnblogs.com/willick/p/3299077.html

使用Ninject

1、我们先创建一个NinjectControllerFactory 工厂类

    public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
} private void AddBindings()
{
// todo:后面再来添加绑定
ninjectKernel.Bind<LX.NHibernate.Service.IUser_InfoManage>().To<LX.NHibernate.Service.Implement.User_InfoManage>();
}
}

2、在Global.asax文件Application_Start方法里面注册一个全局

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

工厂类的AddBindings() 方法的作用就是注入。让我们看看开始写的代码如下:

我们在调用Service层方法的时候得new一个对象。

     private IUser_InfoManage userInfoService = new User_InfoManage();
public ActionResult Index()
{
LX.NHibernate.Data.Domain.User_Info model = userInfoService.GetUser_Info(); return View(model);
}

现在呢?我们可以使用Ninject注入帮我们完成new那步操作

        private IUser_InfoManage userInfoService;
public UserController(User_InfoManage userService)
{
userInfoService = userService;
}

其实,我个人觉得哈,好像并没有省事到哪里去,还不如直接new呢。呵呵,典型的屌丝气质!大家有什么好的关于Ninject的文章也可以推荐一下、谢谢!

更新于2015-04-23 15:15

1、添加了通用分页方法以及分页条

2、添加Log4Net日志记录功能

一个项目需要的大致功能差不多就这些了吧。

总结

大家有什么指出的请在下方留言。也希望给一些意见和建议,比如好的文章和书。现在是学习的阶段,希望能得到好心人的帮助。谢谢大家!

源码下载

GIT获取源码:https://git.oschina.net/lxsweat/LX.NHibernate.V1_Company.git (git 上面的版本可能会及时完善代码和功能)

点击下载:源码(没有Ninject、正好可以自己试试,网盘的代码不维护完善功能)

原文来自:http://www.cnblogs.com/lxsweat/p/4447154.html