[英]Splitting up a class into sub-classes
我有一個業務邏輯層類,其中包含數據庫中每個表的訪問方法。 由於現在有很多表,我想對其進行重組,以按實體對方法進行分組(以便在編碼時更易於訪問)。 因此,從此:
BLL.Database MyDB = new BLL.Database();
BLL.Entity.User MyUser = Database.UserGetById(42);
對此:
BLL.Database MyDB = new BLL.Database();
BLL.Entity.User MyUser = Database.User.GetById(42);
我希望該類在可能的情況下保持非靜態,而所有類也都是“部分”的(允許我向生成的主類中添加其他方法)。 我有什么選擇來實現這一目標? 這是當前的布局:
namespace BLL
{
public partial class Database
{
// private members..
// constructor
#region User
public IQueryable<BLL.Entity.User> UserGetAll()
{
// ...
}
public BLL.Entity.User UserGetById(int UserId)
{
// ...
}
public void UserSave(ref BLL.Entity.User user)
{
// ...
}
public void UserDelete(int userId)
{
// ...
}
#endregion
// More sets of methods for other entities in database here..
}
}
這可行嗎?
namespace BLL
{
public partial class Database
{
private _User;
public User
{
if (_User == null)
{
_User = new User();
}
return _User;
}
// Other data access classes here..
}
public partial class User
{
public IQueryable<BLL.Entity.User> GetAll()
{
// ...
}
// GetById, Save & Delete methods here..
}
}
謝謝。
除了使用partial
,您的布局看起來還不錯。 我不認為partial
可以為您提供足夠的擴展性,因為partial
類必須全部駐留在同一程序集中。
更好的解決方案可能是創建擴展方法 。
是的,那是可行的。 實際上,我實現了很多類似的東西。
當然,這樣做之后,我聽說了有關SQL的Linq,我們開始使用它了……所以您可能希望將其檢查為另一種選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.