I have read this stackoverflow topic:
I understand there is a risk of the compiler not understanding one of the types in ternary operator. However, my both classes inherit the same base class.
This is what I currently have:
public async Task Handle(IClientPendingIntegrationEvent message) {
Command<Unit> command = _requestContext.SourceType == RequestSourceType.Backoffice
? new CreateManualUserEventCommand(message.UserId, eventType, UserEventDomain.Registration, message.Reason)
: new CreateUserEventCommand(message.UserId, eventType, UserEventOriginType.System, UserEventDomain.Registration, message.Reason);
await _mediator.Send(command);
}
public class CreateManualUserEventCommand : Command<Unit>
{
public CreateManualUserEventCommand(
string userId,
UserEventType userEventType,
UserEventDomain domain,
string comment = null)
{
UserId = userId;
Type = userEventType;
Domain = domain;
Comment = comment;
}
public string UserId { get; private set; }
public UserEventType Type { get; private set; }
public UserEventDomain Domain { get; private set; }
public string Comment { get; private set; }
}
public class CreateUserEventCommand : Command<Unit>
{
public CreateUserEventCommand(
string userId,
UserEventType type,
UserEventOriginType createdByOrigin,
UserEventDomain domain,
string comment = null)
{
UserId = userId;
Type = type;
CreatedByOrigin = createdByOrigin;
Domain = domain;
Comment = comment;
}
public string UserId { get; }
public UserEventType Type { get; }
public UserEventOriginType CreatedByOrigin { get; }
public UserEventDomain Domain { get; }
public string Comment { get; }
}
This is the exact error the compiler throws.
Error CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between
'MFG.Auditing.Application.Features.Backoffice.UserEvents.Commands.CreateManualUserEvent.CreateManualUserEventCommand' and 'MFG.Auditing.Application.Features.System.UserEvents.Commands.CreateUserEvent.CreateUserEventCommand'
I understand that I can easily rework this with if and else but I need to use ternary operator here.