Architecture Issue: Using Dependency Injection Leading to Junk API

I am trying to create a class that performs all kinds of low-level database related activities, but is a really simple interface for the user interface layer.

This class is a collection of data, everything inside a specific aggregate root, obtained by a single int identifier.

The constructor takes four parameters:

public AssetRegister(int caseNumber, ILawbaseAssetRepository lawbaseAssetRepository, IAssetChecklistKctcPartRepository assetChecklistKctcPartRepository, User user)
{
  _caseNumber = caseNumber;
  _lawbaseAssetRepository = lawbaseAssetRepository;
  _assetChecklistKctcPartRepository = assetChecklistKctcPartRepository;
  _user = user;
  LoadChecklists();
}

The user interface layer accesses this class through the interface IAssetRegister. Castle Windsor can independently provide the ILawbaseAssetRepository and IAssetChecklistKctcPartRepository parameters, but the UI code must supply the other two using an anonymous type, for example:

int caseNumber = 1000;
User user = GetUserFromPage();
IAssetRegister assetRegister = Moose.Application.WindsorContainer.Resolve<IAssetRegister>(new { caseNumber, user});

API . , IAssetRegister . , .

, - . - ?

+5
2

, (), ,

, ( ) , IAssetRegister , .

IUserProvider :

public class UserProvider : IUserProvider 
{
    // interface method
    public User GetUser() 
    {
        // you obviously don't want a page dependency here but ok...
        return GetUserFromPage();
    }
}

, , . , .

+2

. , , , - . , :

public class RegisterAssetCommand
{
    [Required]
    public int CaseNumber { get; set; }

    [Required]
    public User Operator { get; set; }
}

-:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

:

var command = new RegisterAssetCommand
{
    CaseNumber = 1000,
    Operator = GetUserFromPage(),
};

var commandHandler = WindsorContainer
    .Resolve<ICommandHandler<RegisterAssetCommand>);

commandHandler.Handle(command);

.. , commandHandler ( ).

, ICommandHandler<RegisterAssetCommand> :

public class RegisterAssetCommandHandler
    : ICommandHandler<RegisterAssetCommand>
{
    private ILawbaseAssetRepository lawbaseAssetRepository;
    private IAssetChecklistKctcPartRepository assetRepository;

    public RegisterAssetCommandHandler(
        ILawbaseAssetRepository lawbaseAssetRepository,
        IAssetChecklistKctcPartRepository assetRepository)
    {
        this.lawbaseAssetRepository = lawbaseAssetRepository;
        this.assetRepository = assetRepository;
    }

    public void Handle(RegisterAssetCommand command)
    {
        // Optionally validate the command

        // Execute the command
    }
}

, User RegisterAssetCommand, IUserProvider RegisterAssetCommandHandler. IUserProvider GetUserForCurrentContext, .

, .

+3

All Articles