Single table inheritance is a method to implement object-oriented inheritance in a relational database. It is a design pattern used in object-oriented programming to map inheritance hierarchies onto a single database table.
In C#, this pattern can be implemented using various techniques, such as Entity Framework Core for ORM (Object-Relational Mapping) scenarios.
Following is a basic example of how you might implement Single Table Inheritance in C# using Entity Framework Core:
Suppose you have a base class Vehicle, and two derived classes Car and Bike. You want to store instances of both Car and Bike in a single database table called Vehicles.
using Microsoft.EntityFrameworkCore;
public class MyDbContext : DbContext
{
public DbSet Vehicles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Configure your database connection here
optionsBuilder.UseSqlServer("your_connection_string");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure the inheritance mapping
modelBuilder.Entity()
.HasDiscriminator("vehicle_type")
.HasValue("car")
.HasValue("bike");
}
}
public class Vehicle
{
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Common properties for all vehicles
}
public class Car : Vehicle
{
public int NumberOfDoors { get; set; }
// Additional properties specific to cars
}
public class Bike : Vehicle
{
public bool HasBasket { get; set; }
// Additional properties specific to bikes
}
In the above example, Vehicle is the base class with common properties shared by all vehicles. Car and Bike are derived classes with additional properties specific to cars and bikes, respectively.
Entity Framework Core is used to map these classes to a single table Vehicles. The discriminator column vehicle_type is used to differentiate between the types of vehicles.
Now, when you look at the list, you can easily tell what each item is. If you need to do something specific with cars, you find items with the “car” tag, and if you need to do something with bikes, you find items with the “bike” tag.
When you query the Vehicles table, Entity Framework Core will automatically populate instances of the correct derived class based on the value of the discriminator column.
Advantages of using Single Table Inheritance(STI) :-
- STI simplifies the database schema by reducing the number of tables needed to represent a class hierarchy.Instead of creating separate tables for each subclass, all data can be stored in a single table.
- With STI, querying data becomes more efficient because you don’t need to perform joins across multiple tables to fetch related data.
- Having all related classes stored in one table can make the database schema easier to understand and maintain.
- STI allows you to add new subclasses to your hierarchy without modifying the database schema.
Drawbacks of using Single Table Inheritance(STI) :-
- STI can lead to redundancy in the database, especially if subclasses have many attributes that are not applicable to other subclasses. This can result in wasted storage space and potentially slower query performance.
- As the hierarchy of classes grows, managing a single table with many columns representing different attributes for different subclasses can become complex and difficult to maintain.