.NET 常用ORM之Gentle.Net

       .Net常用的就是微软的EF框架和Nhibernate,这两个框架用的都比较多就不做详细介绍了,今天我们来看看Gentle.Net,Gentle.Net是一个开源的优秀O/R Mapping的对象持久化框架。Gentle.Net在配置和使用上要远比NHibernate简单很多。

       Gentle.Net可以说是比较容易上手,语法使用也相对简单,但Gentle.Net的使用要依赖一个东西,那就是代码生成器,因为这对于它来说,是最重要的一步了,这个代码生成使用起来也很方便,本文也会给大家来介绍这个代码生成器的使用。Gentle.Net的优点是配置和使用都比较方便,所有能很多程度的减低开发成本。

 

首先你要是使用Gentle.Net需要下载几个东西:

①. Gentle.Net文件包(其中包含dll文件、类文件生成器模板)

Gentle.Net-1.5.0 下载文件包介绍:

Build\

强名密钥文件,NDoc文档生成文件等

Configuration\

配置文件示例,App.config也修改为Web.config文件。

Contributions\

代码生成器的模板文件,装上代码生成器之后双击这些文件就可以使用。

Documentation\

Gentle.Net相关的说明文档。

Output\

Gentle.Net的生成dll文件。

Source\

Gentle.Net源代码。

②.MyGeneration或CodeSmith代码生成器

下载的文件度娘上有很多链接地址,在这里笔者就不放下载链接了。 

 

下面说说具体使用:

1.新建一个工程,在下载的 Gentle.Net文件包找到所需的dll添加到项目中

2.建一个测试数据库(下文将以下表作为测试数据表)

 3.根据下载文件包Gentle.NET 1.5.0\Configuration\App.config 配置web.config文件,如下:其中两处log4的配置的文件如果不需要可以不做添加,gentle配置节点下

<DefaultProvider name="SQLServer" connectionString="Data Source=127.0.0.1;Initial Catalog=Test;User ID=sa;Password=XXXXXXXX;" />数据库连接改成自己的即可,还有<Providers>节点中把所要使用的SQLServer配置打开,把其他的数据都注释掉,配置文件就OK了。

4.代码生成器生成gentle所需要的实体类,一下将以codesmith做介绍:

(1)下载安装.....;

(2)可以找到gentle文件的中的生成器模板 F:\XXX\GentleNet\Gentle-1.5.0\Gentle.NET 1.5.0\Contributions\CodeSmith\GentleBusinessObject.cst,安装好了生成器工具就是可以双击打开模板,最主要需要改的就是SourceTable,其他都可以不做改动;

4.2.1>选择数据源

4.2.2>

4.2.3>

4.2.4>这里要说的是MSSql需要选择SqlSchemaProvider里面可没有SQLServer的选项

4.2.5>到了这个页面就很熟悉了,就是配置并选择数据源信息

4.2.6>选择完之后,选择Generate

4.2.7>生成的类代码大概长这样

/// <summary>
    /// TODO add description of Userinfo here.
    /// </summary>
    [Serializable]
    [TableName("UserInfo")]
    public class Userinfo : Persistent
    {
        #region Private member data
        [TableColumn("ID", NotNull=true), PrimaryKey(AutoGenerated=true) ]
        protected int id;
        [TableColumn("Name", NotNull=true) ]
        protected string name = String.Empty;
        [TableColumn("Age", NotNull=true) ]
        protected int age;
        [TableColumn("Sex", NotNull=true) ]
        protected int sex;
        [TableColumn("Men", NotNull=true) ]
        protected int men;
        [TableColumn("Remark") ]
        protected string remark = String.Empty;
        #endregion

        #region Constructors
        /// <summary>
        /// Create  Userinfo from existing/full data set (used by the data layer).
        /// </summary>
        public Userinfo( int id, string name, int age, int sex, int men, string remark ) : base( true ) 
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.men = men;
            this.remark = remark;

        }
        /// <summary>
        /// Select an existing Userinfo given its unique identifier
        /// </summary>
        static public Userinfo Retrieve( int id )
        {
            Key key = new Key( typeof(Userinfo), true, "id", id );
            return Broker.RetrieveInstance( typeof(Userinfo), key ) as Userinfo;
        }
        #endregion
        
        #region Public Properties
        ///<summary> 
        /// ID accesses the ID column of the UserInfo table.
        ///  This is the Identity column for the Table. It can only be read.
        ///</summary>
        public int ID 
        {
            get{ return id; }
        }
        
        ///<summary> 
        /// Name accesses the Name column of the UserInfo table.
        ///</summary>
        public string Name 
        {
            get{ return name; }
            set{ name = value; }
        }
        
        ///<summary> 
        /// Age accesses the Age column of the UserInfo table.
        ///</summary>
        public int Age 
        {
            get{ return age; }
            set{ age = value; }
        }
        
        ///<summary> 
        /// Sex accesses the Sex column of the UserInfo table.
        ///</summary>
        public int Sex 
        {
            get{ return sex; }
            set{ sex = value; }
        }
        
        ///<summary> 
        /// Men accesses the Men column of the UserInfo table.
        ///</summary>
        public int Men 
        {
            get{ return men; }
            set{ men = value; }
        }
        
        ///<summary> 
        /// Remark accesses the Remark column of the UserInfo table.
        ///</summary>
        public string Remark 
        {
            get{ return remark; }
            set{ remark = value; }
        }
        
View Code

 

5.类文件生成完成,将类代码copy到项目中,就可以享用GentleNet的便利,记得项目文件得引用Gentle相关dll文件,否则实体会报错

 

6.代码简单调用测试

导入命名空间

using Gentle.Common;
using Gentle.Framework;
using Gentle.Provider.SQLServer;
using Model;
View Code

实际使用

 public ActionResult Index()
        {
            //1.新增
            //Userinfo modUser = new Userinfo(6, "Jojo", 22, 1, 1, "GentleNetTest");
            //Gentle.Framework.Broker.Insert(modUser);



            // 2.复杂查询[支持t-sql]
            string sql = "select * from UserInfo where name like '%himi%'";
            int s = Gentle.Framework.Broker.Execute(sql).Rows.Count;


            return View();
        }
View Code

看到测试数据的变好了嘛?本文只是对gentle的一个小试牛刀,当然这只是最基本的使用,还有更多更复杂的使用,具体的要根据实际业务来定,这里就不细说了。。。

 

 

 

 

 

posted @ 2018-04-16 10:59  HI_Hub_MI  阅读(270)  评论(0编辑  收藏  举报