📄 accessorbase.cs.svn-base
字号:
using System;
using System.Data;
using System.Data.Common;
using DatabaseUtil;
using Mied.BusinessObject;
using Mied.DAL.Exceptions;
using Mied.DAL.TableSchema;
using System.Collections;
using System.Collections.Generic;
using DatabaseUtil.Schemas;
namespace Mied.DAL.Accesses
{
public abstract class AccessorBase
{
internal AccessorBase(MiedDatabase database, string tableName)
{
this.Database = database;
this.TableName = tableName;
this.Database.Closed += new System.EventHandler(Database_Closed);
}
public void Insert(Entity entity)
{
entity.ID = null;
this.Save(entity);
}
public void Insert(Entity entity, DbTransaction transaction)
{
entity.ID = null;
this.Save(entity, transaction);
}
public void Save(Entity entity)
{
int? oldID = entity.ID;
using (DbConnection connection = this.Database.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
Save(entity, transaction);
CommandHelper.CommitTransaction(transaction);
}
catch (Exception ex)
{
transaction.Rollback();
entity.ID = oldID;
if (ex is SaveEntiyException)
throw ex;
else
throw new SaveEntiyException(ex, entity);
}
}
}
public void Save(Entity entity, DbTransaction transaction)
{
this.SaveBefore(entity, transaction);
DbConnection connection = transaction.Connection;
CommandFieldValueList pairList = this.BuildPairList(entity);
DbCommand command = null;
Entity entityOld = null;
if (entity.IsNew)
{
entity.ID = CommandHelper.GetNewID(transaction, this.TableName);
pairList.ID.Value = entity.ID;
command = CommandFactory.Insert(connection, this.TableName, pairList);
}
else
{
command = CommandFactory.UpdateByID(connection, this.TableName, pairList);
entityOld = this.Select(entity.ID.Value);
}
this.CommandSaveSupplement(command);
command.Transaction = transaction;
CommandHelper.ExecuteCommand(command);
this.SaveAfter(entity, entityOld, transaction);
this.FireSaved(entity);
}
public virtual void Delete(int id)
{
using (DbConnection connection = this.Database.CreateConnection())
{
connection.Open();
DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
this.Delete(id, transaction);
CommandHelper.CommitTransaction(transaction);
}
catch (Exception ex)
{
transaction.Rollback();
throw new DeleteEntiyException(ex);
}
}
}
public void Delete(int id, DbTransaction transaction)
{
this.DeleteBefore(id, transaction);
DbCommand command = CommandFactory.DeleteByID(transaction.Connection, this.TableName, Schema.FieldID, id);
command.Transaction = transaction;
CommandHelper.ExecuteCommand(command);
if (this.Deleted != null)
this.Deleted(id);
}
public IList SelectList()
{
DbCommand command = this.CommandSelectEntity();
IList list = this.CreateEntityList();
CommandHelper.ExecuteReader(command, this.ReadEntity, list);
return list;
}
public Entity Select(int id)
{
DbCommand command = this.CommandSelectEntity();
CommandFactory.WhereAnd(command, this.TableName + "." + Schema.FieldID, id);
Entity entity = (Entity)CommandHelper.ExecuteReader(command, this.ReadEntity);
return entity;
}
public delegate void EntityHandle(Entity entity);
public delegate void IDHandle(int id);
public event EntityHandle Saved;
public event IDHandle Deleted;
protected void FireDeleted(int id)
{
if (this.Deleted != null)
this.Deleted(id);
}
protected void FireSaved(Entity entity)
{
if (this.Saved != null)
this.Saved(entity);
}
protected Entity Select(string fieldName, object value)
{
DbCommand command = this.CommandSelectEntity();
CommandFactory.WhereAnd(command, this.TableName + "." + fieldName, value);
Entity entity = (Entity)CommandHelper.ExecuteReader(command, this.ReadEntity);
return entity;
}
protected void DeleteAll()
{
CommandHelper.DeleteAll(this.Database.Connection, this.TableName);
}
protected void DeleteAll(DbTransaction transaction)
{
DbCommand command = CommandFactory.DeleteAll(transaction.Connection, this.TableName);
command.Transaction = transaction;
CommandHelper.ExecuteCommand(command);
}
protected abstract CommandFieldValueList BuildPairList(Entity entity);
protected abstract Entity ReadEntity(RecordReader reader);
protected virtual void CommandSaveSupplement(DbCommand command)
{
}
protected virtual DbCommand CommandSelectEntity()
{
DbCommand command = CommandFactory.SelectAll(this.Database.Connection, this.TableName);
return command;
}
protected virtual IList CreateEntityList()
{
return new List<Entity>();
}
protected virtual void DeleteBefore(int id, DbTransaction transaction)
{
}
protected virtual void SaveBefore(Entity entity, DbTransaction transaction)
{
}
protected virtual void SaveAfter(Entity entity, Entity entityOld, DbTransaction transaction)
{
}
private void Database_Closed(object sender, System.EventArgs e)
{
OnDatabaseClosed();
}
protected virtual void OnDatabaseClosed()
{
}
protected readonly MiedDatabase Database;
protected readonly string TableName;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -