user9200027
user9200027

Reputation:

Injecting objects into each other

The typical architecture I am using consists of Manager objects in the Business Layer. I am using DI/IOC within .NET Core/ .NET Standard. The managers are injected into the service layer and consequently the services are injected into our API controllers. So I am presently working in a Manager class. I now need a method that resides in another manager class. Usually I return back through the service layer to the controller, then call the next service and then the manager through that.

I am wondering whether it is OK to just inject a Manager I require directly into the Manager I am working in. Therefore cutting out the trip back to the controller and then back up through the other service to the other manager. Basically I have 2 * Managers.

public class TypeCodeManager : ITypeCodeManager
{
    public TypeCodeManager()
    {
    }

    public async Task<int> GetTypeCodeAsync(string typeCode, string code)
}

public class UserManager : IUserManager
{
     private readonly ITypeCodeManager _typeCodeManager;

     public UserManager(ITypeCodeManager typeCodeManager)
     {
        _typeCodeManager = typeCodeManager
     }
}

Is this generally a good idea?

Upvotes: 0

Views: 223

Answers (1)

emagers
emagers

Reputation: 911

I would say it's generally not a good idea to cross over into other domains through the "managers", assuming that your managers are what talks to the persistence layer. This will quickly lead to confusing dependency maps and code.

Your services are a much better layer to orchestrate the cross-domain concerns as they may describe cross domain workflows and depend on multiple managers.

Upvotes: 2

Related Questions