πŸ“– Role.cs Documentation


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Data Model: Role Entity
    1. Class Declaration & Table Mapping
    2. Properties Reference
    3. NotMapped Properties
  4. UML Class Diagram
  5. Usage Examples & Code Snippets
  6. Best Practices & Notes
  7. Conclusion

1. Overview & Purpose

Role.cs defines the Role entity in the IrbyImaging domain.
It represents a security or organizational role (e.g., β€œAdministrator”, β€œTechnician”, β€œViewer”) within the system.

  • πŸ” Primary Function: Manage user roles, their descriptions, and associated permissions.
  • πŸ—ƒοΈ Persistence: Mapped to the database table irby.Role.
  • πŸ› οΈ Auditing: Tracks creation/modification metadata for robust change history.
  • πŸ”— Associations: Links roles to Permissions, Employees, and optionally to Departments.

2. Dependencies & Technologies

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
  • .NET Core / .NET Framework with Entity Framework (Code First).
  • DataAnnotations for validation & schema mapping.
  • GUID primary key strategy for security and distribution.

3. Data Model: Role Entity

3.1 Class Declaration & Table Mapping

namespace IrbyImaging.DomainClasses
{
    [Table("Role", Schema = "irby")]
    public class Role
    {
        // ... properties
    }
}
  • [Table("Role", Schema = "irby")]
    Maps the Role class to irby.Role in the database schema.

3.2 Properties Reference

Property Type Attributes Description
RoleId Guid [Key] Primary Key. Unique identifier for each role.
Name string [Display(Name="Role Name")] Human-readable role name (e.g., β€œAdmin”).
Description string [Display(Name="Role Description")] Brief description of role’s purpose.
IsDeleted bool Soft-delete flagβ€”true if role is archived.
AddedBy string Username or ID of the creator.
AddedDate DateTime UTC timestamp when created.
ModifiedBy string Username or ID of the last modifier.
ModifiedDate DateTime UTC timestamp of last update.

3.3 NotMapped Properties

These properties are not persisted but facilitate UX and internal logic:

Property Type Purpose
Department List<Department> Temporary container to assign multiple departments in UI flows.
CopyRolePermissions bool Flag to copy permissions from an existing role during role creation.
Permissions ICollection<Permissions> Navigation property for role’s permissions (lazy-loaded).
Users ICollection<Employee> Navigation property for users/employees assigned to this role.
[NotMapped]
public List<Department> Department { get; set; }

[NotMapped]
public bool CopyRolePermissions { get; set; }

[NotMapped]
public virtual ICollection<Permissions> Permissions { get; set; }

[NotMapped]
public virtual ICollection<Employee> Users { get; set; }

4. UML Class Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚             Role                β”‚
│─────────────────────────────────│
β”‚ - RoleId : Guid {PK}            β”‚
β”‚ - Name : string                 β”‚
β”‚ - Description : string          β”‚
β”‚ - IsDeleted : bool              β”‚
β”‚ - AddedBy : string              β”‚
β”‚ - AddedDate : DateTime          β”‚
β”‚ - ModifiedBy : string           β”‚
β”‚ - ModifiedDate : DateTime       β”‚
│─────────────────────────────────│
β”‚ + Department : List<Department> β”‚
β”‚ + CopyRolePermissions : bool    β”‚
β”‚ + Permissions : ICollection<>   β”‚
β”‚ + Users : ICollection<>         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. Usage Examples & Code Snippets

5.1 Creating & Saving a New Role

using (var context = new IrbyImagingContext())
{
    var newRole = new Role
    {
        RoleId = Guid.NewGuid(),
        Name = "Supervisor",
        Description = "Oversees daily operations",
        IsDeleted = false,
        AddedBy = currentUserId,
        AddedDate = DateTime.UtcNow,
        ModifiedBy = currentUserId,
        ModifiedDate = DateTime.UtcNow
    };

    context.Roles.Add(newRole);
    context.SaveChanges();
}

5.2 Querying Roles & Including Permissions

var rolesWithPermissions = context.Roles
    .Where(r => !r.IsDeleted)
    .Select(r => new 
    {
        r.RoleId,
        r.Name,
        r.Description,
        Permissions = context.Permissions
                             .Where(p => p.RoleId == r.RoleId)
                             .Select(p => p.PermissionName)
    })
    .ToList();

5.3 Soft-Deleting a Role

var role = context.Roles.Find(roleId);
if (role != null)
{
    role.IsDeleted = true;
    role.ModifiedBy = currentUserId;
    role.ModifiedDate = DateTime.UtcNow;
    context.SaveChanges();
}

6. Best Practices & Notes

  • πŸ” Soft Delete: Use IsDeleted instead of physical deletion. Always filter queries with !IsDeleted.
  • ⏰ UTC Timestamps: Store AddedDate/ModifiedDate in UTC to avoid timezone issues.
  • 🀝 Navigation Properties: Mark with virtual to enable EF’s lazy loading (if enabled).
  • πŸ“‹ Validation: Add [Required], [MaxLength] as needed for stricter schema validations.

7. Conclusion

The Role class is a cornerstone of IrbyImaging’s security and authorization model. By encapsulating role metadata, soft-delete semantics, audit trails, and navigational links to permissions and users, it empowers administrators to manage access control in a flexible, maintainable manner.

Whether creating new roles, assigning permissions, or auditing changes, Role.cs provides a clean, EF–friendly domain model that integrates seamlessly into the wider application architecture.

Happy coding! πŸš€