Feb 23, 2009

Type Conversion

I ran into a bit of confusing code recently while I was trolling through some code.  Here’s a simple example.

What Not To Do

This code had two classes that were very similar, but in terms of inheritance were not directly related.  Here I have a similar setup with two classes, Mammal and Table.

ClassDiagram1

Notice that they have identical properties, but you could safely say they had nothing to do with each other.  What’s why I found it odd that the code author was somehow able to get away with this:

Mammal dog = new Mammal();
dog.Color = "Brown";
dog.NumberOfLegs = 4;

//What??
Table dogTable = dog;

Now, I’ve seen some strange stuff in my time, but never have I seen someone get away from that assignment.  That shouldn’t even compile.

I looked into it further and the programmer had done something clever… a little too clever.

public static implicit operator Table(Mammal m)
{
  Table t = new Table();
  t.Color = m.Color;
  t.NumberOfLegs = m.NumberOfLegs;
  return t;
}

The programmer had overloaded the implicit cast operator for that type to allow implicit casting between the two types instead of writing some sort of helper method for conversion.

Several things are wrong with this approach, but the most obvious is the violation of the Principle of Least Surprise.  Kudos to the programmer for trying to simplify code, though.

What To Do

It’s fairly common to need to adapt from one type to another at runtime.  It’s very common these days to need to convert a data entity from a data store to a type that can be served from a service interface, for example.  There are quite a few ways to skin this particular cat.  I’ll show you two I use commonly.

The simple solution is to use a static adaptation method.  There are several advantages to this.  For one, it’s simple to understand.

public static class Converter
{
  public static Table ConvertToTable(Mammal m)
  {
      Table t = new Table();
      t.Color = m.Color;
      t.NumberOfLegs = m.NumberOfLegs;
      return t;
  }
}

You also isolate the coupling between these two types outside of the classes themselves.

The other thing that’s great about this is that it’s compatible with quite a few other scenarios, like converting a list of one entity to another.

Mammal[] mammalList = GetAllMammals();
Table[] tables = Array.ConvertAll(mammalList,
                                 m => Converter.ConvertToTable(m));

AutoMapper

Another, more elegant solution is a library called AutoMapper which comes courtesy of Jimmy Bogard (Twitter). Without writing any explicit conversion code, AutoMapper allows you to do things like this:

Mammal dog = new Mammal() { Color = "Brown", NumberOfLegs = 4 };
Table table = new Table();

//Setup code.  You only have to do this once.
//Maps are cached statically.
AutoMapper.Mapper.CreateMap<Mammal, Table>();

table = AutoMapper.Mapper.Map(dog, table);

Which is brilliant.  Converting an array of objects is just as easy.  I highly recommend this very flexible library.

Hopefully this gives you a few strategies for not confusing me and making your life easier.

Feb 9, 2009

Dirty Laundry in Public

I've been hosting with Ultimahosts.net for quite some time now for my blog. They have never been 100% reliable on the email side (I could expect outages every 4-5 months for hours at a time), but I had very few complaints on the website side of things. If things ever went wrong I could expect a reply from technical/billing/sales guy, Mark Schiavetta. Now, let's get one thing straight. Mark's a bit of a dick. I had come to expect a little dickishness from him over the years and as someone who is a bit of a dick myself, I was pretty ok with it. We did business and went our separate ways. Things went wrong today, though, starting with this email from billing@ultimahosts.net:
We have been trying to resolve this issue internally but have been unable to. On Friday 6th of Februrary [sic] our billing system was hijacked by a previous Ultima Hosts partner. As of now Ultima Hosts has no access to this billing system and therefore cannot cancel credit card authorization or process refunds / payments. Please contact your bank directly to cancel all credit card authorizations with Ultima Hosts USA... ...We are going to be offering free hosting in the month of Februrary [sic] to compensate you for this issue. We take full responsibility for giving access to this senstive [sic] data to someoone [sic] we trusted but who ultimately abused this trust.
Not more than an hour later, I received a billing notice that I was just charged 2x what I am normally charged for my hosting. By whom? Still unclear. I emailed billing@ultimahosts.net a short email saying "should I be worried about this? This is more than I usually pay." This is what I received back, supposedly from Mark:
We have paid this month's hosting fee in good faith after the actions of our ex-partners. Please make sure you cancel all credit card authorities with Ultima Hosts USA LLC.
I'm still a little confused, but ok. I start the process of following Mark's instructions. A little while later I receive an email from a third party I'd never heard of before, KineticHosts.com. The story starts to get epic right about here.
We are aware that Mark Schiavetta has recently sent out emails to you regarding billing with UltimaHosts USA, LLC. We regret that you, our valued customer, are brought into the middle of a business dispute. Let us first make this clear: if you wish to continue your hosting with Mark, we will be happy to refund your recent payments. We are, however, in the process of setting up a new hosting company that will live up to the values that started Ultima which, unfortunately, seemed to have been left in the dust by Mark. If you wish to move your hosting over to us, we will also be happy to accommodate this and will honor your payments in full. It has come to our attention that Mark is also claiming that your account was incorrectly billed and that the payment (PayPal) account was hijacked. This is inaccurate. Mark, on his own, decided that he was going to leave the LLC that was formed between himself, James and Dana. He also removed access to James' and Dana's access to resources held and paid for by the LLC. There had been ongoing negotiations on how to resolve the splitting of the LLC that we feel Mark participated in in bad faith, reneging on an agreement that had been reached to split the company with the shared hosting, which he has claimed that he wants to get out of, going to James and Dana and VPS and dedicated servers going to Mark. Some of you may have experience Mark's harsh attitude in recent months and we do apologize for that. He has claimed that he views shared hosting as a waste of time and did not want to provide the support that you, our customers, have every right to expect. Unfortunately, Mark is not in the US (he now lives in Uruguay) and feels that he can act as he wishes because he is beyond the reach of any and all authorities. (See www.expatworld.org for a description of what he's doing.) Additionally, the PayPal account which was used for UltimaHosts USA, LLC billing was to go to Dana and James. There are many issues with these charges. First, James is the contact for the LLC and the only one with the authority to act on its behalf. Second, the PayPal account that he is referring to is registered solely to James. So, it is not possible for James to have hijacked the billing system. Instead, Mark was "beaten to the punch" since, after revoking server access, Mark was certainly going to hijack the PayPal account himself. Before this, Mark also took money from the PayPal account without authorization. His message today is likely the result of our dispute of this unauthorized withdrawal. We are also actively investigating our legal options, though his location will make any remedies difficult to enforce. Again, we apologize that you have been brought into the middle of this dispute.
I couldn't decide whether to be angry or pleased at my string of luck being witness to this airing of dirty laundry in public. It's not every day you get to see something this pathetic and unbusiness-like. It doesn't stop there, though. I then receive an email shortly after that one from billing@ultimahosts.net (Mark):
Some clients are reporting that they have received unsolicited emails from a third party regarding the unauthorized billing. Rest assured your hosting is secure with us. Our primary concern at the moment is your financial data. Hosting will continue as normal. We are taking every step necessary to ensure that you are not adversely affected by the actions of an unauthorized third party. Please feel free to forward any future unsolicited emails and we will pass them onto the appropriate authorities.
Hosting was indeed continuing as normal, but not billing, apparently. In any case, I shot back this reply to both Mark and KineticHosts.com:
What I would like is to continue ... until such time as I can find an appropriate host outside of this little coup d'état. I don't care about the details of this sorted mess, I just want to keep my service as-is for a month or so. I need specific steps that I need to follow in order to do this, please.
I'd been trying all week to find an excuse to use the phrase "coup d'état", and there it was! So I got back 2 responses, one from KineticHosts.com and one from Mark. Here's Kinetic's take on the deal:
I am sorry but I certainly understand. Your billing will remain the same of you do not register with the billing Mark has setup. This billing is not going to Ultima Hosts USA LLC. We will be regaining access to the servers very shortly and you will be afforded the time to relocate. If there is a chance that we can regain you as a customer I would like to offer that we will keep your billing and plan details the same and we are planning to go back to our roots with this company, again offering add-ons and customizable hosting plans that fit your needs. Our goal is customer satisfaction and we do not wish to charge you for services that you do not need. If I am able to assist you with anything further, just let me know.
That was very nice and customer service-y. I'm still not really sure what I am supposed to do. And here's Mark's response:
It is free for this month so please take the time to find an alternative host.
So it's pretty clear Mark doesn't want my business from this response. This is fine - I've been planning to move away from them for a while, but I didn't expect it to come so soon. My concern turns toward the extra moolah I just paid for my hosting... paying twice and then "getting a month free" adds up to paying the regular rate, for those of you keeping score. At this point I really don't know who has access to my billing information and I'd like to get this resolved without making a claim with Visa. I'm mad and didn't think about backing up my data before I fire off this little number to both Mark and KineticHosts.com:
I'll give my business to the first person who credits my paypal acct the difference in the billing for this month. Go.
To which I received a very prompt response from Mark:
Please take your business elsewhere. I am cancelling your account.
So that is the story of how I ended up with no host and no data from the previous version of my blog. Customer service is something that sets successful companies apart from those that fail or don't deliver on their earning potential. The way I see it, Ultimahosts.net (or whatever form Mark's company turns into) has an uphill battle in winning customers back. As for KineticHosts.com, it would be hard for me to trust a company related to this debacle, but I at least hear some concern for their customers, which is a good start.
Update 2/11/2009
I'd sent an email to Mark asking him for a brief reprive from my account lockout so I could grab my data and restore it here, rather than having to snarf it from archive.org or some other such method.  I hadn't heard back today, so I figured I would give it one last fruitless shot.
I just wanted to follow up and see if it would be possible to get a tiny window of time where I could get access to my data to back it up and take it out.  I'd only need an hour or so, tops.  Thanks again in advance.
I got back a very prompt response this time, which I like.  It's hard to find a hosting company that will respond with a concise understandable answer within seconds of you asking it.
HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH
From one dick to another NO FUCKING WAY JOSE!!!!
So so so glad to be done with fuckers like yourself.
The difference between us dicks is that I am a rich one and you are just plain stupid.
Looks like he reads my blog.  Who knew?