LINQ to SQL、NHibernate比较(三)-- NHibernate实例

在上一篇博文中,为朋友们共享了一个简单的LINQ to SQL示例系统程序,为了比较LINQ to SQLNHibernate在使用上不同,这里使用NHibernate实现一个完全一样的系统。
      源码下载/Files/chenl861004/MyNHibernate.rar

1、  简单系统

请参见LINQ to SQL、NHibernate比较(二)-- LINQ to SQL实例

2、  系统实现

使用与LINQ to SQL、NHibernate比较(二)-- LINQ to SQL实例基本相同的方法来建立这个简单系统,不同的是现在实体类不是拖过去就自动生成的,而是要我们自己来写了。

 

ContractedBlock.gif ExpandedBlockStart.gif Item
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyNHibernate.DataObj
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// Item表对应的类
    
/// </summary>

    class Item
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
字段#region 字段
        
private int iD;//物料编号
        private string storage;//所在仓库
        private string itemKind;//物料类型
        #endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 物料类型
        
/// </summary>

        public virtual int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return iD; }
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (iD != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    iD 
= value;
                }

            }

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 所在仓库
        
/// </summary>

        public virtual string Storage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return storage; }
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (storage != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    storage 
= value;
                }

            }

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 物料类型
        
/// </summary>

        public virtual string ItemKind
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return itemKind; }
            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (itemKind != value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    itemKind 
= value;
                }

            }

        }

        
#endregion

    }

}

 

映射文件:

ContractedBlock.gif ExpandedBlockStart.gif Item.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping 
    
xmlns="urn:nhibernate-mapping-2.2" 
    default-lazy
="false"
    assembly
="MyNHibernate"
    namespace
="MyNHibernate.DataObj"><!--assembly:程序集;namespace:命名空间-->
    
    
<class 
        
name="Item" 
        table
="Item" >
        
        
<id 
            
name="ID" 
            type
="Int32" 
            column
="物料编号"
        
/>
        
        
<property 
            
name="Storage" 
            column
="所在仓库"
            type
="String"
        
/>
        
<property 
            
name="ItemKind" 
            column
="物料类型" 
            type
="String"
        
/>
        
    
</class>
</hibernate-mapping>


    其他两个类类似,就不贴出来了。

 

数据库连接类:

ContractedBlock.gif ExpandedBlockStart.gif DataConnection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace MyNHibernate.Methods
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 数据库连接的类(多个界面共享一个链接,提高效率)
    
/// </summary>

    class DataConnection
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
字段#region 字段
        
//配置类,读取全局配置文件hibernate.cfg.xml的相关信息
        
//包括数据源信息和需要加载的实体类映射文件
        private static Configuration config = new Configuration().Configure();
        
//工厂类,ISessionFactory是一个全局对象,允许多线程同时访问。
        private static ISessionFactory factory = config.BuildSessionFactory();
        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
属性#region 属性
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 配置类
        
/// </summary>

        public static Configuration Config
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return config; }
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 工厂类
        
/// </summary>

        public static ISessionFactory Factoty
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return factory; }
        }

        
#endregion

    }

}

 

 窗体方法类:

 

ContractedBlock.gif ExpandedBlockStart.gif Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Collections;
using System.Transactions;
using NHibernate;
using NHibernate.Cfg;
using MyNHibernate.DataObj;


namespace MyNHibernate.Methods
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// 泛型类,传入类型参数
    
/// </summary>
    
/// <typeparam name="TbeToOpe"></typeparam>

    class Methods<TbeToOpe>
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
字段#region 字段
        
//会话类,每个界面每次都实例化一个,关闭窗口时关闭连接
        private static ISession session = DataConnection.Factoty.OpenSession();
        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
方法#region 方法
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 取得数据源
        
/// </summary>

        public IList GetDataSource()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return session.CreateCriteria(typeof(TbeToOpe)).List();
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 一键式实现实体类与数据库同步
        
/// </summary>

        public void DoChange(BindingSource bs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//实例化事务
            using (TransactionScope ts = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//选择对话框
                    if (MessageBox.Show("确定执行对数据库的操作吗?""确定操作",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question) 
== DialogResult.OK)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//增删改
                        InsUpDel(bs);
                        
//提交到数据库
                        session.Flush();
                        MessageBox.Show(
"对数据库的操作成功!""操作成功!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }

                    
//捕捉异常
                catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    MessageBox.Show(ex.Message, 
"操作失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                
//事务结束
                ts.Complete();
            }

        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 窗体关闭事件
        
/// </summary>

        public void FormsClosing(BindingSource bs, FormClosingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
using (TransactionScope ts = new TransactionScope())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    DialogResult dr 
= MessageBox.Show("确定保存对数据库的操作吗?""退出提示",
                       MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    
if (dr == DialogResult.Yes)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
//增删改
                        InsUpDel(bs);
                        
//提交到数据库
                        session.Flush();
                    }

                    
else if (dr == DialogResult.Cancel)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        e.Cancel 
= true;
                    }

                }

                    
//捕捉异常
                catch (Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    e.Cancel 
= true;
                    MessageBox.Show(ex.Message, 
"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                
//事务结束
                ts.Complete();
            }
 
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 实现与数据库的同步
        
/// </summary>
        
/// <param name="bs"></param>

        private void InsUpDel(BindingSource bs)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
foreach(TbeToOpe tto in bs)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//增、改
                session.SaveOrUpdateCopy(tto);
            }

            
foreach (TbeToOpe tto in GetDataSource())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//
                if (!bs.Contains(tto))
                    session.Delete(tto);
            }

        }

        
#endregion

    }

}

 前台窗体代码:

ContractedBlock.gif ExpandedBlockStart.gif ItemForm
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyNHibernate.Methods;
using MyNHibernate.DataObj;

namespace MyNHibernate
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
    
/// Item处理窗体
    
/// </summary>

    public partial class ItemForm : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//实例化一个方法类
        Methods<Item> ItemMethods = new Methods<Item>();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 构造函数
        
/// </summary>

        public ItemForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }

        
//窗体加载
        private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//初始化
            itemBindingSource.DataSource = ItemMethods.GetDataSource();
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 一键式实现数据库增删改
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void buttonGo_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ItemMethods.DoChange(itemBindingSource);
            
//重新绑定数据源
            itemBindingSource.DataSource = ItemMethods.GetDataSource();
        }

        
//窗体关闭
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            ItemMethods.FormsClosing(itemBindingSource, e);
        }

    }

}

3、小结
            NHibernate还是很强大的,不过配文件和映射文件刚开始学习时一直不能正确配置,导致这个实例多半的时间是在写这些文件。。。囧。。。

转载于:https://www.cnblogs.com/chenl861004/archive/2009/09/28/1575551.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值