Archive for the '.NET 3.0' Category

23OctBeating a dead horse: Stored Procedures

I seem to be having the same conversations with the dev teams whenever I switch clients. The topic of this post is one that many people have written about before. I’m just going to put my opinion on my blog so I can refer people to it in the future instead of having to repeat myself every time.
What prompted this post is that since I’ve moved to Belgium I’ve had to take a step back from living on the bleeding edge and using open source projects. Most of the work is concentrated in Brussels and is at big corporates or banks not exactly what you’d see as the progressive thinkers (with reason).
I guess it would be safe to say that I’ve been immersed in “enterprise” development. In short I still haven’t seen anything that is more complicated than a web app like [Xero](http://www.xero.com). But perhaps more about that in another post. This one is about stored procedures and their valid uses.

Continue reading ‘Beating a dead horse: Stored Procedures’

01OctCommon mistakes in software development (part 2): Mixing up the tiers

In my [previous post](http://flanders.co.nz/2008/09/24/common-mistakes-in-software-development/) I explained some very quick wins to make your code a little bit cleaner. As I’ve been appointed an [asp.net](http://www.asp.net) project at work at the moment I have the chance to get more ammunition for blogging :) .
This time I’d like to talk about properly separating your tiers so that the next person doesn’t have to go through the complete application and make changes everywhere just to make a minor change to the application.

One of the problems; one of the most time consuming that is; I’ve seen is that people are confused on what they have to put in the data logic layer and what is business logic. In my case this is fairly extreme because there isn’t an ORM tool but rather every entity gets populated by calling a stored procedure and then in the code the graph objects get set. Whether this is a good approach for fetching your data or not is not within the scope of this blog post, but I’m guessing there are more people who are facing this type of situation.

Anyway let’s start with the beginning and explain the typical [n-tier architecture](http://en.wikipedia.org/wiki/N-tier) people seem to follow. This is not a particular pattern like [MVC](http://en.wikipedia.org/wiki/Model-view-controller) or [MVP](http://en.wikipedia.org/wiki/Model-view-presenter) that people are talking about so much lately. This goes back to the guidance that can be found on the [msdn website](http://msdn.microsoft.com). This type of architecture is often used in combination with data sets but not in my example for this post. This architecture is generally divided in 3 parts that can, but don’t have to, run on different machines if needs be. When talking about this type of architecture people mostly talk about an n-tier application.

##The first part is the data layer (tier).
The golden rule for this one: this is the gateway between the rest of your application and the database. **NONE** of the other layers should be talking directly to the database but instead should be doing their talking through this layer. That means if you have stored procedures you provide wrappers for them in this layer. You populate your entities in this layer too.
Other functions you can perform in this layer is setting the graph members (populating relationships). IMHO if you’re talking to the database (open/close connection) you’re doing stuff that belongs in the data layer which includes populating relationships.

Encapsulating this logic in it’s own layer, which could potentially be walled off through only exposing it with remoting or WCF, allows you to reuse the code in different places of your applications or sharing this data access assembly with multiple applications.

##The second part is the business logic layer (tier).
This layer encapsulates all the operations you do on entities to express the business rules. That means you would probably do most of your work in this layer. Basically **all** of the programming you will be doing for the business rules should be done here. Business logic doesn’t live in stored procedures, it doesn’t live in the UI or the data layer. Nope this layer is where it lives and nowhere else. This statement may raise some eyebrows but only and only when you find that a certain routine is a bottleneck and it is really data intensive you can put it in a stored procedure but more on that subject in another blog post.
If you find yourself transforming data so you can display it in your GUI then you’re probably expressing business rules that aren’t explicitly stated as a business rule.
When you find yourself to be concatenating strings or writing logic to translate your pages in your GUI layer then you’re probably expressing business logic (business logic doesn’t have to come from business ;) in case that wasn’t clear).
Another common find in business logic is validation for example because this generally expresses some kind of strict rule that comes from the business domain your application deals with. Validation is a tricky one but the rule is you should do **all** validation in your business logic. To provide a better user experience you can maybe reuse that validation on the client side. In the case of web development you probably will have to duplicate that validation in javascript if you really need it.

Separating these rules into their own layer allows you to reuse the methods and classes you create in the business logic layer, in different parts of your UI or even reuse it in different applications.
By separating this logic it should be easier for you to do some automated testing like unit testing and/or integration testing of that code.

##The third and last layer (tier)
This is typically the UI layer but you could easily use web/WCF services as an interface to your logic. The UI doesn’t have to be a GUI it can also be a CLI (Command-Line Interface) or something. But that is how you interact with the user or external application. The idea is that in this layer you have virtually no logic except for what’s on the screen **everything** else should be handled by your business logic. To clarify this statement: you can show/hide UI elements or add/remove elements to the UI and respond to events triggered by user actions but the data of that response and the processing really belongs in the business logic layer.

The UI layer can talk to both the business logic and data logic layers. If for example you’re getting a category list with just an id and name from the database chances are you won’t need to transform that data so your UI can bind directly to the entities returned by your data layer. But more complex items like an invoice for example will probably need some processing and then it should probably pass through the business logic layer.

This is typically a somewhat harder part of your application to provide tests for although there are some libraries out there that make it easier but still there are easier parts to test in your application.

So that was a quick refresher on what the classic n-tier architecture is about an how it should be structured. I hope you will agree with me into stating that its not that hard and pretty straight-forward to implement, but what I find in the “enterprise” is far from the points mentioned above.
It is a bloody mix of everything everywhere, leaving me thinking -come on guys it’s not that hard-:
*talking to the database => datalayer*
*showing windows/adding UI elements,… => UI layer*
*everything else => business logic*

Failing to abide by the previous simple rules will result in hell freezing over, entire plagues will be released upon the world; to cut a long story short: the world as you know it will seize to exist and turn into complete chaos.
Following the rules should result in less code duplication, an instantaneously easier to maintain codebase and probably more happy successors for when you move on to the next project. It should also give you a higher degree of code reuse.
If there is one thing you should take away from this article then it should be:
**Don’t mix your tiers**

Of course there are a couple of situations when you can diverge from the ideas presented in this post but you should always be able to justify why you break the rule. So you need a good reason to break the proposed architecture and that would probably also warrant a comment so the next guy also knows what’s going on.
The most important part is to separate all non-UI logic out from the UI layer and put it in one of the lower layers.

Thanks for reading
Ivan – writing for more maintainable software -

24SepCommon mistakes in software development

***** Rant Alert ******

<rant>

At my current client I’ve got to do mainly maintenance on existing applications. This gives me the chance to look into codebases that have been created by other people and that don’t really reflect how I would write things. That is all good though it gives me a chance to learn new ways of doing things and when I think their way is better I’ll surely adopt.

Anyway when I’m browsing these codebases I do find a lot of things that could have been done better or more correctly and that’s what I’ll be writing about a little today.

The first one is returning bools:

I’ve found this in just about every project I’ve been in:

public bool IsNull(){
  if(obj == null)
    return true;
  else
    return false;
}

The snippet above is a very long winded way of writing. IMHO this hurts readability and you’re saying the same thing twice. obj == null already returns a bool it makes no sense writing it again.

public bool IsNull() { return obj == null; }

Another thing I keep seeing is very liberal use of try..catch blocks that catch all exceptions. Admittedly try..catch is cool but it should be used at times you are actually interested in the exception that is thrown. But it shouldn’t be used as a safeguard to swallow exceptions you don’t want to fix at this moment.  I keep seeing this code in projects:

try{
  myBLObject.FindSomething(someId).SomeMethod();
}
catch(Exception){
// Nothing to be done but error stops
}

Now that can be easily written so that it won’t throw an exeption and then the try catch isn’t necessary anymore at all. Try..catch blocks most certainly have their use but throwing and catching exceptions definitely hurts performance because the system has to generate a complete stack trace etc. for every exception that is being thrown.

var result = myBLObject.FindSomething(someId);
if(result != null) result.SomeMethod();

The code becomes a lot more readable, not to mention faster. I’ve seen this being used in OnRowDataBound events etc on grids with 500+ rows, removing the try catch blocks more than doubles the speed of that page.

The next one on the list is using if,else and switch statements. They are sometimes a cause of code bloat. To put it in the words of Scott Hanselman:

I think that using only if, for and switch is the Computer Programmer equivalent of using “like” in every sentence.

Scott does a great job explaining why they can be pretty evil so I’ll just leave you with a link to his post

I have another couple of posts in the making on this subject but I had to get this out of my system. These are also very quick wins the other things I’m going to talk about are application architecture and stored procs….
</rant>

07AugIronNails : Rails like development for IronRuby with WPF/Silverlight

For my book IronRuby I’m working on chapter 4. That chapter is about doing WPF development with IronRuby. I started out with a straight port of Witty to IronRuby. As I was doing that the cogs started turning and I came up with a way to bring the rails style of development to WPF.   I decided to investigate that route a little bit further and now I have a small framework that enables you to write WPF applications with the MVC paradigm. I decided to open that code up as open source and host it on github. 

At first I used the name Sails for my framework but it turns out there is java clone of rails that is called opensails. So to avoid confusion David M. Peterson proposed the name IronNails.

On Sat, 02 Aug 2008 08:00:44 -0600, Charles Oliver Nutter wrote:

    FYI, there’s already a framework named "Sails" for Java:
http://www.opensails.org/

For the sake of sticking to the "Iron" theme, why not replace the ‘S’ with an ‘N’ and go with IronNails. ;-)   Maybed it’s just me, but if given the choice, I’d much rather nail it than sail it any day of the week. :D


/M:D

M. David Peterson

You can find the project here: http://github.com/casualjim/ironnails

At this moment it’s definitely not finished at all, but it does work. The remainder of the week I’ll move my previous demo code onto this framework, update the code samples in my chapter and finish the content. I hope I will have all of this done by the end of next week.

Back to the IronNails project:

Because DLR objects cannot be used to bind to in WPF you have to define a skeleton of the ViewModel in C#, but this will change in the future. When that changes I’ll look at extending the framework to make use of some other WPF patterns like defining a DependencyObject and Behaviors. Once those are defined you get a very clean separation between design and behavior.

This is abstracted away from you but in the background the framework works with the View – ViewModel – Model – Controller pattern although I’ve tried to keep your exposure to the view model to a minimum. The framework follows naming conventions per language. So in C# and XAML you camel case stuff and in IronRuby you underscore stuff.

The very core of the framework is defined in C# but most of the code is IronRuby, depending on how hard it will be after the DLR RTM’s I may look at adding support for all the DLR languages.

IronNails
=========

IronNails is a framework inspired by the Rails and rucola frameworks. It offers a rails-like way of developing
applications with IronRuby and Windows Presentation Foundation (WPF).
This framework uses the pattern Model – ViewModel – View – Controller (M-VM-V-C). It should be able to run on both WPF
and Silverlight.
The idea is that the views can be created using a design tool like Blend for example and just save that xaml as is. The
designer should not need to use anything else than drag and drop to create a GUI design. The behaviors are then added to
the view by using predefined behaviors in the framework or by defining your own behavior.
The framework then generates a proxy for the view which will be used to transparently wire up the commands in the
behaviors to controller actions.

You are now able to write the following code for a controller:

class MyController < IronNails::Controller::Base

  view_action :show_message, :triggers => :my_button do
    MessageBox.show "This is the great message from a block"
  end

  view_action :change_color, :triggers => { :element => :my_text_block, :event => :mouse_enter }
  view_action :reset_color, :triggers => { :element => :my_text_block, :event => :mouse_leave } do |view|
    view.my_text_block.foreground = :black.to_brush
  end

  view_object :people, Person.find_all

  def change_color(view)
    view.my_text_block.foreground = :red.to_brush
  end

end

At this moment the project has 0 unit tests, it has below minimal documentation and it still needs a work like defining the behaviors. I have to move on with my book but intend to continue developing this framework after my book is finished and IronRuby RTM’s.  There are some more workarounds in the project that will all disappear as IronRuby progresses.

kick it on DotNetKicks.com

05NovCongratulations to Mindscape

A couple of friends of mine own the company Mindscape

They were interviewed by Ron Jacobs at the latest NZ tech ed in August. And that has now just been put up on channel 9 as part of the ARCast series.

The interview is about their product LightSpeed which IMHO is one of the nicest ORM’s out there. I’ve used it in a couple of things now and I am absolutely surprised by the fact that lightspeed is a good name for it because it is really fast. If you haven’t taken it for a test drive you should do so immediately.

Now everybody rush over there to see their interview.

 

Well done JB, JD and Andrew.

del.icio.us tags: , , ,

03NovI’m getting to write a book !!!!!!!!!

I just got confirmation from Manning publishers that I get to write a book on IronRuby.

I personally think this is great news :D

I will keep you updated with more progress as I go along.

I just wanted to get this message out.

29OctA little browser with ironruby and wpf

Whilst preparing for my talk on saturday I got to play a little with Iron ruby and wpf.

One of my experiments was to create a little browser which i dubbed biffy :)

You can download it here: biffy.zip

 

del.icio.us tags: , ,

22OctRejoining the pack

The last year I’ve dabbled a lot with linux, ruby etc. Investigating alternative means of developing web applications.  I have switched back to vista ultimate x64 now because I spend about 95% of my time developing on windows so it didn’t make sense to run linux as a base system.  Both ubuntu with Beryl or vista are just as slow/fast on my computer the admin time for my windows system is a lot shorter than the one for my ubuntu system.

I’m not taking a hard line against webforms any more, I still think I won’t be using them in complex projects but for quick prototyping the dynamic datacontrols are pretty cool. Which won’t stop me from building my own mvc style architecture because the current defacto standard is suffering from code bloat and a too tight integration of javascript frameworks etc.

This weekend I’ve finally started playing around with c# 3.0 and silverlight and I liked what I saw a lot. 

I’m getting my head around the new technolgies by building a little texas holdem multiplayer game.  So the technologies this project will use are: .net 3.5 with c# 3.0, silverlight 1.1 (with xaml), WCF.

I started building that texas holdem poker game because I couldn’t find a nice one that I could install on my LAN or one that wouldn’t require me to get a mortgage if I would lose a game online.

The microsoft one that comes with vista ultimate is nice and i aim to beat it by making it multiplayer.

The most challenging bit of this poker game is definitely the AI for the computer players because I don’t want them to be idiots but also not the best players in the world seen as i’m not the world’s greatest poker player (I’m still working for a living :) ). If somebody that reads this blog has done some similar work or knows of some resources for me to work through some of that stuff please share them.

So far I’ve got the following logic going:

Evaluating wheter it’s a good hand or a bad hand: simulate about 1000 games with the same hand,same cards on the table and the same number of players.

That gives me an idea of how good the hand is and if it should fold.  Next I want to figure out whether the player should bet, check or raise this is done by looking at the odds and the maximum score I could reach as well as how far along we are in the game.. I have more in my head but not enoughtime to write it all down. I’ll write more on this subject in the course of the next few weeks.

I’m planning on making the multiplayer version open sourced for demo purposes but not the one that contains the AI for the computer players.

 

del.icio.us tags: , , ,

16MayWhat is wrong with people?

Did I miss the memo about having to be religious about the tools you use. In my mind anything that falls in the category religion should be banned as it is a cancer on society. Philosophy is good. Budhism is a philosophy to which I would subscribe for example. I am allergic to people that tell me what to think, probably why I never really fell for the “ruby community”. I don’t need convincing, I’ll do that myself, I just want correct information and plenty of it. 

That being said I stumbled across this thread where people are actively discouraging Miguel de Icaza to implement silverlight and co in Mono

http://linux.slashdot.org/article.pl?sid=07/05/03/2033219

These are the reactions to it on channel 9.

http://channel9.msdn.com/ShowPost.aspx?PostID=305405#305405

 

I personally use both I use Linux and I use vista both on a 64 bit system.  I like the whole simplicity of linux but the applications for it are not nearly as finished as the ones that are there on the windows platform.

For example MonoDevelop doesn’t come anywhere near Visual studio. Gimp is no photoshop, ….

Microsoft provides a more viable eco system for people to make money of what they do. I do wish Microsoft would support open source a bit more instead of copying it and thus mostly killing the OSS project. Although I do understand that they are a business

If I would have more time I’d certainly want to contribute to Mono but unfortunately there are only so many hours in a day and these hours are not enough.

To get back to my original point. You can be passionate about something, and everybody that has spent some time talking to me will have picked up on the fact that I don’t just accept the technologies that are being shoved down our throats these days.

The reason I don’t read blogs on Ruby anymore is the following: I simply couldn’t stomach the people going on and on about how cool their mac is and yet they need to reboot it on a regular basis. My vista pc at work hasn’t rebooted for the last 2 weeks now, before that I got a BSOD regarding memory management :)
Furthermore there are programmers that keep going on about the fact that notepad/vi/emacs/textmate/eclipse are the best tools for developing applications.
I don’t know about you but for me programming is about creating stuff, preferrably fast and visual studio is the perfect fit for it.

Granted my vista pc doesn’t look nearly as cool as a mac or an ubuntu with berryl machine but I am far more productive for my job on it.

So my conclusion for this post would be :
Don’t get religious about your tools/language/platform but choose the best one for the job at hand.

Uhm.. this post turned out to be a big rant instead of my intended post which was going to be about how much I look forward to C# 3.0 and the DLR.

As soon as I can get my hands on some of the bits I’ll be posting my findings.

05DecMicrosoft is listening after all

Well today has been an interesting day.  I signed up for a day of presentations on user experience, which is something that can always improve. Turns out it are a bunch of presentations on Microsoft Expression (I guess in some ways that has to do with user experience )It’s not something I personally will be using as it is more designer focussed and I use visual studio for all my development and web design.

Over the last couple of months or so I have been sensing that Microsoft isn’t what they used to be and I’m not the only one that is inclined to think that way

Mostly because I’m currently working as a sole developer in my own company I feel very much left out by them.

Most of the issues I have is to quickly develop something, where I am in full control of what happens, is not something that is easily done in asp.net.  (For more on reasons why I like castle vs asp.net webforms : Am I too late, A bit further down the castle track

 

Back to the original story
Those presentations didn’t really hold value for me but I got into a conversation with 2 microsoft employees that were there with the goal of finding out why people like me are moving to rails and what microsoft isn’t giving us and they should.

My main points of discomfort would be:

Feeling of not being in control

Complexity to get it to render out pages

Slow development pass (might be ok if you’re a giant company in which people actually hold meetings and have managers but not in my case). I’m all about agile (not to be confused with the TFS agile method) development. Plan, build, ship within 2 months would be average.

Need to know a page lifecycle (which is statefull programming in a stateless environment, need i say more)

Complexity to get things done

No proper sample code available that shows something beyond “Hello world”

Anyway we had this interesting talk about it, now it’s again a case of waiting to see if they can make a change before the new year to keep me interested.

While I’m on the subject. Next week will be the week in which I’ll try to build my first RoR site. I’m pretty confident that it will work out and then i can finally assess myself which one is better for what.

Ok I’m off to buy Agile webdevelopment with ruby now


Recent Flickrs

    Blogroll

    Recent Listening

    Scrobbler