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

Dealing with generic commands/ handlers #242

Closed
thiagomajesk opened this issue Mar 3, 2018 · 1 comment
Closed

Dealing with generic commands/ handlers #242

thiagomajesk opened this issue Mar 3, 2018 · 1 comment

Comments

@thiagomajesk
Copy link

thiagomajesk commented Mar 3, 2018

Hi! I'm trying to do something along the lines of:

Generic commands:

public class CreateCommand<T> : IRequest<bool> where T : Entity, new() 
{
  
}

public class DeleteCommand<T> : IRequest<bool> where T : Entity, new() 
{
    public int Id { get; set; }
}

Generic handler

public class CreateCommandHandler<T> : IRequestHandler<CreateCommand<T>, bool> where T: Entity, new()
{
    public Task<bool> Handle(CreateCommand<T> request, CancellationToken cancellationToken)
    {
        /* Works with T and produces return value */
    }
}

Specific command:

public class CreateFooCommand : CreateCommand<Foo>
{
    public string Name { get; set;}
}

Today I'm getting:

InvalidOperationException: Handler was not found for request of type. Register your handlers with the container. See the samples in GitHub for examples.

Basically what I want is to have basic operations to work using only one generic handler for each type (CRUD). That way I could just describe the commands for each entity instead of writing a handler too.

PS.: I'm using AutoFac and I'm aware it could be a limitation, although I have searched and found this solution (Related: #96; #69).

@thiagomajesk
Copy link
Author

thiagomajesk commented Mar 5, 2018

Update: I finally had more time to dig into it and I came with the following solution:

Generic Handlers/ Commands + AutoFac configuration:
https://gist.github.com/thiagomajesk/9abfb649e58847289b2f65d1994636f2

PS.: I'm doing all the AutoFac registration manually since it's just easier to be explicit for each custom configuration than implement a custom scanner for generic cases as seen here (for StructureMap).

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

1 participant