Question

Trying to be a better programmer

I have an application that keeps track of Roles and Permissions, I had classes for Role and Permission which were just value objects.

class Role
{
  int RoleID
  string RoleName
}

class Permission
{
  int PermissionID
  string PermissionName
}

If I wanted to get the permissions for a Role I would do

List<Permission> permissions = PermissionDAL.GetPermissions(RoleID)

But for convenience (it's a Desktop application) I had to move the permission collection to the Role class so I could just do

permissions = roleInstance.GetPermissions();

and the Role class becomes

class Role
{
  List<Permission> permissions = null;
  int RoleID
  string RoleName

  public List<Permission> Permissions
  {
     get
     {
        if(permissions == null)
           permissions = PermissionDAL.GetPermissions(this.RoleID);

        return permissions;
     }
  }
}

Question 1: the challenge - here is the scenario

Role r = getARole();
   //for user to see
display(r.Permissions); 

   //user adding permission to role
RoleDAL.AddPermission(RoleID, PermissionID);

   //display new permissions
display(r.Permissions); 
   //But no, data is not refreshed because of the null check. 
   //Do I need to call a new instance of this Role?

Question 2 : I think this violate Inversion of Control?

Question 3 : How would you do an IoC for List<Role>, setting the permissions for each Role in the collection, if one were to use IoC.

Thanks

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top