Vitor Durante
Vitor Durante
CC#
Created by Vitor Durante on 5/3/2023 in #help
❔ How to cast to a generic of object?
I am using an external api that returns dynamic data. It could be a string, a list or a table. In my code, I am building a controller endpoint that will read the data from this API, parse the data from the body, and return as a result to the requester. To help me with that, I have attempted to build a generic ApiResult<T> that will contain the parsed data. The issue is that I am attempting to cast the ApiResult<T> to an ApiResult<object> and unfortunately it doesn't allow me to do that since object and T doesn't seem to be compatible. Could anyone help me? I don't need the result to be strongly typed, but I really wanted to have the Parse methods strongly typed to have intellisense.
public class ApiResult<T> where T : class
{
public bool Success { get; set; }
public T Data { get; set; }
}

public class ApiResult : ApiResult<object>
{
}

public static class HttpResponseExtensions
{
public static ApiResult ParseApiResult(this HttpResponse response)
{
// I cannot do this because ApiResult<object> is not compatible with ApiResult<T>
return response.Headers["Structure"] switch
{
"text" => response.ParseString(),
"list" => response.ParseList(),
"table" => response.ParseTable(),
_ => null
};
}

private static ApiResult<List<string>> ParseList(this HttpResponse response)
{
return new ApiResult<List<string>>();
}

private static ApiResult<string> ParseString(this HttpResponse response)
{
return new ApiResult<string>();
}

private static ApiResult<List<List<string>>> ParseTable(this HttpResponse response)
{
return new ApiResult<List<List<string>>>();
}
}
public class ApiResult<T> where T : class
{
public bool Success { get; set; }
public T Data { get; set; }
}

public class ApiResult : ApiResult<object>
{
}

public static class HttpResponseExtensions
{
public static ApiResult ParseApiResult(this HttpResponse response)
{
// I cannot do this because ApiResult<object> is not compatible with ApiResult<T>
return response.Headers["Structure"] switch
{
"text" => response.ParseString(),
"list" => response.ParseList(),
"table" => response.ParseTable(),
_ => null
};
}

private static ApiResult<List<string>> ParseList(this HttpResponse response)
{
return new ApiResult<List<string>>();
}

private static ApiResult<string> ParseString(this HttpResponse response)
{
return new ApiResult<string>();
}

private static ApiResult<List<List<string>>> ParseTable(this HttpResponse response)
{
return new ApiResult<List<List<string>>>();
}
}
25 replies
CC#
Created by Vitor Durante on 4/20/2023 in #help
❔ How to optimize a EF Core query with too many joins?
I have a specific endpoint that basically does 12 different joins. It is a central piece of the code that has to filter many different joined tables. I am using split query to prevent Cartesian Explosion from happening. All tables are perfectly indexed. The queries are super fast, but since we have 12 of them when splitting, it ends up taking 2 to 3 seconds to return the result. I was wondering if anyone has ever faced this issue and if anyone has any suggestion on a approach to fix this issue. A few ideas I had: - Some of these joins are not many to many, so I was thinking about a view that would pre-join these tables to reduce the number of joins - this would definitely work, but honestly would implement a drastic change on my database and query structure. I was trying to avoid it. - I was wondering if there was a way to tell Split Query to only split many to many joins, since some of those are many to 1 and wouldn't cause a cartesian explosion. This way I could maybe reduce the number of queries to half and have it run twice as fast. - Run separate queries entirely and pass ids around on subsequent filters - I honestly find it hard to believe this would have any benefit. I would have to load a lot of IDs in memory and then build other big queries with them to incrementally get to the result I need. Thanks!
3 replies
CC#
Created by Vitor Durante on 11/9/2022 in #help
❔ dotnet build quiet still showing a lot of warnings
running dotnet build --verbosity quiet is still showing a lot of warnings. Any suggestions on how to hide those and only show errors?
5 replies