Note that there are some explanatory texts on larger screens.

plurals
  1. POOverly accessible and incredibly resource hungry relationships between business objects. How can I fix this?
    primarykey
    data
    text
    <p>Firstly, This might seem like a long question. I don't think it is... The code is just an overview of what I'm currently doing. It doesn't feel right, so I am looking for constructive criticism and warnings for pitfalls and suggestions of what I can do.</p> <p>I have a database with business objects.<br> I need to access properties of parent objects.<br> I need to maintain some sort of state through business objects.</p> <p>If you look at the classes, I don't think that the access modifiers are right. I don't think its structured very well. Most of the relationships are modelled with public properties. SubAccount.Account.User.ID &lt;-- all of those are public..</p> <p>Is there a better way to model a relationship between classes than this so it's not so "public"?</p> <p>The other part of this question is about resources:</p> <p>If I was to make a User.GetUserList() function that returns a List, and I had 9000 users, when I call the GetUsers method, it will make 9000 User objects and inside that it will make 9000 new AccountCollection objects. What can I do to make this project not so resource hungry?</p> <p>Please find the code below and rip it to shreds.</p> <pre><code>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&lt;Users&gt; GetUsers() { return Data.GetUsers(); } } public AccountCollection : IEnumerable&lt;Account&gt; { private User user; public AccountCollection(User user) { this.user = user; } public IEnumerable&lt;Account&gt; 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&lt;SubAccount&gt; { public Account account {get; set;} public SubAccountCollection(Account account) { this.account = account; } public IEnumerable&lt;SubAccount&gt; 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&lt;Account&gt; GetSubAccounts(Account account) { using (var dc = new databaseDataContext()) { List&lt;SubAccount&gt; 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&lt;Account&gt; GetAccounts(User user) { using (var dc = new databaseDataContext()) { List&lt;Account&gt; 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&lt;User&gt; 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload