Jan 26, 2012

On Code Architecture (.Net, CSharp, WCF and WPF)

Been working on an "educational" project for on of our clients at work. They are migrating to the .Net Framework and CSharp. As an example project that also is useful I (among other co-workers) implemented a nice and simple server-client architecture for an application they need. I'm pretty satisfied with how it turned out so far. The project is almost finished and I've had a easy time tracing and fixing bugs as well as implementing some last minute changes. The architectural concept seems to work well.

Nice thing about such a project is that you get time to have a closer look at the architecture. To have lots of (sometimes heated) discussions with different people about it and to really widen your understanding of different concepts. Also its a good time to experiment with things and try different approaches to find out what works well for you. Furthermore, I learned a lot about some .Net technologies I didn't use extensively before. In my opinion the time I spent digging deeper into these was well worth it. I'm convinced that other projects will greatly benefit from my new/improved knowledge.

The architecture is pretty straight forward and I hope for most nothing new. I've known it for some time but never really implemented it as its intended. Mostly due to lack of time/control or the simplicity of projects I've been working on in the past. This project was the chance to once do things the right way. The architecture is layered and consists of a data layer, business layer and a presentation layer.


Data Layer
This layer is pretty dump. Minimal Logic which handles connecting to a "datasource". On our Server side this is the SQL Database using Entity Framework. On the Client side this is the WCF Webservice. The idea here is to simply exchange this layer with an other one to connect to a different source.

Business Layer
Almost all application logic is located within this layer. It uses the data layer to access the needed information. On the server side it handles transformation of the database objects into POCOs which later can be easily sent over the WCF Webservice to the client. Also, it does data validation and security checks. As our client doesn't do much logic it mostly does data caching and management on that side.

Presentation Layer
This is where others interact with the Server/Client. On the server this means the WCF Webservice. This consists of an Interface as well as the implementation that mostly routes everything into the business layer. On the client side this is a bit more heavy. We use WPF (XAML, Behaviors, Converters, Commands, Data binding aso.) for the actual UI and make heavy use of the MVVM (Model - View - View Model) pattern.

This whole architecture created quite an initial time overhead (which I underestimated). But I think that over time this will pay off. Finding bugs, fixing and extending code is so much easier with this consisted architecture. This will save some time in the future.

Sidenote: We use integration builds and unit test the server business layer. I think with ~70+ tests and a 81,9% statement coverage we do pretty well there. More unit tests would be nice but this covers the most important logic of the application.

There is room for improvement but overall I'm pretty satisfied with our results ...