Archive for May, 2008

Checking out the Source Analysis for C#

I’ve just spent about 30 minutes checking out the recently announced Microsoft Source Analysis for C#. I definitely love the idea, but I’m not sure I’ll be using it just yet. Here are some of my impressions (again, I haven’t spent a lot of time on it yet)…

All using directives must be placed inside of the namespace

Hmm, but VS itself violates that rule on every class we create…!


The property must not be placed on a single line. The opening and closing curly brackets must each be placed on their own line

Huh? But that an auto-implemented property. What would we want to split that into multiple lines?!


Variable names must not start with ‘m_’

Hmpf, that’s the standard with have here at the company…


A closing curly bracket must not be preceded by a blank line

Ok, this one I like.


Tabs are not allowed. Use spaces instead

Huh? Thanks, but no, thanks. I’ve seen a lot of people complaining online about this one too. What’s the problem with using tabs??


The call … must begin with ‘this.’…

Ah, I like that one. This is something we’ve made as part of our guideline here, but couldn’t use FxCop to trap for that, since the ‘this.’ doesn’t make into the IL.

Now, the problem here is that, again, the code that VS generates violates this rule all over the place, such as on the example below:


I’ll continue to use this tool to see if it’s going to stick with me. I’ve tried it on a very small project, and got 246 violations; the vast majority where produced by code generated by VS. On a big project, I’d probably end up with a LOT more violations, so I’m not sure I can live with that. I believe there is a way to disable some of the rules, but haven’t looked for it just yet…

Anyway, I do appreciate Microsoft releasing tools like this one. Hopefully, more and more developers will start paying more attention to how they write code.


Interesting training material

As an MVP, I got offered a license to InnerWorkings training material. I’ve been checking out some of it, and it looks pretty interesting.

I noticed they had some material on Domain-Driven Design Patterns, Enterprise Patterns, and some related topics that I’m always interested on, so I decided to try it out.

There is a "Developer Interface" app, where we keep all the material purchased. Each course has a number of tasks, and each task presents the scenario to be solved, the challenges, and reference material (with links to chapter of great books such as Eric Evan’s on Domain-Driven Design).


The task provides a way to launch the sample project in Visual Studio. Once in VS, we get an "InnerWorkings" Tool Window that gives us quick access to the problems we need to address in the solution, and the useful links.

You can either try to address the problems by yourself, or cheat and see the solution. Once you’re done with it, you may click the ‘Judge Project Code" button that gets added to VS to have your code analyzed to see if you’ve implemented things in the way that was expected.   

I’ll certainly be checking out some of the other courses they have available (I need to catch up with some stuff such as WCF, WPF, Silverlight, LINQ to XML, etc.).


Leave a comment

Getting rid of the slow help engine in Visual Studio

It’s been a few years already since the last time I’ve installed VS’ help (MSDN) on my machines. The thing is huge, slow to come up, and it didn’t use to be that good. I just got used to using a SlickRun magicword to quickly search google, which searches not only MSDN, but also other resources (often much better than MSDN) such as CodeProject, blogs, etc. Since I’m connected almost 100% of the time, why use the slow local MSDN library?

Also, every once in a while a reach for the F2 key in order to start a "rename" refactoring in Visual Studio, and and up pressing F1 by mistake, and then there go 2 minutes of my life, waiting for the help window to come up (even though, again, I don’t even have MSDN installed).

Talking to Mike today, I remembered I used to have a little VS macro that’d search on Google the text I have selected in VS. I thought it’d be good to just re-map the F1 key to that macro. It solves to problems: allows me to quickly perform a Google search from VS, and it doesn’t get me stuck waiting for the VS help window to come up if I’ve hit the F1 key by mistake.  🙂

You can find an example of such macro here. In order to change the keybinding, go to Tools-Options-Keyboard, search for the "GoogleSearch" macro, and bind the F1 key to it.


Leave a comment

Matching my own record: 5 and a half years at EPS!

Wow, today, May 16 2008, I’m matching my personal record at working with a company: 5 and a half years, or to be more precise, 2003 days.  🙂

Here’s a little history:

  • I’ve had my first full-time job when I was 12. It lasted only for a full week; I just couldn’t stand the boss.  🙂
  • When I was 14, I took a temporary job (also full-time), filling in for a friend for a full month.
  • The next Monday when I was done with the previous job, I was hired by a company across the hall: that was the company where I had stayed for 5 years and a half years. I started as a courier. After a year, I got promoted to a financial clerk (at the age of 15). Almost three years later, got promoted to a programmer/analyst.
  • From the previous job I went to a new company, as an IT guy (doing mostly software development, but also a lot of computer setup, networking, hardware, user support, etc.). I worked there for about 3 years.
  • Next, new company, now dedicated to software development. Worked there for about 14 months.
  • On to another company, where I’ve worked for about 8 or 9 months.

The reader have probably noticed the pattern: after the company where I’ve worked for over 5 years, my stay at every job was getting shorter and shorter.

Eventually I’ve decided to go solo as an independent consultant. I had my clients, I’ve put together some conferences for software developers, etc. That lasted for about 8 months. I wanted to have no strings attached to any company because at that time there was a good chance EPS would be hiring me, so I wanted to make sure I could join the company at any time. And that happened in November of 2002.

The rest, as they say, is history.  🙂

1 Comment

Material from Tulsa School of Dev 2008 is online

I’ve just uploaded the material I’ve presented last week at the Tulsa School of Dev 2008. The material includes slide decks and source code for my three sessions (C# 3.0, Design Patterns, and LINQ).

The material can be downloaded here.

Leave a comment

Attributes on constants? Haven’t thought about that…

I’m currently reading this article on CodeProject where the author is creating an MVP framework (seems like a good article so far…). In there, the author applies attributes to constants of a type. I had never thought that was actually possible. I want to make sure I keep that at the back of my mind, because I think I’ll be using it sometime soon.

As a quick reminder, this is how that can be implemented:

public class SomeClass
    [Special("Some special attribute")]
    public const string SomeConst = "This is some constant";

public class SpecialAttribute : Attribute
    public string Foo { get; set; }
    public SpecialAttribute(string foo)
        Foo = foo;

This blog post has a handy method to get a list of constants defined on a type (including its baseclasses):

/// This method will return all the constants from a particular
/// type including the constants from all the base types
/// </SUMMARY>
/// <PARAM NAME="TYPE">type to get the constants for</PARAM>
/// <RETURNS>array of FieldInfos for all the constants</RETURNS>
private FieldInfo[] GetConstants(System.Type type)
    ArrayList constants = new ArrayList();

    FieldInfo[] fieldInfos = type.GetFields(
        // Gets all public and static fields

        BindingFlags.Public | BindingFlags.Static |
        // This tells it to get the fields from all base types as well


    // Go through the list and only pick out the constants
    foreach (FieldInfo fi in fieldInfos)
        // IsLiteral determines if its value is written at 
        //   compile time and not changeable
        // IsInitOnly determine if the field can be set 
        //   in the body of the constructor
        // for C# a field which is readonly keyword would have both true 
        //   but a const field would have only IsLiteral equal to true
        if (fi.IsLiteral && !fi.IsInitOnly)

    // Return an array of FieldInfos
    return (FieldInfo[])constants.ToArray(typeof(FieldInfo));

And here’s a little test method to show how to get the attributes out of the constants of a type:

public void TestMethod1()
    SomeClass obj = new SomeClass();

    FieldInfo[] fields = GetConstants(obj.GetType());

    Assert.AreEqual(1, fields.Length);
    Assert.AreEqual("SomeConst", fields[0].Name);

    object[] attribs = fields[0].GetCustomAttributes(false);
    Assert.AreEqual(1, attribs.Length);

    SpecialAttribute special = attribs[0] as SpecialAttribute;
    Assert.AreEqual("Some special attribute", special.Foo);

Leave a comment

Vista gets on my nerves…

This is just stupid: I have a folder somewhere that I’ve created. It’s not under any security sensitive folder such as Program Files or Windows. I’ve copied some files into that folder. Then, I tried deleting the files. Vista asks me  typical security questions, and I say "yes, just delete the damn files". Eventually it just comes back to me and tells me "access is denied".

Then I try moving the files into a subfolder. It has no problems with that. Tried deleting the subfolder; no luck. Moved the files back to the original location.

Next, I try opening a Command Prompt, running it as admin. I tried deleting the files there, no success either.

Finally, I tried running Windows Explorer as admin, and voila, the stupid thing lets me delete the files that I HAVE CREATED!!! Oh joy. How hard should it be for me to delete a stupid file that I’ve created myself, sitting on a folder that I’ve also created, outside of any O.S. folder???

Leave a comment

Lambda Expression and Object Mappers

Here’s a simple implementation of an object mapper using lambda expressions that I’ve shown yesterday at the Tulsa School of Dev. Say we need to copy some data from one object to another. For this example my objects are a Book and an AlternateBook. The class are shown below:

public class Book
    public Book()

    public Book(string title, int yearPublished)
        Title = title;
        YearPublished = yearPublished;

    public string Title { get; set; }
    public int YearPublished { get; set; }
    public string AuthorFirstName { get; set; }
    public string AuthorLastName { get; set; }

public class AlternateBook
    public string AuthorFullName { get; set; }
    public string Title { get; set; }

I could have a mapper that worked like this:

Mapper mapper = new Mapper(book, alternateBook);
mapper.AddMap("Title", "Title");
mapper.AddMap("AuthorFirstName", "AuthorFullName");

That approach would have some problems, though:

  • The properties are mapped using string literals, which means we don’t benefit from strong-typing or IntelliSense (bugs could easily arise because of typos or properties that don’t exist on the objects);
  • The properties have a 1:1 mapping, which in the sample above wouldn’t really work, since I had to map "AuthorFullName" directly to "AuthorFirstName", as oppose AuthorFirstName and AutorLastName.
  • The mapper would have to use Reflection to look up the properties on the given objects and then assign their values (not the most optimized way of doing it…).

The approach using Lambda expression would look like this:

var mapper = new Mapper<Book, AlternateBook>(); mapper.AddMapping((b, ab) => ab.Title = b.Title); mapper.AddMapping((b, ab) => ab.AuthorFullName = b.AuthorFirstName + " " +

Points to note:

  • As I instantiate the Mapper I provide the type of objects as Generic parameters;
  • The AddMapping method takes in lambda expressions that receive two parameters (b and ab, for book and alternate book, respectively), which I then use to set the mapping. That gives me both strong-typing and IntelliSense support.
  • The lambda expression also opens up control over how things are pushed into the target object. In the sample below, I can format how the alternate book’s AuthorFullName property gets populated;
  • Since the lambda expression is compiled down to a delegate, there’s no Reflection involved, so the code should run faster when executed.

Now, in case you’re not familiar with lambdas yet: the expression does NOT get executed when we are passing it as a parameter to the AddMapping method. At that point we are only registering the m apping; the actual execution occurs whenever we want it to. I would probably, say, at the application start, register all mappings I’m likely to use. Eventually, when I need to actually process the mapping (copying some data from one object to another), I can just call the ApplyMapping, passing in the objects involved with the mapping. For example:

Book book = new Book { AuthorFirstName = "Claudio", AuthorLastName = "Lassala", Title = "Some book" }; AlternateBook alternateBook = new AlternateBook();
mapper.ApplyMappingsTo(book, alternateBook); Console.WriteLine( "Book:"+ "\r\n Title: "+ book.Title + "\r\n AuthorFirstName: " + book.AuthorFirstName + "\r\n AuthorLastName: " + book.AuthorLastName); Console.WriteLine( "\r\nAlternateBook:" + "\r\n Title: " + alternateBook.Title + "\r\n AuthorFullName: " + alternateBook.AuthorFullName);

The code above should produce the following results:


So, how’s the Mapper class implemented? Here we go:

public class Mapper<TSource, TTarget>
    private Collection<Action<TSource, TTarget>> m_Mappings = 
                    new Collection<Action<TSource, TTarget>>();
    public Collection<Action<TSource, TTarget>> Mappings
        get { return m_Mappings; }

    public void AddMapping(Action<TSource, TTarget> action)

    public void ApplyMappingsTo(TSource source, TTarget target)
        foreach (var mapping in Mappings)
            mapping(source, target);

The class keeps a list of all the mappings within its Mappings collection, which is a collection of Action<TSource, TTarget> delegates. Both TSource and TTarget are Generic types provided to the class. The AddMaping method takes in the delegate (which we pass in as a lambda) and adds it to the Mappings collection. The ApplyMappingsTo method  takes in instances for the source and target object, iterates through the Mappings collection, and invokes each mapping (don’t forget it, the mapping is a delegate). 

That’s it. Again, this is just a simple implementation. I’m pretty sure there’s a lot of improvements to be done on top of this, and there’s definitely something some people wrote out there around the same lines; I’ll be looking for that myself in order to see how I can improve this.

For instance, one thing that pops up is: what if the developer messed up and specified the mapping going from the target to the source instead? Like so:

mapper.AddMapping((b, ab) => b.Title = ab.Title);

We certainly don’t want to copy the title from the Alternate Book to the Book object. I don’t know of way yet to have the compiler catch that problem (any ideas?). During runtime, though, I know I could have the AddMapping method analyze the Expression Tree for the lambda it was given, and make sure that the target appears at the left side of the = sign, and the target appears at the right side.

Another improvement that could be made is some optimization. For instance, itake the following code snippet:

mapper.AddMapping((b, ab) => ab.AuthorFullName = b.AuthorFirstName + " " +

There, the developer has used string concatenation to build up the value to be assigned to the property. The AddMapping method could have some logic that analyzes the expression tree, and if it finds something like string concatenation, it could replace that with using a StringBuilder. Of course, such optimization should only be done if we can clearly see that as being something that we really needed to squeeze better performance out of.

Also, it’d be convenient to have the mapper just figure out what properties to copy from the source to the target by looking for properties that map 1:1 (that is, property name and type exist on both objects), and then let the developer only supply special mappings for properties that require transformation.

I’ll be doing some more research on better ways to do this kind of stuff.


The Temple of the King

After some pretty tough weeks with a lot of work and little sleep, I spent an afternoon with my friends Milton and Lucas having some fun making some home-recording of Rainbow’s Temple of the King. Nothing like having some fun to relieve some of the stress.  🙂

Check it out:

Leave a comment

The Twitter thing is not so bad after all

A few weeks ago I mentioned I had broken down and signed up for Twitter. Even though I have not yet fell for it yet, I do some of its value. It does have an obvious problem: if you’re not careful, you can quickly get carried away and become and totally unproductive person. What I’ve been doing is to have a Twitter client open in one of my computers, and every once in a while I check out what my peers are up to (usually on short breaks or when my computer is taking to long to process something).

Here are the things I’ve either experienced or seen that I kind of dig:

  • This morning I had to go from the hotel to the campus where the Tulsa School of Dev was going to take place. I was supposed to start my keynote at 9am, so I came down to the hotel’s lobby at around 7:50am, thinking I’d get a shuttle and be at the campus at around 8:15am (I had no idea how far the place was). Somebody from the hotel tells me they wouldn’t drive to the place. Somebody else says they would. Twenty minutes later a "definitive" word comes that they really would not take me there. Ok, no problem, but they could have me told that before, right? Anyway, I asked whether they could just call for a cab, which they said they would. Another 40 minutes go by, and nothing. They called the cab company 4 or 5 times, and on the last time, the company says they don’t have a cab in the area (it’s not like I was in the middle of nowhere; I was in downtown…). Well, I start barking at people, and eventually one of the hotel’s drivers says he’s taking me to the place, and that it didn’t take 10 minutes for us to get there (while we’re on our way, the hotel calls the guy to say he’s in trouble because he wasn’t supposed to take me). What the hell? It’s 10 minutes away from the hotel!! What’s their problem? Anyway, around 8:30am, I had twittered expressing my frustrating with my wait for a cab. When I got to the auditorium, at 9:01am, Dave said he had seen my twitter, so he knew I should be getting there anytime soon. I didn’t have Dave’s cell phone number, so I was just hoping he’d be checking on Twitter. So I was actually glad I used Twitter for that (yes, I did give the hotel’s manager an earful when I got back there this evening).
  • Every once in a while I see somebody twittering links to articles or blog post on things that I’m personally interested in.
  • I end up connecting with people in ways I would not have even imagined before. For instance, in my list of friends and followers, I have a mix of people who are personal friends, and people who knows me from the software development community (people who follows this blog, sees me presenting, or reads my articles). The other day I twittered to say I had finished watching the first season of Heroes, and all of a sudden I start getting replies from people who also enjoyed the show, and I’ve ended up learning about some other things related to the series that I didn’t know about (such as the fact that there are short graphic novels on the series’ website that extend episodes and provide more details about characters and things like that). I really like this social aspect where a person ends up relating to another by means of other things they like but wouldn’t know they shared it because they usually meet in the realm of, say, software development.
  • You learn some fun tidbits, like when Rick smashed his bass sometime in the 80’s and had to borrow one from Testament.  🙂
  • Some people (like Kevin Miller) are already finding ways to use Twitter to produce some business value.

One other thing I’m not liking about it right now is the fact that I don’t own an *i*Phone, I just own *a* phone; a regular Sony phone that has a tiny little web browser, which makes it *really* hard to type anything on it and post to Twitter, or to follow posts. And an iPhone isn’t getting in my budget plans for the foreseen feature (there’s just too many other things taking higher priority).

Note: I was typing this post offline; when I got online, I saw a tweet from somebody pointing out to a post by Rick that’s very similar to this one of mine.  🙂

Leave a comment