20060918

Hmmmm... Who to Tap... Hmmm... Part Deux

For those who have no idea what I was talking about in my previous post (20060915).

That snip pet of code was showing how to remove an item on a collection based on a condition Person.IsTappable.

List.RemoveAll() is an alternative of this:

   24 //...

   25 for (int i = people.Count - 1; i >= 0; i--)

   26 {

   27     if (!((Person)people[i]).IsTappable)

   28     {

   29         people.RemoveAt(i);

   30     }

   31 }

   32 //...



NYAHHHH!!!!

20060915

QBert - No Mixer Scratch

He may be good at sqratching, but he can't code!

But damn hes good at scratching!

Hmmmm... Who to Tap... Hmmm...

Check out this test...

    9         [Test]

   10         public void ShouldRemoveUnTappablePeople()

   11         {

   12             List<Person> people = new List<Person>();

   13             people.Add(new Person("Raven Riley", true));

   14             people.Add(new Person("Jenna Haze", true));

   15             people.Add(new Person("Lil' Kim", false));

   16             people.Add(new Person("Beyonce", true));

   17             people.Add(new Person("Missy Elliot", false));

   18 

   19             Assert.AreEqual(5, people.Count);

   20 

   21             people.RemoveAll(delegate(Person p) { return !p.IsTappable; });

   22 

   23             Assert.AreEqual(3, people.Count);

   24             Assert.AreEqual("Raven Riley", people[0].Name);

   25             Assert.AreEqual("Jenna Haze", people[1].Name);

   26             Assert.AreEqual("Beyonce", people[2].Name);

   27         }




Result...
1 passed, 0 failed, 0 skipped, took 0.71 seconds.

Disclaimer:

I would tap Lil' Kim, but not Missy.

20060914

Mapping Non-Typed DataRow to a Property

Ever created a utility class that maps a row to a property on an object you created?

Ever wondered how to convert this row item in to a type? (i.e. int, string, bool, etc...)

Ever created a big ass case statement to assign the damn data types to an item?

Well here is a snippet of code that converts a row item into a datatype based on the property it will be assigned to.

NYAHHHH!



   37                     //...

   38                     MethodInfo method = info.GetSetMethod();

   39                     try

   40                     {

   41                         method.Invoke(result, GetValue(method.GetParameters(), row[column]));

   42                     }

   43                     catch (Exception e)

   44                     {

   45                         throw new FormatException(info.Name + " is an incorrect data type." + info.Name, e);

   46                     }

   47                     //...




   64         private static object[] GetValue(ParameterInfo[] parameterInfos, object parameterValue)

   65         {

   66             string parameterName = parameterInfos[0].ParameterType.Name;

   67             if (parameterValue == DBNull.Value)

   68             {

   69                 return new object[] {null};

   70             }

   71 

   72             Type conversionType = Type.GetType("System.Convert");

   73             string methodName = "To" + parameterName;

   74             MethodInfo conversionMethod =

   75                 conversionType.GetMethod(methodName, new Type[] {Type.GetType("System.Object")});

   76             object val = conversionMethod.Invoke(conversionType, new object[] {parameterValue});

   77             return new object[] {val};

   78         }

NHibernate Config

Im so lazy to make a nhibernate.config that my app uses to load all its NHibernate settings. And since you can't use System.Configuration to load items on a assembly I had to find a way to load all these settings.

I just wanted to spike up NHibernate and verify everything with tests (NUnit). So here is a snippet of code that loads NHibernate setting using a Hashtable.

NYAHHHHHHHH!!!!


   22         private void InitializeNHibernateConfig()

   23         {

   24             Hashtable configProperties = new Hashtable();

   25 

   26             configProperties["hibernate.connection.provider"] =

   27                 "NHibernate.Connection.DriverConnectionProvider";

   28             configProperties["hibernate.dialect"] = "NHibernate.Dialect.MsSql2000Dialect";

   29             configProperties["hibernate.connection.driver_class"] =

   30                 "NHibernate.Driver.SqlClientDriver";

   31             configProperties["hibernate.connection.connection_string"] =

   32                 "Server=GHETTOVM\\SQLEXPRESS;initial catalog=Northwind;Integrated Security=SSPI";

   33 

   34             foreach (DictionaryEntry entry in configProperties)

   35             {

   36                 config.SetProperty(entry.Key.ToString(), entry.Value.ToString());

   37             }

   38         }

20060909

DJ Craze

My Hero - DJ Craze winning his 3rd DMC world championship.

20060907

My First Post

Uhh... I just signed up, and now time to figure out what to discuss about.