Archive

Archive for the ‘.NET’ Category

Alternatives to Throwing Exceptions to Flex/Silverlight Clients From a .NET Web Service – part 1

January 22nd, 2009 joelhainley No comments

I’ve been doing a lot of work with webservices talking to Flex clients recently. When I first started doing this work I spent some time trying to figure out how to get exceptions thrown to be picked up by the client and dealt with there. I read through the literature on doing such things, and even spent some time evaluating some third party libraries for making all of this happen.

Recently I was at a .NET users group meeting and heard some people talking about throwing exceptions to Silverlight clients in much the same way that I had been looking at doing for my Flex clients. The problems that they encountered and the solutions that they came up with were essentially the same. “IIS does dumb stuff with the response codes when there’s an exception in a webservice”. (There’s a pretty simple work around for this one ) “You can trap the exception and then make the exception object a property of the service response object”, “There’s a third party product X, that wil…blah blah blah”. It was nice to see that noone had come up with an elegant solution to this “problem” of throwing exceptions over the wire.

I started to wonder if it really was a “problem”. I took a step back and did some critical thinking about such an approach. I had a lot of questions about what I, and the rest of the people out there are trying to accomplish by throwing exceptions “over the wire”.

  • What benefits do you get from handling server exceptions in the UI?
  • Is it really correct to handle exceptions in the UI for problems that occurred in the web service?
  • What are the valid uses for exceptions?
  • What is the definition of an exception?
  • What are people trying to do with these exceptions?/ What problems are they trying to solve by the use of exceptions?

In an effort to clear up my thinking on such things I’m going to go through some of the above questions below.

What is the definition of an exception?

The tongue in cheek response to this question is “it’s when something exceptional happens”. The longer explanation is that when your code encounters something that that is outside the scope of what it was intended to handle. I think there’s a clue in there that addresses the problem I’m trying to deal with, but we’ll have to get back to it after we talk about a few more of the questions.

What are the valid uses for exceptions?

Answering this question is difficult without having a problem domain to work with. The answer can vary depending upon the environment that your code is expected to run in, how reliable various parts of the environment are, how critical certain operations within your application are, etc..etc.. What I would like to say about this is that like we said above your exceptions should handle exceptional situations.

I also think that there is often too little thought put into the implementation of exceptions within an application. There’s a pretty good argument floating around that your exception model should be diagrammed and managed in the same way that your business objects are. I might talk about this at a later date, but for now give it some thought.

Is it really correct to handle exceptions in the UI for problems that occurred in the web service?

In a windows application that is self contained, handling exceptions in the form code  seems to be something that a lot of people are comfortable with (  judging by the codebases I’ve had to maintain ). In fact most books that I run across that show beginning development have the exception handling directly in the form itself. This is arguably correct, if not desirable, for quick and dirty applications, because you’re really just trying to get something done. Besides you don’t want to throw the actual exception out to the user to look at so you catch the exception and give the user a nice “Something really bad happened, try again” message and exit the application.

Now in a situation where you have a client application talking to a server application. You have a different scenario. The server is providing services to the client, if something bad happens in the server it should handle the problem in some way, (i.e. capturing the exception ) and then notify the client that there has been an error.

In the same way that you would never want to throw raw exceptions from your self contained application out to your users. You really don’t want to throw raw exceptions to the clients that use your server API. If there was a problem with your server code handle it gracefully in your server code and provide your clients with a decent error message.

There should be a separation between your server code and your client code, when you start to throw exceptions from your server out to your clients  you begin to tie them together in ways that are not extensible. Consider if you start using a new library in your server code that throws a different type of exception that you felt should be bubbled out to your client. This means that all of your clients will need to be updated to handle the new exception instead of handling a “call failed” response sort of message. Encapsulation is a metaphor for good design at all magnification levels of developement not just at the object/class level.

There is more I could say here but I’m going to move on for now. I might come back and talk some more about this in another posting.

What are people trying to do with these exceptions?/ What problems are they trying to solve by the use of exceptions?

From what I’ve observed most people are trying to ease their burden on the server side by throwing exceptions out to the clients when there are problems. The most common example of this is input validation, or even a little deeper would be business object validation (i.e. Does this user have the right to add another user to this account ) Call me crazy but I don’t see how you can call a user entering incorrect values to be an exceptional situation. At least with my users it never is. If there is a wrong value that can be typed, they are going to type it. I don’t start throwing exceptions and stepping out of the program flow to deal with these errors because they are not exceptional circumstances.  I have a really hard time believing that validation of input is a good place to start throwing exceptions. Yes, the system should reject bad input, or illegal operations but do you really need to start throwing exceptions over the wire to deal with this? Is it necessary to tightly couple your UI code base to your server codebase just to get the proejct done and avoid duplication of validation efforts?

I really don’t think so.

In the next part of this article I am going to provide some diagrams, patterns and maybe even a little sample code to show how you might avoid “exceptions over the wire”

Adventures in ORM on .NET 3.5

December 26th, 2008 joelhainley No comments

I’ve been working on a project in .NET 3.5  and  Adobe Flex 3 and have been having a good time getting the base architecture in place. I’ve spent the last couple of weeks looking at two ORM technologies available in .NET. NHibernate and LINQ for Entities. 

I started started out working with NHibernate because I was familiar with Hibernate in the java world. Well actually, I was familiar with the Repository from ATG which is pretty similar to Hibernate. (My general impression is that ATG’s Repository is a bit more polished than Hibernate) NHibernate worked out great and very quickly I was able to get the database access up and running and had a working prototype. Once this was done, I did some more digging to see what other ORM implementations were available for .NET.  There are quite a few projects out there but I came across LINQ, and specifically LINQ for Entities.

LINQ could probably best be described as a generalized approach at data access from various datasources that is intended to replace all of the customized query languages/approaches ( SQL, XPath, iteration through various collection classes, etc ) with a single language. Ostensibly this will reduce the amount of stuff a developer needs to know to be effective. According to the available documentation LINQ for Entities is the most customizable implementation of ORM functionality available in LINQ. There are some other ways to do similar things with a little less control using LINQ to SQL but I decided to just dig deep as the sort of control I was used to with Hibernate was most similar to LINQ for Entities.

I had to upgrade Visual Studio 2008 to sp1 to use LINQ for Entities, but this was trivial and I soon had things up and running. In short order I had everything working with LINQ and the application was moving forward again. At this point I’m mostly impressed with LINQ and I think I’ll end up using it for this project. There are a bunch of tools available in the IDE to support the mapping operations, and it will autogenerate initial classes by simply pointing the IDE at the database you are using.

I’m not sure if LINQ is the be all end all that microsoft is hoping it will be. I’m not convinced that a generalized query language is needed. It has been said, by several philosophers and more than a couple of programmers, that the language that you have available to express yourself has an impact on your thinking. In this case specialized languages such as XPath and SQL give your mind a set number of ways to express yourself that are consistent with the underlying implementation. Yet a generalized approach abstracts that away and removes you another step farther away from the realities of the toolset you are using. Is this a good thing? Time will tell and at the very least it will be interesting to see how far people can take this approach to querying various datasets. 

The thing I am convinced about is that ORM technology is making it much easier on developers to quickly and easily get database code up and running. The increases in productivity are real. It is nice to interact with the data as objects instead of as sql queries, stored procedures, etc. Somehow I suspect that the Smalltalk crowd is sitting in the wings chuckling at all of the other technology platforms touting their ORM solutions.

Categories: .NET Tags: , , , ,

A quick note about .NET Web Services, Adobe Flex and Namespaces

November 1st, 2008 joelhainley No comments

I was building out some .NET based web services for a Flex project I’ve been working on and bumped up against something that gave me a bit of trouble. When you create a .NET Web Service there is a namespace attribute associated with the service. Here’s an example (in c#) :

namespace HelloWebService
{
[WebService(Namespace = "http://www.examplenamespace.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]

public class Service1 : System.Web.Services.WebService
{

// implementation …

}

This value is required by .NET for a web service, or at the very least .NET gets VERY upset if this value is not set.

You will then need to let Flex know about this namespace in order to access the results. To do this you simply add the following lines to Flex:

private namespace lh = “http://www.examplenamespace.com/”;
use namespace lh;

Not much to it, but it can be a bit of a pain when you’re not aware of this. Flex doesn’t complain, it just gives you empty values when you try to access the values.