I
Iain
Hey folks,
(I posted this in microsoft.public.dotnet.csharp.general yesterday,
but it appears that this group is rather more lively)
For the application I am developing, I have a data access layer that
handles all the talking to the database. There are only certain
exceptions that I want to excape from this class, and all of them
should have sensible meanings - for instance, there should be a "The
database is not happy" and "There is a bug here, because we got this
wacky exception", but not a IndexException or whatever - that is, I
want to make it very obvious what exceptions can escape from a
provider of the data access layer interface.
Unfortunately, the data access layer is currently at about 20 methods;
I don't want to put an indentical try catch block into each method,
because that would be a daft bit of code duplication. There is
obviously going to be a certain set of exceptions thrown, and any
other exception means bug. That means I want to surround every single
function with something like this:
try
{
//do work
}
except (DataAccessLayerException e)
{
throw;
}
except (Exception e)
{
throw new DataAccessLayerInternalException(typeof(e).toString(),
e);
}
but to put that into every method would be stupid.
I can see the following solutions:
- The post at http://primates.ximian.com/~lluis/blog/archives/000183.html
details some funky interception stuff, which may be a way forward.
However, "Calls may be slow for some applications, since all calls go
through the remoting infrastructure."
- A code generator that creates a wrapper class that provides that bit
of exception magic for each method
What I think I really want, though, is a default exception handler for
a class. Does c# or the framework offer such things?
Is there some approach that I am missing?
(I posted this in microsoft.public.dotnet.csharp.general yesterday,
but it appears that this group is rather more lively)
For the application I am developing, I have a data access layer that
handles all the talking to the database. There are only certain
exceptions that I want to excape from this class, and all of them
should have sensible meanings - for instance, there should be a "The
database is not happy" and "There is a bug here, because we got this
wacky exception", but not a IndexException or whatever - that is, I
want to make it very obvious what exceptions can escape from a
provider of the data access layer interface.
Unfortunately, the data access layer is currently at about 20 methods;
I don't want to put an indentical try catch block into each method,
because that would be a daft bit of code duplication. There is
obviously going to be a certain set of exceptions thrown, and any
other exception means bug. That means I want to surround every single
function with something like this:
try
{
//do work
}
except (DataAccessLayerException e)
{
throw;
}
except (Exception e)
{
throw new DataAccessLayerInternalException(typeof(e).toString(),
e);
}
but to put that into every method would be stupid.
I can see the following solutions:
- The post at http://primates.ximian.com/~lluis/blog/archives/000183.html
details some funky interception stuff, which may be a way forward.
However, "Calls may be slow for some applications, since all calls go
through the remoting infrastructure."
- A code generator that creates a wrapper class that provides that bit
of exception magic for each method
What I think I really want, though, is a default exception handler for
a class. Does c# or the framework offer such things?
Is there some approach that I am missing?