c# (29)

KnownTypeAttribute serialization error

It can happen that you get this exception while deserializing an object from xml: Type ‘xxxx’ with data contract name ‘xxxxxxxxx’ is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the […]

SimpleMembership with an existing database.

I’ve been trying the SimpleMembership today and I just love it, the Asp.Net team came out with the simplest solution, great! Managing users and their rights has always been a pain using the built in Membership feature pushing many developer to write their custom user/role tables, spending a lot of time in configuration and extending […]

Resize your images for Facebook

This an old utility I wrote a couple years ago in order to resize the images downloaded from my digital camera. After a recent trip to Spain where me and my friends took a lot of pictures, I tough it worthed to pick up again the solution and give it a refresh. Instead of uploading […]

Fluent interfaces introduction and sample.

Learning how to write a fluent interface can help when you want to write the next ORM or if you just wonder how to concatenate your methods in a LINQ style. I’m particularly interested in using this technique to write Html helpers. Anyway, let see a basic example. A simple calculator class/interface , using a […]

Easy join tables with EF Code First.

Entity Framework Code First is getting even better, release after release. Here’s how to easily get a many to many relationship between two entities. Not to much to do really, once the relationship is specified within the two classes (an ICollection navigation property for each of the two ends in the relation), EF will take […]

Default value if null object in an extension method.

Sometimes you need to get a value from a nested object and in order to do it we need to be sure that the object hierarchy do not have any null objects in it. This lead to a long series of checks. Here an example, assuming we got these entities: Code Snippet   public class Country […]

Developer homework test.

Below you can find a test I received via email long time ago. Also, in this attachment there’s what I think is a good solution, any tough or suggestion is appreciated ! Developer Homework Please write code to fulfill the requirements of the following user stories. We are looking for answers that demonstrate good test […]

Ignore all non exisisting properties with Automapper

Code Snippet public static class MappingCustomExtension {     public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)     {         var sourceType = typeof(TSource);         var destinationType = typeof(TDestination);         var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType.Equals(sourceType) && x.DestinationType.Equals(destinationType));         foreach (var property in existingMaps.GetUnmappedPropertyNames())         {             expression.ForMember(property, opt => opt.Ignore());         }         return expression;     } }

MVC3 Retrieving the browser Url

Code Snippet Public Module MyUrlExtensions     <Extension()> _     Public Function UrlOriginal(ByVal request As HttpRequestBase) As Uri         Dim hostHeader As String = request.Headers(“host”)         Return New Uri(String.Format(“{0}://{1}{2}“, request.Url.Scheme, hostHeader, request.RawUrl))     End Function End Module

Scaffhold views/controllers within n-tier solutions. EF Code first flavour.

I’ve been struggling for a while this morning, trying to use one of the most popular MVC tools features: the ability to generate a controller with the CRUD (Create, Read, Update, Delete) actions over a model. The generator will create the different views as well. Or better.. it should =) Everything works smoothly when you […]