Closed keerthirajap closed 4 years ago
Can you please clarify what you're trying to achieve here?
Default interface implementations only apply if the implementing class does not implement the methods. In a way, they are meant to provide compatibility for implementing classes if the interface changes, especially when interface owner has no control over the implementations.
In Insight's case, auto-implementing an interface means implementing the methods, in which case the default implementation will not be invoked, as per C# specification. It seems to me that your usage would suit better to an extension method, and not a default implementation.
For example:
public interface IBeerRepository : IDbConnection
{
public IDbConnection GetConnection();
[Sql("SELECT * FROM Beer")]
Task<IList<Beer>> GetBeerByType();
}
public static class BeerRepositoryExtensions
{
public static Task<IList<Beer>> GetBeerByTypeDefault(this IBeerRepository repo)
{
return repo.GetConnection().QueryAsync<Beer>("SELECT * FROM Beer");
}
}
Also, please note that you're calling the wrong Insight method. If you want to pass SQL to the server (and not a stored proc name), then you should use the API method that ends with Sql
:
Task<IList<Beer>> GetBeerByTypeDefault()
{
return GetConnection().QueryAsync<Beer>("SELECT * FROM Beer"); //Throws error
}
Should be:
return GetConnection().QueryAsyncSql<Beer>("SELECT * FROM Beer"); //Throws error
Still getting same error after changing to QueryAsyncSql
public interface IBeerRepository : IDbConnection
{
public IDbConnection GetConnection();
[Sql("SELECT * FROM Beer")]
Task<IList<Beer>> GetBeerByType(); // This works perfect
Task<IList<Beer>> GetBeerByTypeDefault()
{
return GetConnection().QuerySqlAsync<Beer>("SELECT * FROM Beer"); //Throws error
}
}
InvalidOperationException: The stored procedure 'GetBeerByTypeDefault' doesn't exist
InvalidOperationException: The stored procedure 'GetBeerByTypeDefault' doesn't exist.
Insight.Database.Providers.Default.SqlParameterHelper.DeriveParameters(SqlCommand cmdToPopulate)
Insight.Database.SqlInsightDbProvider.DeriveParametersFromStoredProcedure(IDbCommand command)
Insight.Database.Providers.InsightDbProvider+<>c__DisplayClass12_0.
I was able to filter out non-abstract methods on interfaces when the code generator runs.
So now if you want to implement methods on interfaces, Insight won't override them.
Fixed in next build.
Please provide support for executing code inside Default interface methods in c# 8 along with Auto Interface Implementation. When I tried it executing SQL inside interface method throw error.
Asp.Net Core 3.0 + MVC, Insight.Database 6.2.10
Steps to reproduce
IBeerRepository.cs
Beer.cs
HomeController.cs
SQL Create table :
CREATE TABLE Beer ([ID] [int], [Type] varchar(128))