Tuesday, October 08, 2013

Serializing list of enums in .NET

Recently I've been coding some WCF methods and I've got a strange exception during WCF message serialization. I needed to send list of enums to the WCF service. There are couple of ways to do that.
First solution is using [Flag] attribute to combine several enum values into one variable - but then you need to use powers of 2 for enum values. I couldn't do that because of the requirements and actual big number of that enums stored in current database. It would require writing and applying a lot of scripts just to correct old enum values in db.
Another way of doing that is by passing a list of enums. So I've decided to use that approach. But strangely I've encountered a little problem around that. Below is a sample code from WCF data contract:

 public class SomeClass  
   public List<SomeEnum> SomeEnums { get; set; }  

Looks pretty simple but if we will try to pass empty list of that values we'll encounter an below exception:
Enum value '0' is invalid for type 'SomeEnum' and cannot be serialized. Ensure that the necessary enum values are present and are marked with EnumMemberAttribute attribute if the type has DataContractAttribute attribute.
Code is valid. The problem is that during serialization .NET framework is trying to serialize empty list which has capacity set to something bigger than 0. So the solution is simple - we just need to set the capacity to 0 by using Count property.

 someObject.SomeEnums.Capacity = someObject.SomeEnums.Count;  

It's a shame that it isn't working like that out of the box. In my opinion .NET Framework should check that before serialization and do not throw any exception. Code is not doing something invalid so it shouldn't throw any exception.

Sunday, October 06, 2013

Gaining MCSD - about Microsoft exams

Recently I've passed last exam required to obtain Microsoft Certified Solutions Developer. Now I'm fully certified in web development in .NET framework. That wasn't my first meeting with exams from Microsoft and because of that I want to leave some comments about them.

First of all I must say that preparation for some of them took me more time than I thought. That's because exams are testing knowledge in a very detailed way. In my opinion sometimes in too detailed way. Of course they should be difficult enough to ensure somebody with this title is representing something but hey - in some areas we are being questioned which attributes in WCF configuration are valid(!). That's not fair in my opinion - I don't think that somebody might be editing those files without referencing MSDN documentation or some kind of editor.
Below are exams that I've passed in chronological order.

70-513 TS: Windows Communication Foundation Development with Microsoft .NET Framework 4
It was mostly about configuring WCF services. I have couple of caveats - as I've said earlier I don't think that knowing all configuration options by heart is a good thing, we need to be realistic about it.

70-480 Programming in HTML5 with JavaScript and CSS3
This exam was offered for free by Microsoft, so I've decided to give it a try. Before preparing for it I thought that it might contain some questions about Metro style specific styles and extensions, but it was purely about HTML5 and CSS3 without mentioning anything about Metro UI.

70-516 TS: Accessing Data with Microsoft .NET Framework 4
This exam was checking our general knowledge about Entity Framework, LINQ To SQL and similar topics. I think it is a very important one, because in almost every application that is being developed we can encounter tampering with data of some kind.

70-515 TS: Web Applications Development with Microsoft .NET Framework 4
Because I've been working a lot with web technologies, mostly with ASP.NET MVC I thought it will the be easiest one to pass for me... and I was right - from this particular one I've managed to gain maximum number of 1000 points. What I didn't like about this exam? The fact that they are testing knowledge about ASP.NET MVC 2 when the current edition is MVC 4. Framework has changed a lot during that time. Hopefully there wasn't too much questions about MVC at all.

70-519 PRO: Designing and Developing Web Applications Using Microsoft .NET Framework 4
This exam was the last one and in some way it's trying to sum up knowledge tested in three earlier exams. Because of that fact it was also rather easy one. After passing that one I've obtained MCPD title.
70-486 Developing ASP.NET MVC4 Web Applications
That one was the next version of 70-515 - I was complaining that it was about MVC2 so they've prepared new version with MVC4 on board (so MVC3 is not included in any exam at all) - and there were even questions about mobile stuff. This exam is definitely the most appropriate in web developer path. It is really testing knowledge that is crucial in real life scenarios. Very important one.

70-487 Developing Windows Azure and Web Services
That one is in my opinion new version of 70-513 because it is mostly examining WCF knowledge. It's a pity that there were only couple of (maybe 3 or 4) questions about Windows Azure. It should have removed Windows Azure from title - because it was only testing general knowledge around that. Most questions were about WCF and LINQ.

To sum up exams are really different - some of them are testing really important knowledge which is good and some of them are checking if you can remember xml configuration schema properly which is bad. Good thing is that preparation to exams in standardizing knowledge and making it more confident. Even though I had had some knowledge around particular topics after preparation my self-confidence had risen.

Another interesting thing is that some kind of people (especially me) need some kind of goals to achieve. Some kind of path to follow. It is really satisfying when you finish something that you have been doing for a long time.