Search code examples
c#oopdesign-patterns

Which role controls the object creation in an application?


I am designing an application that will create reports based on business rules.

So far there are:

  • TableDataGateway (and subordinate classes) to import data and keep it in ready-to-access form as a DataTable
  • ModelBuilder to map the data onto individual business models
  • IdentityMap to keep track of existing model objects

There will be a WinForms UI which will control the application (to be implemented).

Let there be a use case where the user wants to load specific samples and view their list within the UI.

The question: Which class should control the object creation process (data retrieval, construction and IMap registration)? Should all this be handled by the controller within the Presentation Layer (Forms)? Or should some class within the Business layer receive the message from the Presentation Layer and deal with the request?


Solution

  • IMO the object creation process should be controlled by dependency injection code written in (or used by) the application entry point - if it's a single assembly application you can simply handle all dependency injection code inside the class that has your entry point method (by default, that would be in the Program class).

    With multiple assembly applications (i.e., Micro Services architecture) you might want to create a class dedicated to wrap your IOC container and use it in each assembly's Program class, just to provide a layer of abstraction between your IOC container and your application code. (A lesson learned when switching from one IOC provider to another)