0

I am designing the classes so that each class uses the data access layer to populate itself from the database. All classes would be instantiated empty and would load themselves based on a given Id. The Business Layer Session would simply loop through all classes in the session and call load() on each.

What is the best way to instantiate all 297 classes and have them empty and waiting in the Session? I was looking at IoC, Reflection, and the good ole object factory switch statement but that switch is too massive. I really don't want to hard code the class names to be registered and created within the code...regardless of IoC, reflection, or delegate method registration.

I was kind of thinking of adding an extended property to each table (DomainObject: TypeStringOfObject) since the Domain Classes match one to one against the tables. Then just grab all of those extended property values from any tables that have them. My code would create instances of classes based on the extended properties that are returned. That seems overkill to me. The other thing I was thinking about was creating a class instance for every class in that specific namespace but I think using reflection would be a huge performance hit. I guess an IoC container would be my best option for the second approach.

Any ideas on how to deal with my scenario without having a large switch statement in a factory? Or having to read in the list of 297 classes that need to be created?

Thanks -Ken

Villain
  • 79
  • 9
  • How will the 297 class instances be stored? – Chronicle Jul 18 '18 at 18:17
  • I’m afraid that having to early instantiate 297 objects by user session, just for them to be there, doesn’t look like a really cool design. Also I think we have a “XY problem” situation here, please tell us what was the problem that leaded you to a solution where you have to instantiate all those objects. – Pablo Recalde Jul 18 '18 at 18:29
  • 1
    don't instantiate them up front - instantiate on demand only. provide an API which consumers can ask for an instance, and lazy-initialize it when the first consumer requests it. – Cee McSharpface Jul 18 '18 at 18:32
  • The classes will be stored in memory, loaded with data from DB1 Views, validated, and then each class will perform a persist to move the data to DB2 database where the tables are identical to the objects (view from DB1). – Villain Jul 18 '18 at 18:35
  • 1
    How about defining an interface with a Load() function, and get all the instances of the class implementing the interface, looping through all and call Load? https://stackoverflow.com/questions/26733/getting-all-types-that-implement-an-interface – Y.S Jul 18 '18 at 18:41
  • @Y.S yes, that was my plan for the Load(). I already have a Base DomainModel that implements Load() and Persist() from an Interface. Are you saying, I can take an IoC and have it hand me all the instances for classes implementing the Interface? – Villain Jul 18 '18 at 18:46
  • I think you just described an [ORM](https://en.wikipedia.org/wiki/Object-relational_mapping)... – Mark Seemann Aug 22 '18 at 11:01

0 Answers0