Archives

Archives / 2008
  • Isn't it boring to write crosscutting concerns?

    To make sure we have clean code and make it easy for us to maintain, we should try to be lazy and reuse code as much as possible, we should also avoid redundant code. But when it comes to crosscutting concerns, we have to be redundant, or? What code is mostly redundant in our code? I will assume that code to check security and logging exceptions are the most redundant code we write. A method should only do one thing, but we can't skip crosscutting concerns, we need it. So basically several method will do more than one thing. Wouldn't it be much better if we only focus on the core concern and skip the crosscutting concerns when we write our code? How can we skip the crosscutting concerns but still has it? The answer is Aspect Oriented Programming (AOP). The PAG team has a Policy Injection Application BLock (PIAB) which we can use to intercept our crosscutting concerns into our code, we also have Spring.Net and some other frameworks. By using AOP frameworks we don't need to write the same code for logging exception etc over and over again, we will also have a much cleaner code, and our focus will only be the core concern. Oh, I feel so lazy, but why should I spend time to write code that do the same thing over and over again, it's so boring, isn't it :P Are you tired of writing crosscutting concerns over and over again, and do you use AOP today?

  • Returning IQueryable<T>

    Since LINQ was added to the .Net Community as a new wonderful new player, more and more solutions I have seen returns the IQueryable<T> from the Data Access Layer. One reason is to easy create different kind of queries in an easy way and execute them. Some developers use the IQueryable<T> interface to create a light weight interface for the Data Access Layer. It's an interesting solution, but at the moment I don't like it.

  • Controllers in the MVC pattern is not a replacement for Business logic layer

    I have seen several demos and examples on presentations where the Controllers are more or less used as a Business logic layer and based on the definition of the MVC Pattern, that is not the correct way of using the Controller. The Controller in the MVC is not a replacement of the Business Logic Layer. The Model in the MVC is the Business Logic Layer. The Controllers purpose is to be a contract between the View and the Model, and handle user actions. Think of the Controller like the Web Form's Code-behind, the logic you put in the code-behind can sort of be added to the Controller, the benefit you get by using MVC over Web Forms is that you can now with the MVC write unit test to test "complex" presentation layer code, and it's not easy to write test against the code-behind because it's event driven.

  • What to do if computers didn't exists?

    If there wasn't any computers in the world, what should I do right now? It's not an easy question to answer. When I was about 6 years old, I was dreaming about being a farmer, have three kids at the age of 20. At the age of 20, I was working with a Business 2 Business solution, no kids and almost single. I don't think I would like to be a farmer now, maybe some kind of a designer.

  • Why doesn't some of you write unit test?

    I have educated over 1000 developers the last 1 1/2 year and about 5% of them raise their hands when I ask them if they writes test, like unit test or if they use Test Driven Development (TDD). 5%! That is not much. When I asked them why they didn't write any tests, the answer is often, we should but don't have time to do it, some answer "we have test but no one maintain the test and update them".

  • Using Web Services in a 3-tier architecture

    During the age of the Windows DNA, most of us developers used COM/COM+ and a 3-tier architecture. Where we separate concerns by placing the views to present data into a layer called PL (Presentation Layer), we add all business logic into one single layer called BLL (Business Logic Layer), all code that handle data access was also placed into its own layer called DAL (Data Access Layer). During this time when Windows DNA was a hot topic, most application used this architecture.


    3tier


    By using DCOM it was easy to distribute the different components in the layers into distinct tiers. DCOM had its advantage and also disadvantage (DCOM hides the "distribution" and distribution could be done after application was written without the developers awareness, most applications wasn't designed for distribution purpose.). There was few application I was involved with that used DCOM, and I will in this article focus on the application that didn't use DCOM. The 3-tier architecture was well defined and common used. Several applications today used this architecture. When .Net arrived several developers was scratching their heads, it was a whole new platform for most of them and a difficult and confused time began, "how should this 3-tier architecture be applied on the .Net platform?". Web Services was introduced somewhere around this time and that was a bright light for some solution architects and developers, now they understand how to apply the 3-tier architecture on the .Net platform, the answer was to replace COM/COM+ components with Web Services. So instead of using a binary standard for the communication, XML and HTTP was used.

  • Bring LINQ to SQL down

    There is a discussion on the net about LINQ to SQL and if it should be removed in the future. My collogue and friend Patrik Löwendahl wrote a post about what he think, you can read about it here. I don't care if LINQ to SQL will be removed, honestly I want it to be removed. As Patrik wrote in his blog post, it's not the ADO.Net Team that created the LINQ to SQL. I think the team that focus on data access etc should be the team that build data access framework and the ADO.Net Team are doing a great job. After watching a session in the PDC about EF in the future, I must say that I will not use nHibernate or other OR-mapper as I have mention before in some old posts, I will now use EF. If we compare LINQ to SQL today with the current version of EF, LINQ to SQL is best suited for RAD, but in the future even EF will be suited for RAD. What I don't like with LINQ to SQL, is that developers are using the database first approached, they generate a model out from a database schema. A database is not object oriented.

  • Is it important to write good code?

    The last three weeks I have visit several companies and talked about writing good code. It's amazing to see how different developer thinks about writing good code. Here are some comments when I asked if it's important to write good code:

  • Region is an excuse for hiding large files

    Often when I take a look at sample code there are a lot of regions. I need to open them all the time, it really start to make me crazy. Why does people even use Region in their code, what's the point of hiding code? I think it's only an excuse to hide code because of a too large file. What do you think?

  • Played around with Microsoft Extensibility Framework - MEF

    I played around with the June CTP of the Microsoft Extensibility Framework (MEF). I wanted to try both Dependency Injection (DI) and adding classes during runtime to extend an application with more logic without shutting down the main app. As an example I created a LogManager, I thought of the most simple possible thing to do. The LogManager will use different kind of loggers, to log exceptions. During runtime I wanted to add more loggers. For example lets pretend we are building an app where we log all exceptions into a database, suddenly we need to log it into a XML file also. If we don't use a provider based solution, we would probably open our application, add the new code to log into an XML file, recompile and deploy. But what with the MEF we can add the extra logger without recompiling the app, we don't even need to shut it down. (Well, this nothing new, this have bean possible for ages with framework like Spring).

    I will start writing about my Loggers before I go into the LogManager and how to add "components" to the MEF frameworks containers. Here is a common interfaced used by the Loggers:

  • Map Objects to one Object

    In my previous post I wrote about how we could use Dynamic Compilation for mapping object to object in a configuration file, mosly for fun to simply try something different. I spend a few minutes to see if I could do something more with it. It ended up that I can use it to several things, like creating Factories, or send in different kind of objects and map several into one. Here is how I for example can send in more domain object to map them into a PresentationCustomer:

  • How to map a Domain model to a Presentation Model, can this be something?

    If you have read my previous post about Presentation Model, I decided to try to see if I could make the mapping between the Presentation Model and the Domain Model simple by creating an Object to Object Mapper. I don’t want to use a messy XML file or attributes on the classes to handle the configuration of how object should be mapped to each other. Instead I wanted to use the nice Object Initializer feature in C# 3.0 which I think is easy to read and easy to use. Here is a modified version of my Mapping method I had in my CustomersController class in my previous post:

  • Who's to blame, Microsoft and/or the .Net Community

    I’m interested in why most of Microsoft and some other developer’s demons about writing apps mostly begin with the creation of a database? The design of the app will more or less in that case be driven by the database schema, after the database in place the Data Access Layer will be built and the entities will mostly reflecting the database schema. A data base like Microsoft SQL Server is a relation database, so our model will more or less be a relational model instead of a more object oriented model. That can make it harder to use good design patterns and principles to solve some Implementation problems. OO has a rich toolset for modeling the domain than the relation model.

  • Avoid returning "null" and use the Null Object pattern?

    When I build applications and add methods to return a list of objects, I make it robust. So I always return an instance of the list instead or returning null. The reason is because I like the use of Count and also use foreach. I don't want to add extra code to see if the method returns null. For example:

  • Avoid "else" as much as possible, use "?:" instead

    To have a 10 week old baby Chihuahua takes a lot of time… the housekeeping is not so easy. I need to go up twice at night to let the little baby boy do his stuff. But it’s really wonderful to have such a small dog in my home, I bought a lot of books about how to be a pack leader.. I love reading books about leadership etc so it should be interesting to read those books about packs. Here is a video on YouTube where I play with him, if anyone is interested to see a dog that has the same size as a cat puppy. http://www.youtube.com/watch?v=KOWZjHTBiKo

  • Lazy Developers?

    When I was out with my new dog (I Chihuahua, wonderful dog), I was thinking about Defensive Programming and Design by Contract. I like to do some refactoring and help other people to write cleaner code etc,  not that I’m an expert in the area but I think most of developers can see stuff that blinds other developers during development. I remember a scenario that made me kind of mad, I often notice that some developers doesn’t even care to validate arguments on public API or other methods, they assume the caller pass in the right values. When I started to develop apps, I didn’t validate arguments (In know I was a noob ;)). But since I started to do validation, it reduces the number of bugs in my code, and the difference was big. But that isn’t the only reason I like to do validation. Another  reason is because the caller of the method should know if they have passed a wrong value, there are of course some other reasons also, but the main reason is make sure the caller or the method pass in the correct value, and if they do, they will get what they want.

    I notice that several developers today don’t care about validation, I ask them why and they all gave me the same argument “Why should I, it will only make me add more code and it’s boring to write code that checks values”. Can this be summarized that some developers are lazy?

    Maybe I’m stupid because I get mad when I hear that kind of argument, but I’m special ;)

  • Why can't I be satisfied when I'm looking back on old applications?

    Most of the time I build an application I  have the feeling "this is going to be a great app. Good design, nice written code and a perfect application". I think most of you had the same feeling sometimes. But when I look back on previous applications, I say "What!? NO!!! This is not good, well this could have been done much better ... omg! What have I done! .. what a bad method name....". I don't know why I often feel like that when looking back on my previous apps. When I'm looking back on my code, I always find something I could have done better. Why can't I just write the perfect app, so I can go back later and say to my self "Well Fredrik, this one is great, good work!".

  • Can the use of Extension methods break the Law of Demeter?

    To make an easy description of Law of Demeter we can summarize it to the following sentence:

    “In particular, an object should avoid invoking methods of a member object returned by another method.”

    What does this has to do with Extension methods? It depends on how it’s used. For example we have several Extension Methods for the IEnumerable interface, like Where and OrderBy. By using those methods we can easy select data out from lists, for example (I’m so worthless when it comes to give methods good name, and the following method is so stupid, but it’s only an example.):

  • ASP.Net MVC Framework - How do I design my apps with the MVC pattern and +P

    When I'm playing around with the ASP.NET MVC Framework I have created several prototype applications with different solutions to solve some "problems." In this post I will write down how I combine a Presentation Model (When I talk about Presentation Model in this post, I refer to a model which purpose is only to define a model for presentation purpose) with the MVC, it's what the P in MVC+P stands for. 

    When I build apps today I use Domain Driven Design and I use the MVC pattern in some of my web based applications. As many of you know the M in MVC stands for the Model and contains our entities, business logic can data access components. Often when I build my apps with the ASP.Net MVC Framework (still only use in as a prototype because the framework is under development) I create my own custom Controller Factory to support Dependency Injection (DI). For example I use constructor injection or setter injection to inject my Repositories or manual injection of Mock objects during test. I use Spring.Net as my DI framework because it’s the framework I like the most.

  • ASP.Net MVC Framework 2 - Interception and creating a Role Action Filter

    With the ASP.Net MVC Framework Preview 2, we can use Action Filters. An Action Filter can be used to intercept actions before and after an Action method of a Controller is executed. By using Action Filters you can for example add code that will check if a user has the right permission to access a action method, or you can use it to log messaged before and action is executed, and also after. An Action Filter is easy to apply to a Controller or its Action method. Just add the ActionFilter as an attribute to the class or specific method of a Controller.

  • ASP.Net MVC Framework 2 - Define Routes in Web.config

    Something I like with ASP.Net MVC Framework Preview 2 is the RouteValueDictionary used by the Route class’s Defaults, Constraints and DataTokens property, before they use an anonymous type. Because the properties are now a specific type it’s much easier to define the routes in web.config. Before it was quite easy but some reflection was needed. I have now created a ConfigSection and a helper method to easily register routes which can be defined in the web.config for the Preview 2 version of the ASP.Net MVC Framework. I decided to use the following XML structure to define routes in web.config: