星空网 > 软件开发 > ASP.net

瞎折腾之 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.

配置映射文件

实体类和对应的实体映射

添加一个User_Info实体类

瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)
  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  }

View Code

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

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

瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)
<??><hibernate-mapping ="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>

View Code

配置数据库

teamcity-hibernate.cfg.

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

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

<??><hibernate-configuration ="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源码)

扩展

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

什么是Ninject ?为什么要用?

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

园子里的文章学习借鉴:

Tyler‘s Blog:http://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(1017);      return View(model);    }

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

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

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

总结

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

源码下载

GIT获取源码:https://git.oschina.net/lxsweat/LX.NHibernate.V1_Company.git

点击下载:源码(没有Ninject、正好可以自己试试)

 

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

 




原标题:瞎折腾之 NHibernate ORM框架的接触(MVC + Repository源码)

关键词:Hibernate

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

重要通知:Shopee开店无需任何费用:https://www.ikjzd.com/articles/9027
弄懂Shopee的藏价逻辑很重要!藏价后运费的计算办法。:https://www.ikjzd.com/articles/9031
运营Shopee必须掌握的关键字广告技巧:https://www.ikjzd.com/articles/9036
官宣:天泽信息已成功并购有棵树:https://www.ikjzd.com/articles/9038
美日贸易谈判受阻,日本表示农业问题绝不让步:https://www.ikjzd.com/articles/90458
亚马逊开始整改配送中心,FBA卖家或将受影响!:https://www.ikjzd.com/articles/90459
如何从抖音上多挣钱?记住这12个字 :https://www.kjdsnews.com/a/1836445.html
连麦专家:2024年到底如何做小红书? :https://www.kjdsnews.com/a/1836446.html
相关文章
我的浏览记录
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流