Introduction to the nhibw.orm framework (MVC + Repository source code) and nhibernateorm

Source: Internet
Author: User
Tags mssql server

Introduction to the nhibw.orm framework (MVC + Repository source code) and nhibernateorm

In the age of continuous fire and rapid technological updates, I don't know, even use 2 ~ I am embarrassed to say how many years of work experience I have in the three frameworks. In addition, it would be a bit arrogant to go out for an interview.

This time, nhib.pdf is involved. In fact, these ORM frameworks are encapsulated for users. It's not that difficult. It is mainly to have a pair of eyes that are good at discovering technical points and desire for new technologies. You can also learn about it on a platform. For example, I read Jin sanyin Si last time and shared my job search experience. In this article, the interviewer asked me which ORM frameworks I used. When the landlord mentioned Dapper, I came to my interest and began to query relevant information. Of course, this article is not about Dapper, and may be about to continue in the future.

So I wrote this article about NHibernate and Dapper. Will someone study these things?

When I got used to EF and got started with nhib.pdf, I suddenly felt that there were so many configurations. This is troublesome.

This time I am not just talking about the use of nhib.pdf. I also learned a little about Repository warehousing. Let's take a look at the shortcomings. I hope you will not be misled. Please point out any errors. Thank you!

Small Frame

You can build a small framework that is suitable for your use and convenient. If you have any information, please leave a message below. I also hope to give some comments and suggestions, such as good articles and books. Now is the learning stage, hoping to get help from others. Thank you!

The source code is here! Source code (you can try it yourself without Ninject)

What is nhib.pdf?

Nhib is an object/Relational Database ing tool for. NET environments. The term object/relational ing (ORM) represents a technology used to map objects represented by the object model to the SQL-based relational model data structure.

Articles in the garden:

Xi'an _ Wang Lei: http://www.cnblogs.com/stone_w/archive/2011/09/15/2177830.html

Liu Dong: http://www.cnblogs.com/GoodHelper/tag/NHibernate/

Inverse heart: http://www.cnblogs.com/kissdodog/category/453550.html

 

Follow the tutorial to configure it.

Note:

1. ing file properties --> Generate operations --> embedded Resources

2. teamcity-hibernate.cfg.xml database profile --> copy to output directory --> always copy

Configure ing file object class and corresponding object ing

Add a User_Info object class

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

Ing file configuration details: nhib.pdf ing file configuration instructions

Add the ing file corresponding to the User_Info object class and the corresponding field attribute. The type must be the same.

<? Xml version = "1.0" encoding = "UTF-8"?> <Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2"> <class name = "LX. nhib.pdf. data. domain. user_Info, LX. nhib.pdf. 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> Teamcity-hibernate.cfg.xml is similar to Config

Configuration Description: attributes of the configuration file in nhib.pdf

Database link string core: connection. connection_string

<?xml version="1.0" encoding="utf-8" ?>Required dll Resources

Dll is provided in the source code.

How does one access the underlying layer?

Look at the underlying Writing Method

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();            }        }
Extension

Since it's a fun task, I want to join Ninject and try it!

What is Ninject? Why?

Ninject is an IOC container used to solve the coupling problem of components in the program. It aims to achieve minimal configuration. Other IOC tools are too dependent on the configuration file and need to use the assembly-qualified name for definition. long and complex IOC tools often destroy programs by typing wrong characters. These are their advantages and the reason for choosing them. Ninject cannot be hot-swappable at the same time.

Learning from articles in the garden:

Taylor's Blog: http://www.cnblogs.com/tylerdonet/p/3297915.html

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

Use Ninject

1. Create a NinjectControllerFactory class first.

Public class Identifier: identifier {private IKernel ninjectKernel; public identifier () {ninjectKernel = new StandardKernel (); identifier () ;}protected override IController GetControllerInstance (RequestContext requestContext, Type controllerType) {return controllerType = null? Null: (IController) ninjectKernel. get (controllerType);} private void AddBindings () {// todo: bind ninjectKernel. bind <LX. nhib.pdf. service. IUser_InfoManage> (). to <LX. nhib.pdf. service. implement. user_InfoManage> ();}}

2. register a Global file in the Application_Start method of the Global. asax file.

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

The AddBindings () method of the factory class is used for injection. Let's take a look at the code we started to write as follows:

We need to create an object when calling the Service layer method.

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

What about now? We can use Ninject injection to complete the new step.

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

In fact, I personally think that it seems that there is no time to worry about new. Haha, a typical cool! We recommend any good articles about Ninject. Thank you!

Summary

If you have any information, please leave a message below. I also hope to give some comments and suggestions, such as good articles and books. Now is the learning stage, hoping to get help from others. Thank you!

Source code download

GIT get source code: https://git.oschina.net/lxsweat/LX.NHibernate.V1_Company.git

Click to download: source code (you can try it yourself without Ninject)

 

From: http://www.cnblogs.com/lxsweat/p/4447154.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.