晚上花了两个小时时间,配置了一个Gentle并研究了一下。
版本:Gentle v1.2.6 下载页面http://sourceforge.net/project/showfiles.php?group_id=102032
下载的是完整版本,包含生成测试、文档、源码、输出、CodeSmith和MyGeneration代码模板,默认配置模板,这个小巧的DBRM工具基本上五脏俱全了。
步骤:
一.配置
在配置目录(Configurations)中有两个已经写好了的配置文件:
App.config
Gentle.config
Gentle官方文档声称可以只使用app.config或者web.config来配置,也可以单独写在一个Gentle.config文件中,我图省事,就将配置代码复制到自己的app.config(Asp.net中为web.config)中去了:
<configuration>
<appSettings>
... 原有appSettings配置
appSettings>
<configSections>
{这里放入gentle和log4net的配置节<section name= ... .../>}
configSections>
<gentle>
{复制gentle配置节的具体内容}
gentle>
<log4net>
{复制log4net配置节的具体内容}
log4net>
configuration>
1.选择自己需要的是哪种数据库,Gentle支持MSSQL、Oracle、MySql、Sybase、SqlLite等多种数据库,它为每种数据库配置在注释中写了一份配置信息,只需要将注释去调,并将其它类型数据库配置信息用注释起来即可,默认的是SQLServer,
设置连接字符串:

2.配置日志记录
默认的配置已经为你写好了,如果不想做日志记录可以在gentle配置节中的logging中将其关闭,注释中已经写得很清楚了,子节点Category设置一下属性:
并将其它Category子节点删除或注释掉。
3.添加引用
在应用程序中添加对下面程序集的引用:
Gentle.Common.dll
Gentle.Framework.dll
添加一个数据库引擎,我使用SQLServer,所以用这个
Gentle.Provider.SqlServer
添加一个日志记录引擎(也可以用另外一个引擎QuickGraph.*.dll大家可以试试看)
log4net.dll
OK。至此配置完毕
二.程序
1.数据实体
在Contributions目录中包含了一些生成代码的模板,有CodeSmith的,也有MyGeneration的,这里我用CodeSmith。
用CodeSmith打开GentleBusinessObject.cst文件
设置以下属性:(以Northwind的Categories表为例)
context类属性
ClassName:Category,生成类名
ColumnPreString:无,(要去除的列名的前缀,比如一些设计者喜欢用cat_catid,将cat_去掉)
ForceFirstLetterOnly:True(强制只将公开属性首字母大写)
ForcePrivateLowercase:True(强制私有变量首字母小写)
ForcePublicCapitalize:True(强制大写)
RemoveUnderscore:True(去掉下划线)
SourceTable:dbo.Category(表名)
VariablePreChar:不设置(加上前缀,默认为_)
NameSpace:输入命名空间(默认为数据库名)
可选设置:
Collections:None|ArrayList|xxxCollection(返回集合时可设置使用,还能使用CodeSmith的ArrayList.cst模板生成强类型集合,但是我看了cst的源码发现作者不推荐,不知道为何 )
IncludeListAll:True|False(获取全部数据集合,前提是上面Collection 不能为None)
IncludeVSSHeader:True(是否包含VSS头信息)
ListByString:True(是否为每个字符型列生成ListByXXXStartsWith方法)
ObjectsForForeighKeys:False (是否为每一个外键引用对象生成为延迟加载(lazy-loaded)属性)
完毕后点“Generate”在右边查看生成结果:
* $Id: $
* This file generated by CodeSmith on 2005年10月23日
*/
/*
* Template to generate a Gentle compatible business object from the database
* Copyright (C) 2004 Roger Hendriks, www.fenetre.nl & James M. Curran, www.noveltheory.com
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License 2.1 or later, as
* published by the Free Software Foundation. http://www.gnu.org/copyleft/lesser.html for details.
*
*/
using System;
using Gentle.Framework;
namespace TestNameSpace
{
#region Category
///
/// TODO add description of Category here.
///
[Serializable]
[TableName("Categories")]
public class Category : Persistent
{
#region Private member data
[TableColumn("CategoryID", NotNull=true), PrimaryKey(AutoGenerated=true) ]
protected int categoryID;
[TableColumn("CategoryName", NotNull=true) ]
protected string categoryName = String.Empty;
[TableColumn("Description") ]
protected string description = String.Empty;
[TableColumn("Picture") ]
protected byte[] picture;
#endregion
#region Constructors
///
/// Create Category from existing/full data set (used by the data layer).
///
public Category( int categoryID, string categoryName, string description, byte[] picture ) : base( true )
{
this.categoryID = categoryID;
this.categoryName = categoryName;
this.description = description;
this.picture = picture;
}
///
/// Select an existing Category given its unique identifier
///
static public Category Retrieve( int categoryID )
{
Key key = new Key( typeof(Category), true, "categoryID", categoryID );
return Broker.RetrieveInstance( typeof(Category), key ) as Category;
}
#endregion
#region Public Properties
///
/// Id accesses the CategoryID column of the Categories table.
/// This is the Identity column for the Table. It can only be read.
///
public int Id
{
get{ return categoryID; }
}
///
/// CategoryName accesses the CategoryName column of the Categories table.
///
public string CategoryName
{
get{ return categoryName; }
set{ categoryName = value; }
}
///
/// Description accesses the Description column of the Categories table.
///
public string Description
{
get{ return description; }
set{ description = value; }
}
///
/// Picture accesses the Picture column of the Categories table.
///
public byte[] Picture
{
get{ return picture; }
set{ picture = value; }
}
#endregion
/// TODO: Add Properties for Many-to-Many Relationship here.
/// (Note, they are included as comments in the class file for the linking table)
///
}
#region CodeSmith Parameters
// The follow parameters were used with CodeSmith to create this file:
//
// SourceTable : dbo.Categories
// ClassName: Category
// Namespace: TestNameSpace
// VariablePreChar:
// ColumnPreString:
// ForcePrivateLowercase: True
// ForcePublicCapitalize: True
// RemoveUnderscore: True
// IncludeVSSHeader: True
// ListByString: True
// ObjectsForForeignKeys: True
// IncludeListAll: True
// Collections: None
#endregion
#endregion Category
}
是不是很酷?它并不需要像NHibernate那样还得写一个*.hbm.xml文件,尽管那也可以用工具生成代码,但我总觉得NHibernate的这种做法有点多余。同样的内容要写两个地方感觉不好
2.操作
有了Category数据实体后,便可以对其进行操作,一个简单的操作例子:

Gentle.Framework.Broker.Insert(category);//插入记录
//声明一个Key,这里使用CategoryID列
Gentle.Framework.Key key = new Key(typeof(Category),true,"CategoryID",4);
//读取对象
Category category = Gentle.Framework.Broker.RetrieveInstance(typeof(Category),key) as Category;
//读取对象也可以这么写,注意如果获取不到记录会抛出异常
Category category = (Category)Gentle.Framework.Broker.RetrieveInstance(typeof(Category),key);
其它,可以通过Gentle读取单个实体,也可以读取父/子对象,支持列表读取、分页读取、结果可以自动缓存也可以手动缓存等,功能还是比较完善的,我暂时还没发现对于主键的生成有哪些支持,毕竟只用了一个晚上,好多功能慢慢发掘吧,休息了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步