HuntressMain
HuntressMain

Reputation: 197

How to correctly use generics and type constraints in this scenario?

I am confused on generics and type constraints, I will get straight to the point.

I have a BaseQueryResult class

public abstract class BaseQueryResult<T>
{
    public int Count => Models != null && Models.Any() ? Models.Count : 0;

    public Exception Exception { get; set; }

    public bool HasException => Exception != null;

    public bool IsSuccess => Exception == null;

    public bool NotFound { get; set; }

    public string ContinuationToken { get; set; }

    public IList<T> Models { get; set; }
}

A child class which inherits from the above

public class TransportListingQueryResult : BaseQueryResult<TransportListingQueryModel>
{
    public TransportListingQueryResult(IList<TransportListingQueryModel> models, string continuationToken)
    {
        Models = models;
        ContinuationToken = continuationToken;
        NotFound = false;
    }

    public TransportListingQueryResult(Exception exception)
    {
        NotFound = false;
        Exception = exception;
    }

    public TransportListingQueryResult(bool notFound, Exception exception)
    {
        NotFound = notFound;
        Exception = exception;
    }

    public static TransportListingQueryResult NotFoundResult(Exception exception)
    {
        return new TransportListingQueryResult(true, exception);
    }
}

My Extension method that I am using

public static class TransportListingQueryResultExtension
{
    public static IActionResult ToActionResult<T>(this T result, ControllerBase controller, int limit, string routeName, object values, HttpStatusCode successStatusCode = HttpStatusCode.OK)
        where T : BaseQueryResult<T>
    {
        if (result.NotFound)
        {
            return controller.NotFound();
        }

        if (!result.IsSuccess)
        {
            if (result.HasException)
            {
                throw result.Exception;
            }

            return controller.BadRequest(new ErrorResponse { Messages = new[] { ErrorMessages.InternalServer } });
        }

        var uri = controller.Url.Link(routeName, values);
        var response = new HyperMediaResponse<T>(
            new LinkItem(uri),
            new PageItem(limit, result.ContinuationToken, result.Count),
            result.Models);

        switch (successStatusCode)
        {
            case HttpStatusCode.Created:
                return controller.Created(string.Empty, response);
            case HttpStatusCode.OK:
            default:
                return controller.Ok(response);
        }
    }
}

And Finally my Action in my Controller

public async Task<IActionResult> Create([FromBody]CreateListingModel createListing)
    {
        var result = await _transportListingStore.CreateNewAsync(createListing);
        return result.ToActionResult<TransportListingQueryResult>(this, 1, Constants.RouteNames.CreateListing, null, HttpStatusCode.Created);
    }

I am getting an error on this line in my action return result.ToActionResult ( this, 1, Constants.RouteNames.CreateListing, null, HttpStatusCode.Created );

The error is:

The type 'TransportListings.API.Application.Response.TransportListingQueryResult' cannot be used as type parameter 'T' in the generic type or method 'TransportListingQueryResultExtension.ToActionResult(T, ControllerBase, int, string, object, HttpStatusCode)'. There is no implicit reference conversion from 'TransportListings.API.Application.Response.TransportListingQueryResult' to 'TransportListings.API.Application.Response.BaseQueryResult'

I am confused as my child class inherits BaseQueryResult, but the error is telling me there is no implicit reference conversion. I am not too sure what is wrong with my code and why its giving me the error it is.

Any help would be appreciated. Thanks in advance.

Upvotes: 2

Views: 82

Answers (1)

DavidG
DavidG

Reputation: 118977

The problem is your constraint is impossible to satisfy:

public static IActionResult ToActionResult<T>(this T result, /* snip */)
    where T : BaseQueryResult<T>

Where what you really want is something like:

public static IActionResult ToActionResult<T, U>(this T result, /* snip */)
    where T : BaseQueryResult<U>

And call it like this:

return result.ToActionResult<TransportListingQueryResult, TransportListingQueryModel>(
    this, /* snip */);

Upvotes: 1

Related Questions