How to create a Business Layer object with Injection Dependency?

My goal is to simplify my infrastructure as much as possible from my application (although I know that this is not entirely possible).

I have several different common types of models that I use in my applications ... mappers, data objects, and value objects. For example, UserMapper takes a UserData object and collects information from the database, and then maps it to a UserValue object for use in the controller.

This means that these models have the following dependencies:

  • UserMapper: need UserData and a way to create UserValue (s)
  • UserData: Db required (from framework)
  • UserValue: nothing needed

Do I include in my DIC methods to create Mapper objects, Data and Value objects so that dependencies can be automatically entered? Or am I creating a separate DIC / Factory for processing Business Layer data?

+4
source share
1 answer

A dependency injection container can handle the creation of all objects. This includes your DIC using factories to create specific objects that use the factory pattern.

I like loading DICs with closures that create objects. This way all objects are lazy loaded only on demand, but I can still have a lot of flexibility when creating objects.

You can write so that your datamapper gets an empty uservalue object and then initializes the object data based on the database data. An alternative is to create a tight connection between your data classes and service classes, as one creates the other on its own.

Trying to separate your framework from your business logic should not mean that it does not allow two to touch, just that the business logic code is not included in your infrastructure code. Using DIC to create objects that are used for business logic does not mean that your DIC has business logic.

0
source

Source: https://habr.com/ru/post/1394500/


All Articles