ASP.NET MVC 2 - An item with the same key has already been added.

I was working with asp.net mvc, and all of a sudden, on a form post I kept getting :

An item with the same key has already been added.

My model looked ok, the data being submited looked ok - I just couldn't figure it out. I wanted to debug into the MVC2 source (started to think select was broken :) ) ... which lead me down a path where I eventually reinstalled VS 2010 :) .

Eventually I came to my senses and started to think about it. I realized that my view Model had a base class - and then it hit me : in the base class I had a property "UserName" and in the child class "Username". Notice the small difference in caseing. 

Since the model binder doesn't care about case - there you have it : a nice cryptic error. 

Hth.

Tags:

invalid XML identifier as required by FOR XML

 The weirdest error I got today was this : 

  •  Column name 'DATA()' contains an invalid XML identifier as required by FOR XML; '('(0x0028) is the first character at fault.

This was happening when I was trying to concatenate rows from a column into a single row like this :

   1:  
   2: REPLACE(
   4: (
   6: SELECT [Name] as [DATA()]
   7:   
   8:   FROM ContactPerson 
   9:  
  10:    WHERE ClientId = dbo.Client.ClientId
  12:    
  13: FOR XML PATH ('') ), ' ', ',') as AllPersonsNames

 I eventually found on a web site the answer : it shouldn't be DATA() it should be data() Surprised

Tags: ,

Inversion of Control on PocketPC

Something one should really strive for (in my opinion) is writing code in the spirit of the SOLID principles . One of the principles talks about DI( Dependency Injection) and even if it doesn’t refer strictly an Inversion of Control container – this is the easiest example of DI.

A simple example of DI would be an `OrderProcessor` class that needs to calculate on an order the tax that should be applied. That calculation would best be accomplished by a separate class, and because there might be more than one way to calculate taxes (different states from where the order is placed for example) `OrderProcessor` shouldn’t depend on a specific implementation of the tax calculator.

So an interface appears and instead of :

   1: class OrderProcessor{
   2:     ITaxCalculator calculator1 = new SomeStateTaxCalculator();
   3:     ITaxCalculator calculator2 = new SomeOtherTaxCalculator();
   4:     
   5:     // do some logic and decide which calculator to use ..
   6: }

you would have :

   1: class OrderProcessor{
   2:     ITaxCalculator _calculator;
   3:     
   4:     public OrderProcessor(ITaxCalculator calculator){
   5:         _calculator = calculator;
   6:     }
   7:  
   8:     // now you have the calculator you need
   9:     // the responsibility of which calculator to use is no longer in this class.
  10: }

To the PocketPC

And now after a short introduction to what Inversion of Control is about, I was surprised to find out that on the .Net Compact Framework is quite hard to find an IoC library.

Luckily, Germán Schuager, has put in the effort of creating a small version of an IoC container. That can be found at : http://code.google.com/p/compactcontainer/ . You’ll also find on that page a few links to short example of how to use the library.

For personal use I just made a small class to register types needed at startup, and be able to resolve dependencies at runtime.

   1: static class IoC
   2:     {
   3:         private static CompactContainer _container;
   4:         private static bool _configured;
   5:         public static void Configure()
   6:         {
   7:             _container = new CompactContainer { DefaultLifestyle = LifestyleType.Transient };
   8:  
   9:             _container.AddComponent(typeof(ISettingsManager), typeof(SettingsManager), LifestyleType.Singleton);
  10:             _container.AddComponent(typeof(IRatesDownloader), typeof(RatesDownloader));
  11:             _container.AddComponent(typeof(IRatesParser), typeof(RatesParser));
  12:             _container.AddComponent(typeof(IWebUpdater), typeof(WebUpdater));
  13:             _container.AddComponent(typeof(IContextPersister), typeof(ContextPersister));
  14:  
  15:             _configured = true;
  16:         }
  17:  
  18:         public static T Resolve<T>()
  19:         {
  20:             if (!_configured)
  21:                 throw new Exception("The IoC container was not configured before use. Call Configure() on the IoC class before the Application.Run");
  22:             return _container.Resolve<T>();
  23:         }
  24:     }

Tags: , ,

Currency Calculator for PocketPC

This summer I'm taking a vacation to Croatia and I wanted a simple PocketPC app that will convert between the Euro, the Croatian kuna , and my national currency the Romanian Leu.
Obviously I first searched for such an app and I did found one - unfortunately id had everything I wanted BUT the Croatian kuna :). So , without too much thinking I decided to write the app myself (small syndrome of not invented here :) ). So, I set about creating my own application.

 The most important part(at least from a programmers point of view) , and the one that I really wanted to get right was the CurrencyCalculator that would convert from a currency to another currency and the Money class that would represent a specific Currency.
I'm not exactly proficient with TestDrivenDevelopment and thought this would make a grate candidate for TDD. You'll find 2 (yes two :) ) tests in the CurrencyCalculator.Tests project. 

These are the only tests in the application for the moment , I'll write a few more for some other stuff (like downloading an xml with exchange rates ) but the rest is pretty much UI stuff which I'm not gonna test.

Another interesting part of the project is the Dependency Injection part - where I only wanted to be able to do some constructor injection and be able to specify LifeStyle (Transient or Singleton) for different classes . It's surprising how little choice you have for the mobile platform, even for simple stuff.

But I'll talk more about this in another post.

The project is Open Source, and you can grab the code from google code : Currency Calculator for PocketPC.

Tags: , ,

Insert generator for MSSQL tables

Having inserted some data in an MSSQL database table, I wanted an easy way of creating an insert script for that specific table. And I don't want something fancy that will generate inserts for all my tables because most of the time the inserts I want generated are for tables that have lock-up data like : ProjetStatus or ProjectResolution. I certanly didn't want something like : sqlmanager - it does alot of stuff that I just didn't need.

My first instinct was : "I'll create a small application with the help of SMO that will ....."(slaps self arround the head). I realized this would take some time and it wouldn't be very usefull. What I really wanted was something done by someone else, something that just worked. 

A quick search on google revealed a cool stored procedure for MSSQL that would do exactly what I wanted and more, here it is: a procedure that would generate inserts for your data.

And the beauty of it is that it just worked. The script generates the procedure in the Master database so don't look for it in your database like I did :).

I also saved the file here, in case the original site won't respond at one point.

generate_inserts_mssql.txt (18.97 kb)

Tags:

WPF - MeasureOverride and ArrangeOverride

Some time ago I read on a blog that if MeasureOverride returns new Size(0,0) then that FrameworkElement says it want's all the space that it is given. However there's a ...

Problem

When working on a control derived from FrameworkElement and that contained other controls, in the MeasureOverride method I called Measure() on all children and then returned new Size(0,0) - I wanted this control to take ALL the space that it was given.

Now a problem appeared - actually the problem was that the control did not appear :). So I started to trak down the problem ( the control previously returned the MeasureOverride size ) and what I realized was that if the Width and Height were set on the control then the control looked great. But if the control had W and H not set , it didn't appear at all.

To resolv this problem I used another method that would measure the containing elements an then return that size for the MeasureOverride as well as for ArrangeOverride - now all of a sudden the control works as intended. :)

Conclusion

So in conclusion if you're setting Width and Height on your control then it's ok to return new Size() - Canvas does that.

But if instead you want the control to get the Width and Height from it's children , you MUST return the proper Size from MeasureOverride.

 

Enjoy,

Tags: , ,