JacksonT
JacksonT

Reputation: 37

How do I automate dbset operations in my application?

I have a project with a large number of entities and a project that can expand further in the future. At this point, I want to automate my dbset operations. In this case, what kind of generator should I create? Or is automating these operations the right approach?

namespace HospitalManagement.Persistence.Context
{
    public class HospitalManagementDbContext : IdentityDbContext<AppUser, AppRole, string>
    {
        public HospitalManagementDbContext(DbContextOptions options) : base(options)
        {
        }
      #region Common
        public DbSet<Address> Addresses { get; set; }
        public DbSet<AnyParam> AnyParams { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<County> Counties { get; set; }
        public DbSet<DbParameter> DbParameters { get; set; }
        public DbSet<DbParameterType> DbParameterTypes { get; set; }
        public DbSet<Error> Errors { get; set; }
        public DbSet<Domain.Entities.Common.File> Files { get; set; }
        public DbSet<Hospital> Hospitals { get; set; }
        public DbSet<ItemType> ItemTypes { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<Status> Statuses { get; set; }
        #endregion
 #region Management
        public DbSet<Announcement> Announcements { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<ItemChange> ItemChanges { get; set; }
        public DbSet<OperationLog> OperationLogs { get; set; }
        public DbSet<PdfTemplate> PdfTemplates { get; set; }
        public DbSet<ServiceLog> ServiceLogs { get; set; }
        #endregion
        #region Medical
        public DbSet<Medicine> Medicines { get; set; }
        public DbSet<MedicineDetail> MedicineDetails { get; set; }
        public DbSet<Prescription> Prescriptions { get; set; }
        public DbSet<Treatment> Treatments { get; set; }
        public DbSet<TreatmentPlan> TreatmentPlans { get; set; }
        #endregion
    #region Users
        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<Patient> Patients { get; set; }
        public DbSet<Staff> Staffs { get; set; }
        public DbSet<Visitor> Visitors { get; set; }
        #endregion
      }
}

Upvotes: 0

Views: 37

Answers (1)

Jalpa Panchal
Jalpa Panchal

Reputation: 12749

You could try using the reflection to automatically register all DbSet properties in your DbContext.

For that first you have to Create a base DbContext class that uses reflection to register DbSet properties:

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Reflection;

namespace HospitalManagement.Persistence.Context
{
    public class BaseDbContext : IdentityDbContext<AppUser, AppRole, string>
    {
        public BaseDbContext(DbContextOptions options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            RegisterAllEntities(modelBuilder, typeof(IEntity));
        }

        private void RegisterAllEntities(ModelBuilder modelBuilder, Type interfaceType)
        {
            var types = Assembly.GetAssembly(typeof(HospitalManagementDbContext))
                .GetTypes()
                .Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces().Contains(interfaceType));

            foreach (var type in types)
            {
                modelBuilder.Entity(type);
            }
        }

        public DbSet<T> Set<T>() where T : class
        {
            return base.Set<T>();
        }
    }
}

Define the IEntity interface and ensure all your entities implement it:

namespace HospitalManagement.Domain.Entities
{
    public interface IEntity
    {
        // Common properties can be defined here if needed
    }

    public class Address : IEntity { /* ... */ }
    public class AnyParam : IEntity { /* ... */ }
    // Add IEntity to all your entities
}

Inherit your DbContext from the base DbContext:

namespace HospitalManagement.Persistence.Context
{
    public class HospitalManagementDbContext : BaseDbContext
    {
        public HospitalManagementDbContext(DbContextOptions options) : base(options)
        {
        }

        #region Common
        public DbSet<Address> Addresses { get; set; }
        public DbSet<AnyParam> AnyParams { get; set; }
        public DbSet<City> Cities { get; set; }
        public DbSet<Country> Countries { get; set; }
        public DbSet<County> Counties { get; set; }
        public DbSet<DbParameter> DbParameters { get; set; }
        public DbSet<DbParameterType> DbParameterTypes { get; set; }
        public DbSet<Error> Errors { get; set; }
        public DbSet<Domain.Entities.Common.File> Files { get; set; }
        public DbSet<Hospital> Hospitals { get; set; }
        public DbSet<ItemType> ItemTypes { get; set; }
        public DbSet<Room> Rooms { get; set; }
        public DbSet<Status> Statuses { get; set; }
        #endregion
        #region Management
        public DbSet<Announcement> Announcements { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<ItemChange> ItemChanges { get; set; }
        public DbSet<OperationLog> OperationLogs { get; set; }
        public DbSet<PdfTemplate> PdfTemplates { get; set; }
        public DbSet<ServiceLog> ServiceLogs { get; set; }
        #endregion
        #region Medical
        public DbSet<Medicine> Medicines { get; set; }
        public DbSet<MedicineDetail> MedicineDetails { get; set; }
        public DbSet<Prescription> Prescriptions { get; set; }
        public DbSet<Treatment> Treatments { get; set; }
        public DbSet<TreatmentPlan> TreatmentPlans { get; set; }
        #endregion
        #region Users
        public DbSet<Doctor> Doctors { get; set; }
        public DbSet<Patient> Patients { get; set; }
        public DbSet<Staff> Staffs { get; set; }
        public DbSet<Visitor> Visitors { get; set; }
        #endregion
    }
}

Make sure all your entities inherit from the IEntity interface:

namespace HospitalManagement.Domain.Entities.Common
{
    public class File : IEntity { /* ... */ }
}

namespace HospitalManagement.Domain.Entities
{
    public class Address : IEntity { /* ... */ }
    public class AnyParam : IEntity { /* ... */ }
    // Implement IEntity in all your entities
}

Upvotes: 0

Related Questions