Im tring to implement the Repository pattern and all of the examples I have found are very simple (ignoring joins) or using entity framework.
My DB tables are as follows
tblUser {id, fname, lname, email, password, dateAdded}
tblAccount {id, name, isActive, dateAdded}
tblAccountUser {userId, accountId, isActive, dateAdded}
A user can have multiple accounts and an account can have multiple users. The tblUserAccount has a boolean that tells us if the user is active for the account and when the user was added.
my pocos map directly to the tables with an additional property for the relations. is there a better way to do this part? I could not think of a better way for my repository to return relations on something like GetUsersWithAccounts(userId).
tblUser {
guid id,
string fname,
string lname,
string email,
string password,
date dateAdded,
IList<tblAccount> accounts
}
tblAccount {
guid id,
string name,
bool isActive,
date dateAdded,
IList<tblUser> users
}
//should i have a tblAccountUser poco with something like
//{tblUser user, tblAccount account, bool isActive, date dateAdded}
I have the following repositories:
UserRepository {
Add()...
...
tblUser GetById(guid userId) {}
IEnumerable<tblUser> GetAll() {}
//was unsure if Account repo should retrieve a user's accounts or if the user repo should.
//using uow.User.GetAccounts(user.id) seems natural but please feel free to let me know what you think
IEnumerable<tblAccount> GetAccounts(guid userId){}
//this is the one i was really unsure about
//this would return tblUser obj with its tblUser.Accounts filled.
IEnumerable<tblUser> GetAllWithAccounts()
}
AccountRepository{
Add()
...
AddUser(guid userId) //only adds relation to tblAccountUser Makes sense?
}
//Should i have a repository for the Account <-> User relations?
Questions are all over the place, to sum up:
- When returning pocos from my repos how should i return relations. as i have shown in my pocos above or is there a better way?
- Should my relation tables get their own pocos? If so, is it only when they have additional data like isActive and user/account specific settings.
- In my repositories im unsure of which repo should handle specific requests when it comes to relations.
- Should i have another repo for account/user relations?
criticism, links, help all welcome Thanks.
Edit:
Additional note: Should have mentioned. The reason i want to get the user/accounts together is because they will be in a grid where we can activate/deactivate and modify values for user or their account/s.
A 1 & 3 When your UserRepository method returns a tblUser object, it doesn't need to populate the accounts. All non-collection properties should be handled by UserRepository . In the getter of accounts property, call methods in an AccountRepository to make the database call to get all accounts for the user first time the getter is being called.
A 2 Relation tables don't need their own pocos.
A 4 Create another repository to handle accounts separately.