Simple Service Layer. Please, need some help.

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.
 
A

Arne Vajhøj

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.

Some classes are missing.

And some of the classes shown does not compile.

And it is not clear what you want to do.

Difficult to help with!

Arne
 
S

Shapper

Some classes are missing.



And some of the classes shown does not compile.



And it is not clear what you want to do.



Difficult to help with!



Arne

Sorry Arne. Let me try to explain it better.

The way I intend to use it is the following:

IDispatcher dispatcher = new Dispatcher();
HelloQuery query = new HelloQuery { Question = "Hello, how are you?" };
HelloReply reply = dispacther.Send<HelloReply>(query);

These classes are defined as follows:

public class HelloQuery() : Query {
public String Question { get; set; }
}

public class HelloReply() : Reply {
public String Answer { get; set; }
}

public class HelloHandler : RequestHandler<HelloQuery, HelloReply> {

public HelloHandler() {
return new HelloReply { Answer = "I am fine. How are you" };
} // HelloHandler

}

Note that a Query without Reply would be:

public class HelloHandler : RequestHandler<HelloQuery>

So when I do this:

HelloReply reply = dispacther.Send<HelloReply>(query);

Inside the dispatcher I need to get the Handler that handles a Query of type HelloQuery which is the one I am passing.

And I need to catch exceptions (to log) that occurs on the handle. If any exception occurs than fill the Exception property in the Reply (When a Replyexists).

Maybe I could have a QueryExecutor class, used by the dispatcher, for this.

My code, is at the moment, the following (Look at the comments on the Dispatcher):

using System;

public abstract class Query { } // Query

public abstract class Reply {
public Exception Exception { get; set; }
} // Reply

public interface IQueryHandler<TQuery> : IDisposable where TQuery : Query {
void Handle(TQuery query);
} // IQueryHandler

// 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

public interface IDispatcher {

void Send(Query query);
TReply Send<TReply>(Query query) where TReply : Reply;

} // IDispatcher

public class Dispatcher : IDispatcher {

public void Send(Query query) {

// Get Handler for the Query Type that is passed (Maybe using IoC):
// Execute Handle Method of handler.
// Any exception that might occurs should be catch.

}

public TReply Send<TReply>(Query query) where TReply : Reply {

// Get Handler for the Query Type that is passed (Maybe using IoC):
// Execute Handle Method of handler.
// Any exception that might occurs should be catch and added to Exception property of the Reply.
// Return Reply
return null; // So it compiles at the moment

}

Does this make sense?

Thank You,
Miguel
 
A

Arne Vajhøj

Sorry Arne. Let me try to explain it better.

The way I intend to use it is the following:

IDispatcher dispatcher = new Dispatcher();
HelloQuery query = new HelloQuery { Question = "Hello, how are you?" };
HelloReply reply = dispacther.Send<HelloReply>(query);

These classes are defined as follows:

public class HelloQuery() : Query {
public String Question { get; set; }
}

public class HelloReply() : Reply {
public String Answer { get; set; }
}

public class HelloHandler : RequestHandler<HelloQuery, HelloReply> {

public HelloHandler() {
return new HelloReply { Answer = "I am fine. How are you" };
} // HelloHandler

}

Note that a Query without Reply would be:

public class HelloHandler : RequestHandler<HelloQuery>

So when I do this:

HelloReply reply = dispacther.Send<HelloReply>(query);

Inside the dispatcher I need to get the Handler that handles a Query of type HelloQuery which is the one I am passing.

And I need to catch exceptions (to log) that occurs on the handle. If any exception occurs than fill the Exception property in the Reply (When a Reply exists).

Maybe I could have a QueryExecutor class, used by the dispatcher, for this.

My code, is at the moment, the following (Look at the comments on the Dispatcher):

using System;

public abstract class Query { } // Query

public abstract class Reply {
public Exception Exception { get; set; }
} // Reply

public interface IQueryHandler<TQuery> : IDisposable where TQuery : Query {
void Handle(TQuery query);
} // IQueryHandler

// 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

public interface IDispatcher {

void Send(Query query);
TReply Send<TReply>(Query query) where TReply : Reply;

} // IDispatcher

public class Dispatcher : IDispatcher {

public void Send(Query query) {

// Get Handler for the Query Type that is passed (Maybe using IoC):
// Execute Handle Method of handler.
// Any exception that might occurs should be catch.

}

public TReply Send<TReply>(Query query) where TReply : Reply {

// Get Handler for the Query Type that is passed (Maybe using IoC):
// Execute Handle Method of handler.
// Any exception that might occurs should be catch and added to Exception property of the Reply.
// Return Reply
return null; // So it compiles at the moment

}

Does this make sense?

The code does still not compile as shown.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top