I have this kind of requirement as there are around 100+ model classes.
I have repository pattern implemented for database operations:
public class interface IRepository<TEntity> : IRepository<TEntity>where TEntity : class
It has a method Task Add(TEntity entity);
Under normal circumstances, I would just do this if I want to add UserClass model to DB
public class UserClass { string Name }
UserClass userClassObj = new UserClass { Name = "John" }
await Repository <UserClass>.Add(userClassObj);
However, my requirement is to pass pass class type dynamically based on condition as i have around 100+ models.
if (condition == "1")
classtype = USerClass
else if (condition == "2")
classtype = DepartmentClass
else if (condition == "3")
classtype = CategoryClass
and then pass classtype to the repository
await Repository <classtype>.Add(object of (classtype ));
Is this doable? Any working example will be highly appreciated. Could not find straight forward implementation so far.