Ivan Krivyakov's Blog

Premature optimization is the root of all evil

January 29, 2012

Midnight Thoughts about MVVM

What keeps bothering me about MVVM, is that user-defined views behave very differently from built-in controls. We use built-in controls like this:

<ListBox ItemsSource=... SelectedItem=... />

If, however, ListBox were our own user control, it probably have looked like this:

<ListBox DataContext="{Binding ListBoxViewModel}" />

class ListBoxViewModel
{
   public IEnumerable<bject> ItemsSource { get; set; }
   public object SelectedItem { get; set; }
}

This, of course, will make using ListBox much more complicated and cumbersome, as it would introduce a new entity called ListBoxViewModel, an instance of which must be created for each list box. Also, list box users would have to ensure that the view model supplied is indeed of type ListBoxViewModel, and not ListViewViewModel or TextBoxViewModel. The compiler would not check the type of the view model. If wrong view model is supplied, the list box would probably fail mysteriously at run-time, spewing a bunch of binding errors into the console window.

Thus, tying a view to a data context that must be of specific type inconveniences the users and makes the view difficult to reuse. If a view is responsible for displaying a business object, e.g. a Trade, from the view user’s perspective

<TradeView Trade={Binding aTrade} />

would be much more convenient, logical and safe than

<TradeView DataContext="{Binding aTradeViewModel}" />

However, if TradeView only receives a Trade object via property, it creates another problem. Let’s say TradeView needs to display some additional information about the security being traded, and this information is obtainable from ISecurityMaster service.

If TradeViewModel is passed to the view from the outside, it may contain ISecurityMaster reference, and obtaining that reference is not responsibility of the view. E.g., it may be done via some dependency injection framework, of which the view has no knowledge, and this is a good thing.

However, if the view only gets the Trade object, now it becomes the view’s responsibility to get the security master, and the view does not have enough context to do that. So, not only we will have to put some clever logic into the view, we will also need to use some kind of static (read: global) service locator, which will create hidden dependencies and affect testability of the application.

At this point I am not sure how to resolve this dilemma. Probably, by the time I come up with a satisfactory solution, everyone would be using WinRT anyway :) Perfection is achieved at the point of collapse.

January 25, 2012

Perforce + Unicode = Eplic Fail

Just created a Visual Studio Add-in using the wizard. The source files created by the wizard turned out to be UTF-16. The files I create manually are UTF-8. It is all fine until you check in UTF-16 files into Perforce. It sees line endings like this: 0D 00 0A 00 and “fixes” them by inserting an extra 0D, making it 0D 00 0D 0A 00.

This, of course, screws up a UTF-16 file royally: you get a file full of some Kanji characters. I actually had to write a program that undoes that deed and manually change file encoding to UTF-8. Ouch.

January 2, 2012

Playing with Entity Framework

As certain other “highly integrated” Microsoft technologies, entity framework seems to be high maintenance when it comes to refactoring and moving things around.

To move an .emdx file from one project to another, one needs to:

  • Move the file itself
  • Add a bunch of assembly references to the project and to the system.web/compilation/assemblies section of the web.config
  • Add at least three (3) special connection strings where the name of your model file, including the namespace is repeated about a dozen times, along with the name of your DB server and database.

DRY (don’t repeat yourself) clearly does not apply here

December 6, 2011

Outlook/Exchange Search Performance…

sucks. I did a search for a message from John Doe sent within last month that contains word “foo”. Took about 5 minutes on a folder with about 9300 items. It takes seconds on my Outlook Express (if I limit the date range like that). And this is not a bug, this is a feature:

http://support.microsoft.com/kb/905803

Oh, and by the way, it did not find the message :)

December 5, 2011

C++

C++11 adds a new non-const reference type called an rvalue reference, identified by T&&.
Say what?! Another grammar ambiguity?
Now x && y could be a variable declaration or an expression, depending on what x and y are. Nice.

HTC Droid Incredible 2

Not really a programming topic, but…

Had it for a while now – got it from work. Very lightweight and confortable phone. But I have Motorola Droid to compare, so…

1. The speaker sucks, as it does in any other HTC phone I saw. The volume on speaker is barely more loud than the volume without speaker. This is especially annoying when using the phone for GPS.

2. The phone app is brain dead. In Motorola it uses buttons with clear text labels: Phone, Call Log, Contacts, Favorites. Never got them confused. In HTC it is just icons, that

a) are not descriptive: the Icon for “people” is hardly different from the icon for “messages”

b) don’t fit the screen, unless you customize them out

c) if you don customize them out, you lose corresponding feature completely; e.g. if “send message” icon is not visible, there is no way to send a message to a contact, even via menu or anything else

d) they do this annoying dance when you select them, making you think they moved, but they actually did not. Whoever designed this “feature” should be sentenced to manually routing IP packets for life.

e) if you are in a call they somehow lock down to the person you are calling; when someone asked me someone else’s phone number during the call, I got totally stuck; the only way I found around it was to put “people” shortcut on the home screen

3. And now the killer: you answer the phone but dragging the screen control down (answer) or up (decline). Looks cool, unless the phone is in your pocket. Which means you auto-answer (or sometimes decline) the call while you are taking the phone out. Very bad ergonomics solution. They say there is an “easy answer button” app that fixes this, I will try it. But come on, HTC!

October 20, 2011

Windows 8: Dazed and Confused

So, it looks like we get two operating systems for the price of one. It has two styles of apps: one more suitable for a phone (a.k.a. “Metro” style) and the other suitable for desktop. Metro apps are always full screen, they cannot be closed and do not support right click.

The whole experience is completely schizophrenic: there are two main desktops, two versions of Internet Explorer, etc. Interestingly enough, two Internet Explorers report slightly different user agent strings to the web servers:

Metro:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)

Desktop:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)

It looks like the old explorer is a 32 bit process, while the new one is 64 bit.

The old one is in C:\Program Files (x86)\Internet Explorer, the new one in C:\Program Files\Internet Explorer, they have slightly different file sizes, but the same version number (10.00.8102.0). The last part is totally uncool: number one rule for versioning is that if two released files are different, they must have different version numbers, or you are asking for a maintenance nightmare. Well, this is still an evaluation version…

But, as A.S. Pushkin used to say, “You cannot hitch a trembling doe And horse up to a single carriage“. And I am afraid Microsoft is trying to do just that.

August 29, 2011

Creating ASP.NET membership database: no funky passwords!

To simplify creating and managing users in ASP.NET Microsoft created a lot of infrastructure that takes care of this boilerplate task. Part of the infrastructure is

aspnet_regsql.exe

utility that is supposed to create relevant tables on an SQL server. The trouble is, if you use SQL server authentication, the utility does not like passwords with funky characters. It looks like they get added to the connection string verbatim, and thus you get

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 31.

Looks like they just append the passwords to the connection string, verbatim without escaping. Way to go (not)!

August 9, 2011

JAX-WS and @XmlRootElement

Just finished a fight with JAX-WS. It turns out that if I have a method like this:

@WebService(targetNamespace=...)
@SOAPBinding(parameterStyle = SOAPBINDING.ParameterStyle.BARE /* don't ask */)
class MyWebService
{
    @WebMethod
    MyClass getMyStuff();
}

then generated WSDL depends on whether NyClass has @XmlRootElement annotation. If it does, you’re going to have a WSDL with empty input (client sends empty SOAP body with no indication of request kind whatsoever). If it does not, then everything is OK, request body is not empty.

I am sure there is a simple, logical explanation to all this…

August 8, 2011

It’s the solar flares, stupid

Three large explosions from the sun over the past few days have prompted U.S. government scientists to caution users of satellite, telecommunications and electric equipment to prepare for possible disruptions” (from Slashdot).

That’s it. That explains it. Everyone is just affected by the pesky magnetic fields. Why S&P should be any different? “Mommy, I lost $2 trillion, because the Sun flare burnt my homework” seems like a valid excuse now :)