Author - Manik Malhotra Post Views - 133 views
Table Inheritance

C# – Class Table Inheritance

Class Table Inheritance (CTI) is a design pattern used in object-oriented programming to model a hierarchy of related classes where each class is mapped to its own database table. This pattern is commonly used in Object-Relational Mapping (ORM) frameworks like Entity Framework in C# to represent inheritance hierarchies in a relational database.

In this pattern, each class corresponds to a table in a database, and inheritance relationships between classes mirror the relationships between tables in the database. This allows objects of different classes to persist in separate tables while still maintaining a clear inheritance structure. This allows to mapping of object-oriented inheritance concepts to a relational database structure.

It is often used when dealing with scenarios like “is-a” relationships, where a subclass is a specialization of a superclass. It is often used when having a complex inheritance structure in the object-oriented model and wanting to map it to a relational database.

To explain the Class Table Inheritance pattern in C# with an example, let’s consider a scenario involving a university’s employee hierarchy:

  1. Base Class: Create a base class that represents the common attributes shared by all employees:
    public class Employee
    {
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public decimal Salary { get; set; }
    }
  2. Subclasses: Create subclasses that represent different types of employees, each with its own set of additional properties. For example, we have two subclasses: `Faculty` and `Staff`.
    public class Faculty: Employee
    {
    public string Department { get; set; }
    public string Rank { get; set; }
    }
    public class Staff: Employee
    {
    public string Department { get; set; }
    public string JobTitle { get; set; }
    }
  3. Database Mapping: In Class Table Inheritance, each subclass maps to its own database table. In this scenario, we have three tables: `Employee`, `Faculty`, and `Staff`. The `Employee` table contains the common attributes, while the other tables contain the specific attributes for each type of employee.
  4. ORM Mapping: While using an ORM framework like Entity Framework, attributes or fluent APIs can be used to configure the mapping between the C# classes and database tables as follows using the Entity Framework’s fluent API:
    public class AppDbContext : DbContext
    {
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Faculty> Faculties { get; set; }
    public DbSet<Staff> StaffMembers { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    modelBuilder.Entity<Employee>().ToTable("Employees");
    modelBuilder.Entity<Faculty>().ToTable("Faculties");
    modelBuilder.Entity<Staff>().ToTable("StaffMembers");
    }
    }

Now the Class Table Inheritance pattern is used to manage different types of employees in the application. When querying the database, the ORM would handle the joins and give instances of the appropriate C# classes, maintaining the inheritance structure. For example:

using (var context = new AppDbContext())
{
var facultyMembers = context.Faculties.ToList();
var staffMembers = context.StaffMembers.ToList();
// This would have the facultyMembers and staffMembers as separate lists of employees.
}

This example demonstrates how to use Class Table Inheritance to model a university’s employee hierarchy in a relational database using C# and Entity Framework. Each type of employee is represented by a separate table in the database, and we can query and manipulate them according to their specific properties while still maintaining a common base class for shared attributes. It allows a clear inheritance structure in the code to work with objects in a more object-oriented manner and also minimizes data redundancy.

This way, when persisting these objects in a database, each class corresponds to a separate database table (hence the name “Class Table Inheritance”).

Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a .NET Certification Exams from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri