Tagged with c#

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 … Continue reading

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 … Continue reading

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 … Continue reading

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 … Continue reading

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 … Continue reading

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 … Continue reading

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