📄 LinkRolePermission.cs Documentation

🔗 File Path: IrbyImaging.DomainClasses/LinkRolePermission.cs
🛠️ Project: IrbyImaging.DomainClasses


📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Data Model & Schema Mapping
  4. Key Functional Areas
  5. Class Structure & Properties
  6. Entity Relationships
  7. UML Diagram
  8. Integration into DbContext
  9. Usage Examples
  10. Best Practices & Considerations
  11. Conclusion

1. Overview & Purpose

LinkRolePermission is a join entity within the irby schema that maps many‑to‑many relationships between Roles and Permissions.
Beyond simply linking two entities, it maintains:

  • 🔍 Audit Fields (AddedBy, AddedDate, ModifiedBy, ModifiedDate)
  • 🗑️ Soft‑Delete Flag (IsDeleted)

This design allows you to track who created/modified each link and when, as well as de‑activate links without physical deletion.


2. Dependencies & Technologies

Technology / Library Purpose
.NET (C#) Language
Entity Framework / DataAnnotations ORM Mapping ([Table], [Key], etc.)
System.ComponentModel.DataAnnotations Validation & Schema Attributes
System.ComponentModel.DataAnnotations.Schema Database Schema Mapping
System Core types (Guid, DateTime, etc.)

3. Data Model & Schema Mapping

[Table("LinkRolePermission", Schema = "irby")]
public class LinkRolePermission { /* ... */ }
  • Table Name: LinkRolePermission
  • Schema: irby

Database Columns

Property Column Type Constraints Description
Id uniqueidentifier Primary Key, No Auto‑Increment Unique link identifier
RoleId uniqueidentifier Foreign Key → Role(Id) References a Role
PermissionId uniqueidentifier Foreign Key → Permissions(Id) References a Permission
IsDeleted bit Not Null Soft‑delete flag
AddedBy nvarchar Username or identifier of creator
AddedDate datetime Creation timestamp
ModifiedBy nvarchar Username or identifier of last modifier
ModifiedDate datetime Last modification timestamp

4. Key Functional Areas

  1. Many‑to‑Many Link Entity
    Acts as a bridge table to assign multiple permissions to multiple roles.

  2. Audit Trail

    • Tracks who and when the link was created/modified.
    • Essential for compliance, logging, and troubleshooting.
  3. Soft‑Delete Mechanism

    • IsDeleted flag enables “archive” of links without physical deletion.
    • Retains history and allows easy restoration.

5. Class Structure & Properties

public class LinkRolePermission
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid Id { get; set; }

    public Guid RoleId { get; set; }
    public Guid PermissionId { get; set; }

    [ForeignKey("PermissionId")]
    public Permissions Permissions { get; set; }

    [ForeignKey("RoleId")]
    public Role Role { get; set; }

    public bool IsDeleted { get; set; }
    public string AddedBy { get; set; }
    public DateTime AddedDate { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
}
Property Type Notes
Id Guid Primary Key, must be provided externally (DatabaseGenerated.None).
RoleId Guid FK to Role entity.
PermissionId Guid FK to Permissions entity.
Permissions Permissions Navigation property for the linked Permission.
Role Role Navigation property for the linked Role.
IsDeleted bool Soft-delete indicator.
AddedBy string Creator identifier.
AddedDate DateTime Timestamp of creation.
ModifiedBy string Last modifier identifier.
ModifiedDate DateTime Timestamp of last update.

6. Entity Relationships

  • 🔄 LinkRolePermission (N) → (1) Permissions
  • 🔄 LinkRolePermission (N) → (1) Role

This is the classical bridge in a many‑to‑many association:

Role ──< LinkRolePermission >── Permissions

7. UML Diagram

classDiagram
    class LinkRolePermission {
      +Guid Id
      +Guid RoleId
      +Guid PermissionId
      +bool IsDeleted
      +string AddedBy
      +DateTime AddedDate
      +string ModifiedBy
      +DateTime ModifiedDate
    }

    class Role {
      +Guid Id
      +string Name
      ...
    }

    class Permissions {
      +Guid Id
      +string Code
      ...
    }

    LinkRolePermission --> Role : RoleId
    LinkRolePermission --> Permissions : PermissionId

8. Integration into DbContext

public class ApplicationDbContext : DbContext
{
    public DbSet<LinkRolePermission> LinkRolePermissions { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<Permissions> Permissions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // Additional Fluent‑API configurations if needed
        modelBuilder.Entity<LinkRolePermission>()
            .HasKey(lrp => lrp.Id);

        modelBuilder.Entity<LinkRolePermission>()
            .HasOne(lrp => lrp.Role)
            .WithMany(r => r.LinkRolePermissions)
            .HasForeignKey(lrp => lrp.RoleId);

        modelBuilder.Entity<LinkRolePermission>()
            .HasOne(lrp => lrp.Permissions)
            .WithMany(p => p.LinkRolePermissions)
            .HasForeignKey(lrp => lrp.PermissionId);

        base.OnModelCreating(modelBuilder);
    }
}

9. Usage Examples

9.1 Adding a Permission to a Role

var newLink = new LinkRolePermission
{
    Id             = Guid.NewGuid(),
    RoleId         = someRoleId,
    PermissionId   = somePermissionId,
    IsDeleted      = false,
    AddedBy        = currentUser,
    AddedDate      = DateTime.UtcNow,
    ModifiedBy     = currentUser,
    ModifiedDate   = DateTime.UtcNow
};

dbContext.LinkRolePermissions.Add(newLink);
dbContext.SaveChanges();
var link = dbContext.LinkRolePermissions.Find(existingLinkId);
if (link != null)
{
    link.IsDeleted    = true;
    link.ModifiedBy   = currentUser;
    link.ModifiedDate = DateTime.UtcNow;

    dbContext.SaveChanges();
}

9.3 Querying Permissions for a Role

var permissions = dbContext.LinkRolePermissions
    .Where(lrp => lrp.RoleId == someRoleId && !lrp.IsDeleted)
    .Select(lrp => lrp.Permissions)
    .ToList();

10. Best Practices & Considerations

  • ✅ GUID Generation: Client‑side rather than database‑side to ensure idempotence.
  • 🔄 Soft‑Delete Logic: Filter IsDeleted = false in queries to avoid “ghost” links.
  • 🕒 Time Zones: Consider using DateTime.UtcNow for consistency.
  • 🔒 Audit Security: Ensure AddedBy/ModifiedBy is set via a trusted context (e.g., middleware).
  • ⚙️ Fluent API vs DataAnnotations: Mix & Match carefully; DataAnnotations live on the class, Fluent API in OnModelCreating.

11. Conclusion

The LinkRolePermission class is a robust, auditable join entity essential for managing many‑to‑many associations between Roles and Permissions within the IrbyImaging system. By leveraging EF’s DataAnnotations and the soft‑delete pattern, it strikes a balance between relational integrity and auditability. Use the examples and best practices above to integrate, query, and maintain this link entity effectively.


🎉 Happy Coding! 🎉
If you have any questions or need further assistance, feel free to reach out.