After taking a few days off from my project, and spending some time at the nhusers and DDD groups, I’d like to start looking at things again, and possibly building them here using your input. One reason I want to do this is to get a better direction to the project, and build on your experience. In addition, it will allow you all to see how I think, and make comments on things as they progress. So if you feel up to it, let’s do this together, one step at a time.
At the very heart of things, I needed a permission entity (app will provide a maintenance mode which will allow these to be altered or added and removed from). And so, that was born:
public class Permission : Entity
{
[Signature]
public virtual string Code { get; protected set; }
public Permission(string code)
{
Logic.Check(!string.IsNullOrEmpty(code) && !string.IsNullOrWhiteSpace(code), "Code can not be null, empty or whitespace!");
Code = code.Trim();
}
protected Permission() { }
}
I then needed roles which the above permissions could be assigned to, thereby giving the role usefulness.
public class Role : Entity
{
[Signature]
public virtual string Name { get; protected set; }
private IList<Permission> permissions = new List<Permission>();
public virtual IEnumerable<Permission> Permissions { get { return permissions; } }
public virtual void AddPermission(Permission permission)
{
Logic.Check(permission != null, "Permission can not be null!");
if (!permissions.Contains(permission))
permissions.Add(permission);
}
public Role(string name)
{
Logic.Check(!string.IsNullOrEmpty(name) && !string.IsNullOrWhiteSpace(name), "Name can not be null!");
Name = name.Trim();
}
protected Role() { }
}
Then I needed a user object to which roles could be assigned:
public class User : Entity
{
[Signature]
public virtual string Username { get; protected set; }
public virtual string Password { get; set; }
public virtual string Email { get; set; }
private IList<Role> roles = new List<Role>();
public virtual IEnumerable<Role> Roles { get { return roles; } }
public virtual void AddRole(Role role)
{
Logic.Check(role != null, "Role can not be null!");
if (!roles.Contains(role))
roles.Add(role);
}
public virtual void RemoveRole(Role role)
{
Logic.Check(role != null, "Role can not be null!");
if (roles.Contains(role))
roles.Remove(role);
}
public User(string username)
{
Logic.Check(!string.IsNullOrEmpty(username) && !string.IsNullOrWhiteSpace(username), "Username can not be null!");
Username = username.Trim();
}
protected User() { }
}
So things are starting to look pretty good in my opinion, but before I go on, can I get some comments and feedback on these three preliminary entities? Keep in mind that I have yet to add a lot of other properties to them that aren’t completely relavent at present. The two major questions I have at this point is in regards to the Role entity.
- Should it not also have a list of associated users?
- If so, should I allow for adding and removing of users from the role directly?
- Or do I have this backwards to start with?