S
Shapper
Hello,
I am creating a simple service layer. I have the following:
public abstract class Query { } // Query
public abstract class Reply {
public Exception Exception { get; set; }
} // Reply
When a query is handled it can return or not a reply. So I have:
public interface IQueryHandler<TQuery> : IDisposable where TQuery : Query {
void Handle(TQuery query);
} // IQueryHandler
public interface IQueryHandler<TQuery, TReply> : IDisposable where TQuery : Query where TReply : Reply {
Reply Handle(TQuery query);
} // IQueryHandler
public abstract class RequestHandler<TQuery> : IQueryHandler<TQuery> where TQuery : Query {
public override void Handle(Query query) {
TQuery tquery = (TQuery) query;
Handle(tquery);
} // Handle
public abstract void Handle(TQuery query);
} // RequestHandler
public abstract class RequestHandler<TQuery, TReply> : IQueryHandler<TQuery, TReply> where TQuery : Query where TReply : Reply, new() {
public override Reply Handle(Query query) {
TQuery tquery = (TQuery)query;
Reply treply = Handle(tquery);
return treply;
} // Handle
public abstract Reply Handle(TQuery query);
} // RequestHandler
So I would like to use it as follows:
IDispatcher dispatcher = new Dispatcher();
HelloQuery query = new HelloQuery { Message = "Hello, how are you?" };
HelloReply reply = dispatcher.Send<HelloReply>(query);
So when I send a HelloQuery its handler would run some code.
Inside the handler I need to catch any exception that occurs and return in it the Reply.
Of course it is also possible to have a handler that returns no reply.
How can I do this? I am blocked on the Handler and Dispatcher part.
I am creating a simple service layer. I have the following:
public abstract class Query { } // Query
public abstract class Reply {
public Exception Exception { get; set; }
} // Reply
When a query is handled it can return or not a reply. So I have:
public interface IQueryHandler<TQuery> : IDisposable where TQuery : Query {
void Handle(TQuery query);
} // IQueryHandler
public interface IQueryHandler<TQuery, TReply> : IDisposable where TQuery : Query where TReply : Reply {
Reply Handle(TQuery query);
} // IQueryHandler
public abstract class RequestHandler<TQuery> : IQueryHandler<TQuery> where TQuery : Query {
public override void Handle(Query query) {
TQuery tquery = (TQuery) query;
Handle(tquery);
} // Handle
public abstract void Handle(TQuery query);
} // RequestHandler
public abstract class RequestHandler<TQuery, TReply> : IQueryHandler<TQuery, TReply> where TQuery : Query where TReply : Reply, new() {
public override Reply Handle(Query query) {
TQuery tquery = (TQuery)query;
Reply treply = Handle(tquery);
return treply;
} // Handle
public abstract Reply Handle(TQuery query);
} // RequestHandler
So I would like to use it as follows:
IDispatcher dispatcher = new Dispatcher();
HelloQuery query = new HelloQuery { Message = "Hello, how are you?" };
HelloReply reply = dispatcher.Send<HelloReply>(query);
So when I send a HelloQuery its handler would run some code.
Inside the handler I need to catch any exception that occurs and return in it the Reply.
Of course it is also possible to have a handler that returns no reply.
How can I do this? I am blocked on the Handler and Dispatcher part.