Question

I have the following database model:

[User]
Id
Name

[Role]
Id
Name

[UserRole]
UserId
RoleId
IsActive 

And I want to create a nice way to represent this relationship and the property that is in it with objects without creating a class to represent UserRole table.

Any ideas?

Thanks a lot!

Was it helpful?

Solution

Just have an attribute of a user object called "roles", which contains a list of roles.

You can also have 2 attributes, one for a list of active roles one for inactive ones, in case you need to manage the active flag using that object.

Also, you can have a role object's attribute listing the users in that role (either instead of or in additional to the role attribute in user object) - again possibly with "inactive" copy.

OTHER TIPS

Create a view that joins UserRole and Role:

[VUserRole]
UserId
RoleId
RoleDescription
IsActive

and model it with a UserRole class. Then User has a collection of UserRole and IsActive is an attribute of UserRole.

Note that this solution won't play well with most (any?) persistence framework, and the "right" way to do it is to map the join table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top