我個人同意,就像你在第一段所述信息庫應爲「乾淨」成爲可能。
根據經驗,我通過在CRUD函數中編寫業務邏輯代碼來「欺騙」,這往往要求其他存儲庫是屬性。
在ASP.net中工作時,我利用泛型和事件處理。
public class Repository<T, DC>
where T : class
where DC : DataContext, new()
{
public delegate void RecordInsertedHandler(object s, BasicEventArgs<T> e);
public event RecordInsertedHandler RecordInserted;
public delegate void RecordDeletedHandler(object s, BasicEventArgs<T> e);
public event RecordDeletedHandler RecordDeleted;
public delegate void RecordObtainedHandler(object s, BasicEventArgs<T> e);
public event RecordObtainedHandler RecordObtained;
public delegate void RecordUpdatedHandler(object s, BasicEventArgs<T> e);
public event RecordUpdatedHandler RecordUpdated;
protected DC dc;
public Repository(string connectionString="")
{
dc = ((connectionString == null) || (connectionString == "")) ? new DC() : DynamicTypes.Instantiate<DC>(connectionString); // See code below for DynamicTypes:
}
// There are similar functions for other events not shown.
protected void OnRecordInserted(BasicEventArgs<T> obj)
{
if (RecordInserted != null)
{
RecordInserted(this, obj);
}
}
// Only the Insert is shown here.
private void Insert(T obj)
{
dc.GetTable<T>().InsertOnSubmit(obj);
dc.SubmitChanges();
OnRecordInserted(new BasicEventArgs<T>(obj));
}
}
其基本思想是您可以在正常的CRUD函數的正確位置觸發/調用這些事件。我認爲你的版本庫將是ASP.net頁面或Windows窗體的成員。那些容器會成爲「事件監聽者」,因爲他們可以操縱UI。
動態類型:
public static class DynamicTypes
{
public static T Instantiate<T>(params object[] args)
{
return (T)Activator.CreateInstance(typeof(T), args);
}
}
BasicEventArgs:
public class BasicEventArgs<T> : EventArgs
{
private T _Data;
private string _Message;
public BasicEventArgs(T data, string message="") : base()
{
_Data = data;
_Message = message;
}
public T Data
{
get { return _Data; }
set { _Data = value; }
}
public string Message
{
get { return _Message; }
set { _Message = value; }
}
}
庫:
public class BranceshRepository : Repository<Branch, YourDataContext>
{
public BranchesRepository(string connectionString="") : base(connectionString)
{
}
}
public class ClientsRepository : Repository<Client, YourDataContext>
{
public ClientsRepository(string connectionString="") : base(connectionString)
{
}
}
現在,例如,我們有一個ASP.net頁:
public partial class MyPage : Page
{
protected ClientsRepository _ClientsRepository;
protected BranchesRepository _BranchesRepository;
protected void Page_Load(object s, EventArgs e);
{
_ClientsRepository = new ClientsRepository(...);
_BranchesRepository = new BranchesRepository(...);
_BranchesRepository.RecordInserted += new Repository<Branch,YourDataContext>.RecordInsertedHandler(OnBranchInserted);
_ClientsRepository.RecordInserted += new RepositoryM<Client, YourDataContext>.RecordInsertedHandler(OnClientInserted);
}
protected void OnBranchInserted(object s, BasicEventArgs<Branch> e)
{
/* e.Data is your newly-inserted branch with the newly-generated Id
from the database. You may save this branch to Session for
later use when your user inserts a new client.
*/
}
protected void OnClientInserted(object s, BasicEventArgs<Client> e)
{
Branch currentBranch = (Branch)Session["Branch"];
e.Data.BranchId = currentBranch.Id;
_ClientsRepository.Update(e.Data);
}
// Control event handlers not shown, like CreateClient_BT_Click, for example.
}
這樣,您不需要在ClientsRepository中注入單例,並且您可以在這些事件處理程序中完全訪問您的UI。
特別含糊,我什麼都聽不懂。你的系統是什麼(語言,圖書館,操作的基本概念)?你的情況是什麼「儲存庫」和「分支」?這也應該說明標識符的含義。 – 2012-12-09 11:26:38