π Role.cs Documentation
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Data Model:
RoleEntity - UML Class Diagram
- Usage Examples & Code Snippets
- Best Practices & Notes
- 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 theRoleclass toirby.Rolein 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
IsDeletedinstead of physical deletion. Always filter queries with!IsDeleted. - β° UTC Timestamps: Store
AddedDate/ModifiedDatein UTC to avoid timezone issues. - π€ Navigation Properties: Mark with
virtualto 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! π