Skip to content
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

Open
guimar86 opened this issue May 4, 2024 · 1 comment
Open

Interfaces with Generics #143

guimar86 opened this issue May 4, 2024 · 1 comment

Comments

@guimar86
Copy link

guimar86 commented May 4, 2024

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.

image

See the error message below:

image

@Arnab-Developer
Copy link

Arnab-Developer commented Dec 8, 2024

@guimar86 you have registered ICosmosDbRepository<CosmosDocumentBase> in the DI container. But you have injected ICosmosDbRepository<Document> in the AddCommand method. The generic type parameters (CosmosDocumentBase and Document) are not matching. This is the reason you are getting the error.

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants