C
C#12mo ago
Drunkreaper

Is it possible to create a dependency injection with interface and an abstract(base) class?

So im building a controller that uses IPersonFactory<T> to connect to PersonFactory<T> which is a base class that depending on the generic type sends the request to several different sub-factories which ultimately end in respective repositories. Rn I'm downcasting _factories to different generic types of IPersonFactory<Person> to do the type change. Since this gives a build error would it be better to make fields for all different types of IPersonFactory and make injections for them?
No description
No description
No description
8 Replies
Tarcisio
Tarcisio12mo ago
PersonFactory<T> can not be abstract otherwise how is it supposed to be instantiated
Drunkreaper
DrunkreaperOP12mo ago
thought so... will make fields for all the types of T and instantiate them instead, TY for confirming
Tarcisio
Tarcisio12mo ago
What are you trying to solve here and what was your approach
Drunkreaper
DrunkreaperOP12mo ago
Its a school app that communicates with several tables
No description
Drunkreaper
DrunkreaperOP12mo ago
this screen is responsible for the addition of info into all the people related tables there are several other controllers with get methods and etc but the idea was to be able to add, remove and update the people tables from this single controller (all the factories have different number for Priority) i suppose this might be done with reflection instead of switches but not too sure how to do it
Angius
Angius12mo ago
Are you just using the name "factory" as a stand-in for a generic repository? Because it looks an awful lot like a generic repository usage. Also, are you really storing each type of a person in a separate table? Why not use a role-based system and have a single table for all people?
Drunkreaper
DrunkreaperOP12mo ago
there are also repositories where the Db connection is actually made pretty much its controllers - Interfaces - factories - repositories idk if it's too many classes i was advised by a friend to make the DB connection code harder to see the factories dont rly create anything as their usual purpose would be they just call the repositories the idea was to have different tables connected with FK between each other, for instance the Students are connected to Parents and the Grades tables any recommendations would be appreciated since im still stumbling my way around APIs
Angius
Angius12mo ago
If the factories just call repositories, it soulds like they're completely useless

Did you find this page helpful?