C
C#17mo ago
rcnespoli

❔ How can I create a command handler factory, with this structure that I have

// Comand Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command);
}

// Command Interface
public interface ICommand
{
public string MyProperty { get; set; }

}

// MyCommand class
public class MyCommand : ICommand
{
public public string MyProperty { get; set; }
}

// MyCommand handle
public class MyCommandHandler : ICommandHandler<MyCommand>
{
public async Task<CommandResult> Execute(MyCommand command)
{

}
}

// Command Factory
public class CommandHandlerFactory
{
private readonly ICommandHandler<MyCommand> _myCommand;

public CommandHandlerFactory(ICommandHandler<MyCommand> myCommand)
{
_myCommand = myCommand;
}

public ICommandHandler<?> Build(MyEnum myEnum)
{
var builder = new Dictionary<MyEnum, ICommandHandler<?>>
{
{ LineOfBusiMyEnumessEnum.MyEnumValue, _myCommand }
};
return builder[myEnum];
}
}
// Comand Handler interface
public interface ICommandHandler<in T> where T : ICommand
{
Task<CommandResult> Execute(T command);
}

// Command Interface
public interface ICommand
{
public string MyProperty { get; set; }

}

// MyCommand class
public class MyCommand : ICommand
{
public public string MyProperty { get; set; }
}

// MyCommand handle
public class MyCommandHandler : ICommandHandler<MyCommand>
{
public async Task<CommandResult> Execute(MyCommand command)
{

}
}

// Command Factory
public class CommandHandlerFactory
{
private readonly ICommandHandler<MyCommand> _myCommand;

public CommandHandlerFactory(ICommandHandler<MyCommand> myCommand)
{
_myCommand = myCommand;
}

public ICommandHandler<?> Build(MyEnum myEnum)
{
var builder = new Dictionary<MyEnum, ICommandHandler<?>>
{
{ LineOfBusiMyEnumessEnum.MyEnumValue, _myCommand }
};
return builder[myEnum];
}
}
I'm stuck with the build's return method on CommandHandlerFactory
3 Replies
Denis
Denis17mo ago
What in the world are you trying to do 🤔
rcnespoli
rcnespoliOP17mo ago
I want a class that create commandHandler by MyEnum values
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server