development

warning: Creating default object from empty value in /home/pulsar/webroot/htdocs/modules/taxonomy/taxonomy.pages.inc on line 33.

Unexpected Consequences


Ooops! That was quite unexpected

Accessing Arrays in .NET/c# via Reflection

Yesterday I was implementing a generic serializer (for logging purposes). While there is a good number of XML serializers available for .NET - none of them was meant to be used for only the purpose of reading. That means, I would not need deserialization but only some nice human readable output.

Anyway, thats not the point here. What took me about an hour to figure out is how to handle arrays (of natives) via reflection. I kinda feel stupid, but let me show you what i tired:

  1. Object propertyValue = prop.GetValue(o, null);
  2. if (propertyValue != null && propertyValue.GetType().IsArray)
  3. {
  4. //if you need the type, here is how we get it, it's not used
  5. //in this example.
  6. Type arrayType = propertyValue.GetType().GetElementType();
  7. object[] arr = propertyValue as object[];
  8. for (int i = 0; i < arr.Length; i++)
  9. {
  10. // serialization logic goes here
  11. }
  12. }
  13. ...

While this might work perfectly for arrays of "real" objects such as string[] - it will fail when your array is composed of native objects such as long[]. The cast to object[] will get you a nice null reference. Yummy.

After that I've been looking for Array.ForEach() which was pretty interesting, but did not really help due to it's generic nature. The solution was much simpler in the end, it just did not cross my mind to cast the propertyValue to Array - sometimes .NET can be pretty mysterious in terms of Autoboxing. Here is how it should be done:

  1. Object propertyValue = prop.GetValue(o, null);
  2. if (propertyValue != null && propertyValue.GetType().IsArray)
  3. {
  4. Array array = propertyValue as Array;
  5. foreach (var arrElement in array)
  6. {
  7. // serialization logic goes here
  8. }
  9. }
  10. ...

Straight forward, simple - makes me feel stupid.

Project Euler

So, you want to give your programming skills some training? Or just want to get familiar with a programming language and all test-projects you can come up with are pretty large and have been done 1000 times before? Yes, I know, your blogging software will be badass but will it be ever ready?

Anyway, If you are looking for some nice exercises which will also improve your math skills dramatically - head over to Project Euler and practice!

It contains a lot of math problems which can only be solved using a computer and won't knock out you right away, the challenges will guide you through ascending difficulty levels and should always give you an idea about the math you should use. 

Why I Love Local Initializers...

  1. layoutTable.Rows.Add
  2. (
  3. ControlUtils.CreateRow
  4. (
  5. EmployeeFieldLabels.ExternalCompanyName,
  6. new ExtCompanyPickerControl(CreateChildID("ExternalCompanyName"))
  7. {
  8. Customizer = new ExtCompanyCustomizer(MedaSession),
  9. AllowMultipleSelection = false,
  10. RenderListHeader = false,
  11. CountryProviderId = locationPickerControl.ID,
  12. CountryProviderProperty = "SelectedCountryIsoCode";
  13. },
  14. false,
  15. EmployeeFieldTooltips.ExternalCompanyName,
  16. Page,
  17. CreateChildID("ExternalCompanyLabel"),
  18. (
  19. externalCompanyValidator = new CompareValidator
  20. {
  21. ControlToValidate = CreateChildID("ExternalCompanyName"),
  22. Operator = ValidationCompareOperator.Equal,
  23. Type = ValidationDataType.Integer,
  24. ValueToCompare = "1",
  25. EnableClientScript = false,
  26. Display = ValidatorDisplay.Dynamic,
  27. ErrorMessage = string.Format(EmployeeManagerControlRes.ErrorValueRequired, EmployeeFieldLabels.ExternalCompanyName)
  28. }
  29. )
  30. )
  31. );

Check them out! http://msdn.microsoft.com/en-us/library/bb384062.aspx

Adaptive Framerates Or How Software Video Players Can Maximize The User Experience

Here is an article I originally wrote for the boxee forum:

I would like to address a feature commonly ignored by all media players I have come across so far. Let's talk about adaptive speed. Regardless if you are living in the USA (NTSC) or Europe (PAL) - you will suffer from video jittering sooner or later. (more after the link)

Let me describe the issue here a bit closer.

Imagine a landscape being filmed while the camera pans slowly from the left side to the right side. If you are watching this scene on a DVD using a standalone DVD Player everything should be smooth and without any jittering. Now if you use this DVD and try to playback that one using a PC you will notice the playback not to be that smooth anymore. So, what is happening here?

Once the DVD Player has determined the frame rate of the source video material it will also set the video output refresh rate to match that speed. For instance, If we are dealing with NTSC material which would be at ~30fps the TV will be advised to use ~60Hz. Each frame in the video source will be displayed on 2 frames rendered on your screen. So, everything is fine and in sync here.

Now, if you head over to your PC and configure your monitor to use the refresh rate of 75 Hz for instance, that very same video would map one frame in the video material to 2.5 Frames on the screen. Depending on the player software used you will notice one of the following artifacts:

SQLite, nHibernate and .NET

Misusing this blog as my personal Post-It: http://www.mikeduncan.com/sqlite-on-dotnet-in-3-mins/

User Management with Active Directory

The last 2 days I've been working on a concept paper for a project. One aspect covers user management using an active directory.

At the first glance, everything looks easy. One would think AD is just another LDAP server - no worries here. It turns out, AD has a lot of hidden constraints and expect you do things a certain way. For instance, if you want to add a user to a specific group, you are not going to update the user's "memberOf" LDAP attribute. This is read only; you will need to add that user to the group LDAP node. This is just one and quite straight forward example how AD can be a pain in the arse if you have no idea about the internals.

Luckily the guys over at http://en.csharp-online.net/ have assembled a damn good article about all daily tasks you will encounter if you are going to automate some user administration tasks using the Active Directory as your backend. Head over here: http://en.csharp-online.net/User_Management_with_Active_Directory%E2%80%...

I have also stumbled across a wrapper library which abstracts all the LDAP / DirectoryEntry specific calls and focuses on user- and group-centric API: http://www.dotnetactivedirectory.com/ - I have no idea yet if this one is good or not, I guess I will find out soon since I plan to cut down the development times by 2 Days and use this API instead.

update
After looking into the free (lite) version of .NET Active Directory wrapper, I am not so sure about purchasing a license anymore. 400 or 1000 bucks a a pretty hefty price tag for a software not being in development anymore (as it seems, last update 2007) and consisting only from one pseudo class. It can hanlde only one AD connection due to static (!) fields for login and password. Furthermore it does not persist a connection to the AD but re-creates a new Directry Entry with each operation - now do some mass operations on say... 5k users by assigning them to a AD groups or something.  I suppose it will be still more expensive for the project for me to code the mini-api, but after considering those facts, I might be better off. Especially when it comes to multiple AD servers and support requests I might get for this particular module. Geez! Where do ppl learn coding!? I guess good marketing and price tags not reflecting the code quality can still compensate it

Push To Sync updated to 1.2

Push To Sync 1.2Push To Sync 1.2I've just uploaded an new improved version of Push To Sync. Changelog:

  • [Feature] Time prediction based on average transfer speed 
  • [Feature] Shows the current transfer speed using a nice looking graph
  • [Bugfix] Fixed some issues with file timestamp handling when synchronizing to a linux samba share.
Download available here.

Optimizing for MySQL

Just stumbled over a very good speech about optimizing development for the MySQL (MyISAM & InnoDB) over at Hackzine. If you are developing application which are run on the MySQL Database this is a must-see for you. Despite the fact developing such stuff for more than 10 Years now I had one or two jaw-dropping moments.

 

Sharepoint STSADM Extensions which will save your life!

If you are into Sharepoint-Development you will appreciate the STSADM Custom Extensions made by Gary Lapointe: http://stsadm.blogspot.com/2007/08/stsadm-commands_09.html

In our case they saved us a lot of troubles we had when moving a Sharepoint Site Collection to a new server while preserving the Variations which have been created before.

One of the commands we have been toying with was "gl-fixvariationrelationships". Since the sources to all of these extensions are available to download for free, I could even get this one working for a Variation root other than "/" by modifying the sources a tiny bit. If you run into the same situation feel free to grab my Version of the FixVariationRelationships Class over here or here (can't get drupal to ignore HTML validation for Geshi-Nodes editing)

 

Syndicate content