0

Can anyone help me what am i doing wrong with the following c# code? I am basically trying to add the constraint to ICommandHandler.

public interface ICommand<T> where T : BaseDto{ }

public abstract class BaseCommand<T> : ICommand<T> where T : BaseDto { }

public class CreateAlertCommand : BaseCommand<AlertDto>{}   

public interface ICommandHandler<TCommand> where TCommand : ICommand{}

I am getting error when defining ICommandHandler. It says 'Using the generic type ICommand<T> requires 1 type argument'

5
  • 3
    You don't have an ICommand type. You have an ICommand<T> type. If you want a non-generic ICommand type, you have to create one. Commented Mar 26, 2018 at 15:10
  • Thank you. It really helps me to understand. Commented Mar 26, 2018 at 15:15
  • Just a small and unrelated note, class name, "CreateAlertCommand" should be just "AlertCommand" as class name should be noun and not verbs. Commented Mar 26, 2018 at 15:26
  • @Yogee Classes can represent verbs in which case the naming is correct. This seems appropriate in this case as the ...Command classes represent commands, which are necessarily verbs. Commented Mar 26, 2018 at 15:34
  • @dai , you are right. If "CreateAlert" is a command, the name is correct. Commented Mar 27, 2018 at 15:42

1 Answer 1

4
public interface ICommandHandler<TCommand>
    where TCommand : ICommand
{
}

Should be:

public interface ICommandHandler<TCommand,TDto>
    where TCommand : ICommand<TDto>
    where TDto : BaseDto
{
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This is awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.