-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interfaces with Generics #143
Comments
@guimar86 you have registered You need to design this differently to solve this problem. You can take a look at the below code. using Cocona;
using Microsoft.Extensions.DependencyInjection;
var builder = CoconaApp.CreateBuilder();
builder.Services.AddTransient<ICosmosRepository<BaseDocument>, BaseDocumentRepository>();
builder.Services.AddTransient<ICosmosRepository<Document>, DocumentRepository>();
var app = builder.Build();
app.AddCommand("base-doc", async (ICosmosRepository<BaseDocument> repository) =>
{
var doc = await repository.GetAsync();
Console.WriteLine(doc.Id);
Console.WriteLine(doc.Name);
});
app.AddCommand("doc", async (ICosmosRepository<Document> repository) =>
{
var doc = await repository.GetAsync();
Console.WriteLine(doc.Id);
Console.WriteLine(doc.Name);
Console.WriteLine(doc.Type);
});
await app.RunAsync();
internal record BaseDocument(int Id, string Name);
internal record Document(int Id, string Name, string Type) : BaseDocument(Id, Name);
internal interface ICosmosRepository<T>
{
public Task<T> GetAsync();
}
internal interface IBaseDocumentRepository : ICosmosRepository<BaseDocument>;
internal class BaseDocumentRepository : IBaseDocumentRepository
{
public async Task<BaseDocument> GetAsync()
{
var baseDocument = new BaseDocument(1, "doc1");
var task = await Task.FromResult(baseDocument);
return task;
}
}
internal interface IDocumentRepository : ICosmosRepository<Document>;
internal class DocumentRepository : IDocumentRepository
{
public async Task<Document> GetAsync()
{
var document = new Document(1, "doc1", "type1");
var task = await Task.FromResult(document);
return task;
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When an interface that uses generics is passed as dependency injection in a command it fails on the call as it requests the interfaces as if it is a parameter. Whereas with other interfaces this does not happen at all.
See the error message below:
The text was updated successfully, but these errors were encountered: