© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•5mo ago•
11 replies
KevS

Generic IH Handlers?

I have a json polymorphic type and I depending on where I'm using it, I want to get a different result type. I have two implementations, the top one works but the bottom one seems cleaner but doesn't work. Are generic handlers possible or do I need to stick to method A here

// THIS ONE WORKS
[Handler]
[AutoConstructor]
public sealed partial class LoadPolymorphObject
{
    private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;

    public sealed record Request(int pk, Type targetType);

    private async ValueTask<TObject> HandleAsync(Request request, CancellationToken ct)
    {
        ApplicationDbContext db = await _dbContextFactory.CreateDbContextAsync(ct);
        
        ObjectBase? obj = await db.Table.FindAsync(
            request.pk,
            cancellationToken: ct
        );

        return obj.GetType() == request.targetType
            ? obj
            : throw new InvalidCastException();
    }
}


// THIS ONE DOESNT 
[Handler]
[AutoConstructor]
public sealed partial class LoadPolymorphObject(ApplicationDbContext db)
{
    private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
    public sealed record Request(int pk);

    private async ValueTask<TObject> HandleAsync<TObject>(Request request, CancellationToken ct) where TObject : ObjectBase
    {
        ApplicationDbContext db = await _dbContextFactory.CreateDbContextAsync(ct);
        
        FoundObject? obj = await db.Table.FindAsync(request.pk, cancellationToken: ct
        );

        return obj is TObject ? obj : throw new InvalidCastException();
    }
}
// THIS ONE WORKS
[Handler]
[AutoConstructor]
public sealed partial class LoadPolymorphObject
{
    private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;

    public sealed record Request(int pk, Type targetType);

    private async ValueTask<TObject> HandleAsync(Request request, CancellationToken ct)
    {
        ApplicationDbContext db = await _dbContextFactory.CreateDbContextAsync(ct);
        
        ObjectBase? obj = await db.Table.FindAsync(
            request.pk,
            cancellationToken: ct
        );

        return obj.GetType() == request.targetType
            ? obj
            : throw new InvalidCastException();
    }
}


// THIS ONE DOESNT 
[Handler]
[AutoConstructor]
public sealed partial class LoadPolymorphObject(ApplicationDbContext db)
{
    private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
    public sealed record Request(int pk);

    private async ValueTask<TObject> HandleAsync<TObject>(Request request, CancellationToken ct) where TObject : ObjectBase
    {
        ApplicationDbContext db = await _dbContextFactory.CreateDbContextAsync(ct);
        
        FoundObject? obj = await db.Table.FindAsync(request.pk, cancellationToken: ct
        );

        return obj is TObject ? obj : throw new InvalidCastException();
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ generic class in generic method
C#CC# / help
3y ago
Generic Interfaces
C#CC# / help
3y ago
✅ generic mess
C#CC# / help
3y ago
Reuse handlers with MediatR?
C#CC# / help
14mo ago