Dependency Injection when using the Command Pattern

陌路散爱 提交于 2019-12-12 09:33:34

问题


I'm using the Command Pattern for the first time. I'm a little unsure how I should handle dependencies.

In the code below, we dispatch a CreateProductCommand which is then queued to be executed at a later time. The command encapsulates all the information it needs to execute.

In this case it is likely we will need to access a data store of some type to create the product. My question is, how do I inject this dependency into the command so that it can execute?

public interface ICommand {
    void Execute();
}

public class CreateProductCommand : ICommand {
    private string productName;

    public CreateProductCommand(string productName) {
        this.ProductName = productName;
    }

    public void Execute() {
        // save product
    }
}

public class Dispatcher {
    public void Dispatch<TCommand>(TCommand command) where TCommand : ICommand {
        // save command to queue
    }
}

public class CommandInvoker {
    public void Run() {

        // get queue

        while (true) {
            var command = queue.Dequeue<ICommand>();
            command.Execute();
            Thread.Sleep(10000);
        }
    }
}

public class Client {
    public void CreateProduct(string productName) {
        var command = new CreateProductCommand(productName);
        var dispatcher = new Dispatcher();
        dispatcher.Dispatch(command);
    }
}

Many thanks
Ben


回答1:


After looking at your code I would recommend not using the command pattern, but instead using command data objects and a command handler:

public interface ICommand { }

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

public class CreateProductCommand : ICommand { }

public class CreateProductCommandHandler : ICommandHandler<CreateProductCommand> {
    public void Handle(CreateProductCommand command) {

    }
}

This scenario is more suitable for cases where CreateProductCommand might need to cross application boundaries. Also, you can have an instance of CreateProductCommand resolved by a DI container with all dependencies configured. The dispatcher, or 'message bus' would invoke the handler when it receives the command.

Take a look here for some background info.



来源:https://stackoverflow.com/questions/6617976/dependency-injection-when-using-the-command-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!