Category: Uncategorized

Uncategorized

Implicit and Explicit Operators

Explicit SemanticsI came across an interesting article the other day. It was about a “forgotten” C# feature, the implicit and explicit operators. You can see the original article here. I do remember reading something about these operators years ago, but I don’t think that I’ve seen these operators at use in the wild, though I can definitely think of a ton of times that we needed it.

If you don’t know yet, the explicit and implicit operators help with converting one object of one type into another object of another type. The best use case for this isn’t for converting simple types like ints into doubles, but for converting more complex objects. One common example would be if you are using something like nHibernate or Entity Framework that generates classes for all of your database tables. However, if you are using WCF/Web Services/Remoting/Whatever, you might have different classes that need to exist before transmission.

Let’s pretend that we have the following classes:

namespace DataClasses
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int PersonId { get; set; }
    }
}

namespace ContractClasses
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int PersonId { get; set; }
    }
}

Now, we can see that the classes have the same names and identical declarations. Again, this is very simplified, but it is a real problem. This is one of the types of things that the excellent tool AutoMapper is used for. AutoMapper does this for you automatically, of course, but if you only need this for a few instances, you may not want to take a dependency. If you just try this code, you will get the error “Cannot implicitly convert type ‘DataClasses.Person’ to ‘ContractClasses.Person'”.

DataClasses.Person dcPerson = new DataClasses.Person() {FirstName = "Pete", LastName = "OnSoftware", PersonId = 7};
ContractClasses.Person ccPerson = dcPerson;

However, if you add this code into the DataClasses.Person class (either directly or with a partial), the above code now compiles.

public static implicit operator ContractClasses.Person (DataClasses.Person p)
{
    return new ContractClasses.Person()
               {
                   FirstName = p.FirstName,
                   LastName = p.LastName,
                   PersonId = p.PersonId
               };
}

That does make the code compile, but we are losing some of what is happening there. That is why the explicit operator exists. It allows you to not do a “hidden” convert, but instead make an explicit cast. If we change the code to look like this our code breaks again:

public static explicit operator ContractClasses.Person (DataClasses.Person p)
{
    return new ContractClasses.Person()
               {
                   FirstName = p.FirstName,
                   LastName = p.LastName,
                   PersonId = p.PersonId
               };
}

However, if we change our original code to this, we are back in business and much more readable.

DataClasses.Person dcPerson = new DataClasses.Person() {FirstName = "Pete", LastName = "OnSoftware", PersonId = 7};
ContractClasses.Person ccPerson = (ContractClasses.Person)dcPerson;

This isn’t limited to converting “identical” classes. Jeffrey T. Fritz, the author of the article above that inspired this post converted from an order class to a receipt class, for instance. I definitely believe that using explicit and implicit can make your code much more readable than using utility/conversion classes or extension methods to accomplish the same thing.

Git

Get Your Git On

Can You Dig Git? (from http://blog.aquabirdconsulting.com/?p=262)As I stated in my 2011 technology resolutions, I really wanted to make an effort to learn Git this year. I started with learning some Git Immersion, which I documented as well.

Well, since I like to go big or go home, I decided to start using Git at work full time. We have a big project that we started last week that requires us to branch our code and maintain this “catastrophically different” branch and our “hotfix” branch so that we can still get any bug fixes or emergency features to production in the meantime.

As many of you who have worked with it know, doing a long running branch in TFS (our old VCS) SUCKS hard. The merge would have not been fun. On top of that, switching between the branches to perform bug fixes wouldn’t have been fun either. We would have had to maintain separate directories, which means remapping local IIS routes for testing, etc. Disaster city.

However, when I saw that when you jump between branches in Git and your directories and files just “magically” transform into what you expect them to be in place, I was sold. We had to have that wizardry. Since Chris already knows him some Git, I felt like I was working with a safety net, so we went for it.

Chris used spraints’ git-tfs to migrate our TFS source (with history!) into Git so that we literally lost nothing in the transition. One note of warning was that it had trouble with a very large binary file that we had checked into source due to some memory constraints. We hard deleted it from TFS and the migration went much more smoothly from that point, though Chris still had to work some magic. Maybe he’ll blog it???

We’re officially about a week and a half in with Git and we actually haven’t had as many problems as I had feared. We had a little scare last week when a deployment went wrong, but it turned out that we ended up with a weird WCF issue that forced us to need to reproxy in every project that consumed that service. I had worried that Git had messed up our very large file, but we did some experiments and proved that it behaved exactly as it was supposed to. (Good thing, too, or that “three” I shot in pulling the trigger on the Git switch might have cost me an earful.)

Next blog, I’ll show everyone what “magic” really impressed me with Git. People who are old had at Git will maybe scoff at what I thought was mind-blowing, but I imagine some VSS/TFS-only users may have their face melt off and children will weep over their exploded bodies. (update: I’ve blogged the example here)

Uncategorized

Greetings

Well, I’ve struck out on my own. After having a private blog over on Blogger and posting innumerable quizzes, movie reviews, and jokes, I started to get quite bored. I had the idea to start this blog (and even had the name picked out), but just didn’t really have the motivation to get everything set up.

I’ve finally gotten that motivation and now I’ve struck out on my own with my own installation of WordPress. I plan to blog mostly about everything technology-related in my life (though mostly about software).

It may not be ground-breaking, but hopefully it will be interesting!