首先,这似乎是一个很长的问题。我不认为它是......代码只是我目前正在做的事情的概述。感觉不对,所以我正在寻找建设性的批评和警告,以了解我可以做什么的陷阱和建议。
我有一个包含业务对象的数据库。
我需要访问父对象的属性。
我需要通过业务对象维护某种状态。
如果您查看这些类,我认为访问修饰符不正确。我不认为它的结构很好。大多数关系都使用公共属性建模。SubAccount.Account.User.ID <--所有这些都是公开的..
有没有比这更好的方法来模拟类之间的关系,所以它不是那么“公开”?
这个问题的另一部分是关于资源:
如果我要创建一个返回列表的 User.GetUserList() 函数,并且我有 9000 个用户,那么当我调用 GetUsers 方法时,它将创建 9000 个用户对象,并且在其中将创建 9000 个新的 AccountCollection 对象。我该怎么做才能使这个项目不那么耗资源?
请在下面找到代码并将其撕成碎片。
public class User {
public string ID {get;set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string PhoneNo {get; set;}
public AccountCollection accounts {get; set;}
public User {
accounts = new AccountCollection(this);
}
public static List<Users> GetUsers() {
return Data.GetUsers();
}
}
public AccountCollection : IEnumerable<Account> {
private User user;
public AccountCollection(User user) {
this.user = user;
}
public IEnumerable<Account> GetEnumerator() {
return Data.GetAccounts(user);
}
}
public class Account {
public User User {get; set;} //This is public so that the subaccount can access its Account's User's ID
public int ID;
public string Name;
public Account(User user) {
this.user = user;
}
}
public SubAccountCollection : IEnumerable<SubAccount> {
public Account account {get; set;}
public SubAccountCollection(Account account) {
this.account = account;
}
public IEnumerable<SubAccount> GetEnumerator() {
return Data.GetSubAccounts(account);
}
}
public class SubAccount {
public Account account {get; set;} //this is public so that my Data class can access the account, to get the account's user's ID.
public SubAccount(Account account) {
this.account = account;
}
public Report GenerateReport() {
Data.GetReport(this);
}
}
public static class Data {
public static List<Account> GetSubAccounts(Account account) {
using (var dc = new databaseDataContext()) {
List<SubAccount> query = (from a in dc.Accounts
where a.UserID == account.User.ID //this is getting the account's user's ID
select new SubAccount(account) {
ID = a.ID,
Name = a.Name,
}).ToList();
}
}
public static List<Account> GetAccounts(User user) {
using (var dc = new databaseDataContext()) {
List<Account> query = (from a in dc.Accounts
where a.UserID == User.ID //this is getting the user's ID
select new Account(user) {
ID = a.ID,
Name = a.Name,
}).ToList();
}
}
public static Report GetReport(SubAccount subAccount) {
Report report = new Report();
//database access code here
//need to get the user id of the subaccount's account for data querying.
//i've got the subaccount, but how should i get the user id.
//i would imagine something like this:
int accountID = subAccount.Account.User.ID;
//but this would require the subaccount's Account property to be public.
//i do not want this to be accessible from my other project (UI).
//reading up on internal seems to do the trick, but within my code it still feels
//public. I could restrict the property to read, and only private set.
return report;
}
public static List<User> GetUsers() {
using (var dc = new databaseDataContext()) {
var query = (from u in dc.Users
select new User {
ID = u.ID,
FirstName = u.FirstName,
LastName = u.LastName,
PhoneNo = u.PhoneNo
}).ToList();
return query;
}
}
}