You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using your IQueryHandler pattern with good results. Command and query handlers are basically my data access layer and I would like to always use a query handler when querying the database for something. Even if it is for simply looking up an object by its primary key. I have however been unable to get rid of the use of repositories for one use case: looking up a domain object by its primary key.
I can of course create a specific class that implements IQueryHandler for each domain class I want to be able to look up by primary key, but that seems unnecessary since I can do it in a generic way using a repository.
This is the generic repository I would like to replace:
To replace this I defined the following query class:
public class EntityByPrimaryKeyQuery<T> : IQuery<T> where T : class
{
public EntityByPrimaryKeyQuery(int id)
{
Id = id;
}
public int Id { get; set; }
}
When I do this, Simple Injector gives me the following error:
The constructor of type MyService contains the parameter with name 'queryHandler' and type IQueryHandler<EntityByPrimaryKeyQuery<MyDomainClass>, MyDomainClass> that is not registered. Please ensure IQueryHandler<EntityByPrimaryKeyQuery<MyDomainClass>, MyDomainClass> is registered, or change the constructor of MyService . Note that there exists a registration for a different type IQueryHandler<TQuery, TResult> while the requested type is IQueryHandler`2[[EntityByPrimaryKeyQuery<EntityByPrimaryKeyQuery<MyDomainClass>,MyDomainClass>.'
Is what I am trying to achieve possible? If so, what am I doing wrong?
The text was updated successfully, but these errors were encountered:
Has the following behavior (copied from the API documentation):
Registers all concrete, non-generic, public and internal types in the given set of assemblies that implement the given openGenericServiceType
In other words, this method only registers non-generic types, but EntityByPrimaryKeyQueryHandler<T> is generic.
There are multiple ways around this, such as using the Register overload that takes in an IEnumerable<Type> will using Container.GetTypesToRegister to retrieve the types, while supplying a TypesToRegisterOptions with IncludeGenericTypeDefinitions set to true.
The easiest solution, however, is to add the following registration to your configuration:
I'm using your IQueryHandler pattern with good results. Command and query handlers are basically my data access layer and I would like to always use a query handler when querying the database for something. Even if it is for simply looking up an object by its primary key. I have however been unable to get rid of the use of repositories for one use case: looking up a domain object by its primary key.
I can of course create a specific class that implements IQueryHandler for each domain class I want to be able to look up by primary key, but that seems unnecessary since I can do it in a generic way using a repository.
This is the generic repository I would like to replace:
To replace this I defined the following query class:
I then defined the following query handler class:
I use Simple Injector and I normally register all my IQueryHandler implementations with one line:
I then try to inject an instance of the IQueryHandler in my service MyService for a specific domain class like this:
When I do this, Simple Injector gives me the following error:
Is what I am trying to achieve possible? If so, what am I doing wrong?
The text was updated successfully, but these errors were encountered: