<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cymen&#039;s Blog &#187; ASP.NET MVC</title>
	<atom:link href="http://blog.cymen.org/category/web-development/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cymen.org</link>
	<description></description>
	<lastBuildDate>Fri, 03 Feb 2012 17:53:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ASP.NET MVC and DropDownList: One approach&#8230;</title>
		<link>http://blog.cymen.org/2011/11/18/asp-net-mvc-and-dropdownlist-one-approach/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asp-net-mvc-and-dropdownlist-one-approach</link>
		<comments>http://blog.cymen.org/2011/11/18/asp-net-mvc-and-dropdownlist-one-approach/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 14:55:48 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=280</guid>
		<description><![CDATA[One thing that I&#8217;m not particularly fond of in ASP.NET MVC is figuring out where to put additional data one needs for views. If one is using a &#8220;model per view&#8221; approach it is simple &#8212; stick it in that model. Otherwise you can choose to add it to a model that won&#8217;t always need [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that I&#8217;m not particularly fond of in ASP.NET MVC is figuring out where to put additional data one needs for views. If one is using a &#8220;model per view&#8221; approach it is simple &#8212; stick it in that model. Otherwise you can choose to add it to a model that won&#8217;t always need it or pass it via ViewBag/ViewState. I currently lean towards the last option.</p>
<p>The other annoyance is how best to transform a list of objects into a IEnumerable&lt;SelectListItem&gt; collection cleanly with the special case of handling a default value. I have started to approach this with extension methods and am very happy with how it is working.</p>
<p><strong>Transforming your List&lt;MyObject&gt; to IEnumerable&lt;SelectListItem&gt;</strong></p>
<p>Our current data tier has a manager class for each object type. When that object type is going to be used in a select list, I add an extension to IEnumerable&lt;MyObject&gt; like so:</p>
<pre class="brush: csharp">
    public static class MyObjectManagerExtensions
    {
        public static IEnumerable<SelectListItem> AsSelectList(this IEnumerable<MyObject> list, int? value)
        {
            return (from item in list
                    select new SelectListItem
                    {
                        Selected = value.HasValue &#038;&#038; item.Id == value.Value,
                        Text = item.Name,
                        Value = item.Id.ToString()
                    });
        }
    }
</pre>
<p>To support the option of a default value, I have an extension method that applies to IEnumerable<SelectListItem>:</p>
<pre class="brush: csharp">
    public static class SelectListExtension
    {
        public static IEnumerable<SelectListItem> WithDefault(this IEnumerable<SelectListItem> list, string defaultLabel = "Select one...", string value = "")
        {
            return (new[] { new SelectListItem { Text = defaultLabel, Value = value } }).Concat(list);
        }
    }
</pre>
<p>Here is an example of an actually call to this methods in:</p>
<pre class="brush: csharp">
ViewBag.MyObject = myObjectManager
                       .GetAllMyObjectBy(isForFoo: true, orderBy: MyObjectColumns.Name)
                       .AsSelectList(myObjectId)
                       .WithDefault();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/11/18/asp-net-mvc-and-dropdownlist-one-approach/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mime types for ASP.NET</title>
		<link>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mime-types-for-asp-net</link>
		<comments>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 17:28:59 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Mono MVC]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=229</guid>
		<description><![CDATA[One of the annoyances working on the Windows/IIS stack is that getting mime types is a pain. They are located in multiple places and there is no really ideal &#8220;best practice&#8221; method to get mime types without what I consider overly-complicated solutions. In light of this observation I wrote a basic C# program that fetches [...]]]></description>
			<content:encoded><![CDATA[<p>One of the annoyances working on the Windows/IIS stack is that getting mime types is a pain. They are located in multiple places and there is no really ideal &#8220;best practice&#8221; method to get mime types without what I consider overly-complicated solutions. In light of this observation I wrote a basic C# program that fetches the mime.types file from the Apache project and converts it to a C# Dictionary<string, string> keyed by file extension. It is a basic program but might be useful for others wondering why in the world this is so complicated.</p>
<p><a href="https://github.com/cymen/ApacheMimeTypesToDotNet" title="ApacheMimeTypesToDotNet">ApacheMimeTypesToDotNet</a> on github</p>
<p>The output looks like this: <a href="https://github.com/cymen/ApacheMimeTypesToDotNet/blob/master/ApacheMimeTypes.cs" title="ApacheMimeTypes.cs">ApacheMimeTypes.cs</a></p>
<p><code></p>
<pre class="brush: c-sharp">
using System;
using System.Collections.Generic;

namespace ApacheMimeTypes
{
	class Apache
	{
		public static Dictionary<string, string> MimeTypes = new Dictionary<string, string>
		{
			{ "123", "application/vnd.lotus-1-2-3" },
			{ "3dml", "text/vnd.in3d.3dml" },
			{ "3g2", "video/3gpp2" },
			{ "3gp", "video/3gpp" },
			{ "7z", "application/x-7z-compressed" },
			{ "aab", "application/x-authorware-bin" },
			{ "aac", "audio/x-aac" },
			{ "aam", "application/x-authorware-map" },
			{ "aas", "application/x-authorware-seg" },
			{ "abw", "application/x-abiword" },
			{ "ac", "application/pkix-attr-cert" },
			{ "acc", "application/vnd.americandynamics.acc" },
			{ "ace", "application/x-ace-compressed" },
			{ "acu", "application/vnd.acucobol" },
			{ "acutc", "application/vnd.acucorp" },
			{ "adp", "audio/adpcm" },
			{ "aep", "application/vnd.audiograph" },
			{ "afm", "application/x-font-type1" },
			{ "afp", "application/vnd.ibm.modcap" },
			{ "ahead", "application/vnd.ahead.space" },
			{ "ai", "application/postscript" },
			{ "aif", "audio/x-aiff" },
			{ "aifc", "audio/x-aiff" },
			{ "aiff", "audio/x-aiff" },
			{ "air", "application/vnd.adobe.air-application-installer-package+zip" },
			{ "ait", "application/vnd.dvb.ait" },
			{ "ami", "application/vnd.amiga.ami" },
			{ "apk", "application/vnd.android.package-archive" },
			{ "application", "application/x-ms-application" },
			{ "apr", "application/vnd.lotus-approach" },
			{ "asc", "application/pgp-signature" },
			{ "asf", "video/x-ms-asf" },
			{ "asm", "text/x-asm" },
			{ "aso", "application/vnd.accpac.simply.aso" },
			{ "asx", "video/x-ms-asf" },
			{ "atc", "application/vnd.acucorp" },
			{ "atom", "application/atom+xml" },
			{ "atomcat", "application/atomcat+xml" },
			{ "atomsvc", "application/atomsvc+xml" },
			{ "atx", "application/vnd.antix.game-component" },
			{ "au", "audio/basic" },
			{ "avi", "video/x-msvideo" },
			{ "aw", "application/applixware" },
			{ "azf", "application/vnd.airzip.filesecure.azf" },
			{ "azs", "application/vnd.airzip.filesecure.azs" },
			{ "azw", "application/vnd.amazon.ebook" },
			{ "bat", "application/x-msdownload" },
			{ "bcpio", "application/x-bcpio" },
			{ "bdf", "application/x-font-bdf" },
			{ "bdm", "application/vnd.syncml.dm+wbxml" },
			{ "bed", "application/vnd.realvnc.bed" },
			{ "bh2", "application/vnd.fujitsu.oasysprs" },
			{ "bin", "application/octet-stream" },
			{ "bmi", "application/vnd.bmi" },
			{ "bmp", "image/bmp" },
			{ "book", "application/vnd.framemaker" },
			{ "box", "application/vnd.previewsystems.box" },
			{ "boz", "application/x-bzip2" },
			{ "bpk", "application/octet-stream" },
			{ "btif", "image/prs.btif" },
			{ "bz", "application/x-bzip" },
			{ "bz2", "application/x-bzip2" },
			{ "c", "text/x-c" },
			{ "c11amc", "application/vnd.cluetrust.cartomobile-config" },
			{ "c11amz", "application/vnd.cluetrust.cartomobile-config-pkg" },
			{ "c4d", "application/vnd.clonk.c4group" },
			{ "c4f", "application/vnd.clonk.c4group" },
			{ "c4g", "application/vnd.clonk.c4group" },
			{ "c4p", "application/vnd.clonk.c4group" },
			{ "c4u", "application/vnd.clonk.c4group" },
			{ "cab", "application/vnd.ms-cab-compressed" },
			{ "car", "application/vnd.curl.car" },
			{ "cat", "application/vnd.ms-pki.seccat" },
			{ "cc", "text/x-c" },
			{ "cct", "application/x-director" },
			{ "ccxml", "application/ccxml+xml" },
			...
		};
	}
}</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/09/14/mime-types-for-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Idea: enhancing msdeploy with .skip, .skip-production, etc&#8230;</title>
		<link>http://blog.cymen.org/2011/08/16/idea-enhancing-msdeploy-with-skip-skip-production-etc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=idea-enhancing-msdeploy-with-skip-skip-production-etc</link>
		<comments>http://blog.cymen.org/2011/08/16/idea-enhancing-msdeploy-with-skip-skip-production-etc/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 15:46:32 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=225</guid>
		<description><![CDATA[At work we use Jenkins (formerly Hudson) for continuous integration and for pushing out releases to our staging and production servers. It works well for this however the configuration for msdeploy is a bit of a cluster due to it being in a batch script &#8212; adding or removing directories to skip for deployment is [...]]]></description>
			<content:encoded><![CDATA[<p>At work we use Jenkins (formerly Hudson) for continuous integration and for pushing out releases to our staging and production servers. It works well for this however the configuration for msdeploy is a bit of a cluster due to it being in a batch script &#8212; adding or removing directories to skip for deployment is a pain.</p>
<p>I realized today one way to solve this might be to support creating a .skip or .skip-TARGET (so in our case, .skip-production and .skip-staging) files. Than add a batch or simple program to scan the project directory (for script, can use &#8220;<a href="http://mattypenny.blogspot.com/2007/04/dos-equivalent-to-unix-find-name-print.html">dir /S /B FILENAME</a>&#8221; seems to be equivalent of UNIX &#8220;find FILENAME&#8221;) and add a skip for the directory. That way anyone can add the skip option and it is clear in source control what is and what is not getting pushed out.</p>
<p>It seems like a simple enough idea to implement&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/08/16/idea-enhancing-msdeploy-with-skip-skip-production-etc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yet another round on the ModelState PercentComplete() extension</title>
		<link>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yet-another-round-on-the-modelstate-percentcomplete-extension</link>
		<comments>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 15:09:51 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=216</guid>
		<description><![CDATA[So there were a few issues with the previous version &#8212; at least when I wanted to extend it for some custom calculations so here is yet another version: public static int PercentComplete(this ModelStateDictionary modelStateDictionary, int? ScalePercentWithValueAsZero = null, int? MaxValue = 100) { int totalItems = 0; int validItems = 0; int percentComplete = [...]]]></description>
			<content:encoded><![CDATA[<p>So there were a few issues with the previous version &#8212; at least when I wanted to extend it for some custom calculations so here is yet another version:</p>
<pre class="brush: csharp">
        public static int PercentComplete(this ModelStateDictionary modelStateDictionary, int? ScalePercentWithValueAsZero = null, int? MaxValue = 100)
        {
            int totalItems = 0;
            int validItems = 0;
            int percentComplete = 0;

            if (MaxValue.HasValue &#038;&#038; (MaxValue.Value < 0 || MaxValue.Value > 100))
                throw new ArgumentOutOfRangeException("MaxValue must between 0 and 100!");

            if (modelStateDictionary.IsValid)
            {
                percentComplete = MaxValue.Value;
            }
            else
            {
                foreach (var item in modelStateDictionary)
                {
                    totalItems++;
                    if (item.Value.Errors.Count == 0)
                        validItems++;
                }

                if (totalItems > 0)
                    percentComplete = (100 * validItems) / totalItems;

                if (ScalePercentWithValueAsZero.HasValue)
                {
                    if (ScalePercentWithValueAsZero.Value >= percentComplete)
                    {
                        percentComplete = 0;
                    }
                    else
                    {
                        percentComplete = Convert.ToInt32(Math.Ceiling((double)(percentComplete - ScalePercentWithValueAsZero.Value) / (100 - ScalePercentWithValueAsZero.Value) * 100));
                    }
                }

                if (MaxValue.HasValue)
                {
                    percentComplete = percentComplete * MaxValue.Value / 100;
                }
            }

            return percentComplete;
        }
</pre>
<p>And if a model has a particularly complicated percentage complete calculation in which one needs to manually check some things and add to the total that can be done:</p>
<pre class="brush: csharp">
        public int CustomPercentComplete(Func&lt;int?, int?, int&gt; PercentComplete)
        {
            // scale percent complete 50-100% as 0-100%
            int basePercentAsZero = 50;

            // actually, scale 50-100% as 0-91%
            int max = 91;            

            // call default PercentComplete
            int percentComplete = PercentComplete(basePercentAsZero, max);

            // implement here your custom percente complete for the remaining 9%

            return percentComplete;
        }
</pre>
<p>An example of calling the custom percentage complete calculator on your model:</p>
<pre class="brush: csharp">
        myModelInstance.CustomPercentComplete(ModelState.PercentComplete);
</pre>
<p>And if you need to call either calculator but your controller action doesn&#8217;t bind to an instance of the model you need to calculate the percentage on, you can add a method in your controller like this one (hopefully there is a better way &#8212; let me know if you know of one):</p>
<pre class="brush: csharp">
        // Work around for getting percentage complete when in another action where the model is not the application
        // like so:
        // int percentComplete = MyPercentComplete(application);
        private int MyPercentComplete(MyApplication application)
        {
            return Convert.ToInt32(MyPercentCompleteAction(application).Content);
        }

        // Work around for getting percentage complete when in another action where the model is not the application
        // like so:
        // int percentComplete = Convert.ToInt32(MyPercentComplete(application).Content);
        private ContentResult MyPercentCompleteAction(MyApplication application)
        {
            return Content(application.CustomPercentComplete(ModelState.PercentComplete).ToString());
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Another round on the ModelState percentage complete calculator</title>
		<link>http://blog.cymen.org/2011/08/04/another-round-on-the-modelstate-percentage-complete-calculator/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=another-round-on-the-modelstate-percentage-complete-calculator</link>
		<comments>http://blog.cymen.org/2011/08/04/another-round-on-the-modelstate-percentage-complete-calculator/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 18:29:31 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=212</guid>
		<description><![CDATA[Update: The mathematics is wrong &#8212; not sure what I was thinking. See the updated version here. So the prior version has a bug &#8212; it works fine if the scaling value is 50 however it fails at other values. Whoops! That has been fixed and now we have a MaxValue scaler too. The MaxValue [...]]]></description>
			<content:encoded><![CDATA[<p>Update: The mathematics is wrong &#8212; not sure what I was thinking. See the <a href="http://blog.cymen.org/2011/08/16/yet-another-round-on-the-modelstate-percentcomplete-extension/">updated version here</a>.</p>
<p>So the prior version has a bug &#8212; it works fine if the scaling value is 50 however it fails at other values. Whoops! That has been fixed and now we have a MaxValue scaler too. The MaxValue scaler is useful if you want to calculate the percentage done as maxing out at say 91% because you want to manually calculate the remaining 9%.</p>
<pre class="brush: csharp">
    public static class ModelStatePercentCompleteCalculator
    {
        // Very rough but reusable % complete calculator that is an extension so can be
        // called simply as ModelState.PercentComplete() in a controller action. Iterates
        // over the items in ModelState returns percentage complete where a complete field
        // is seen as having no errors. If the ModelState.IsValid returns true, the
        // model is 100% complete.
        //
        // ScalePercentWithValueAsZero: if you calculate the % done on a model and it says say 40%
        // really it is 0% than set ScalePercentWithValueAsZero=40 and it'll treat 40% as 0% and
        // scale it appropriately to 100 or maxValue
        //
        // MaxValue: if you want the maximum value to be less than 100 than set it here so you can
        // accomodate calculating to 100% based on some other criteria too (which you're
        // responsible for)
        public static int PercentComplete(this ModelStateDictionary modelStateDictionary, int? ScalePercentWithValueAsZero = null, int? MaxValue = 100)
        {
            int totalItems = 0;
            int validItems = 0;
            int percentComplete = 0;

            if (MaxValue.HasValue &#038;&#038; (MaxValue.Value < 0 || MaxValue.Value > 100))
                throw new ArgumentOutOfRangeException("MaxValue must between 0 and 100!");

            if (modelStateDictionary.IsValid)
            {
                percentComplete = MaxValue.Value;
            }
            else
            {
                foreach (var item in modelStateDictionary)
                {
                    totalItems++;
                    if (item.Value.Errors.Count == 0)
                        validItems++;
                }

                if (totalItems > 0)
                    percentComplete = (MaxValue.Value * validItems) / totalItems;
            }

            if (ScalePercentWithValueAsZero.HasValue &#038;&#038; ScalePercentWithValueAsZero.Value > 0 &#038;&#038; percentComplete >= ScalePercentWithValueAsZero)
            {
                percentComplete = (percentComplete - ScalePercentWithValueAsZero.Value) / (MaxValue.Value - ScalePercentWithValueAsZero.Value) * 100;
            }
            else
            {
                percentComplete = percentComplete / 100 * MaxValue.Value;
            }

            return percentComplete;
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/08/04/another-round-on-the-modelstate-percentage-complete-calculator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When not to use AutoMapper</title>
		<link>http://blog.cymen.org/2011/08/03/when-not-to-use-automapper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=when-not-to-use-automapper</link>
		<comments>http://blog.cymen.org/2011/08/03/when-not-to-use-automapper/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 01:48:59 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=209</guid>
		<description><![CDATA[AutoMapper is great but it is both specialized, heavy duty and lacking in documentation. The lack of documentation can bite you: if you need to map two objects in different ways in different places things may not work as you expect. For example, say I have Mapper.CreateMap() with a number of .ForMember(x => x.Something, opt [...]]]></description>
			<content:encoded><![CDATA[<p>AutoMapper is great but it is both specialized, heavy duty and lacking in documentation. The lack of documentation can bite you: if you need to map two objects in different ways in different places things may not work as you expect. For example, say I have Mapper.CreateMap<MyObject, MyObject>() with a number of .ForMember(x => x.Something, opt => opt.Ignore()). Now if I create the same mapping elsewhere but with different .ForMember options the prior .ForMember mapping options persist into my current map. To fix this, one can do a Mapper.Reset() which will blow away the global mappings. But creating mappings is expensive. Food for thought.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/08/03/when-not-to-use-automapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AutoMapper and Mapper.Reset()</title>
		<link>http://blog.cymen.org/2011/06/17/automapper-and-mapper-reset/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=automapper-and-mapper-reset</link>
		<comments>http://blog.cymen.org/2011/06/17/automapper-and-mapper-reset/#comments</comments>
		<pubDate>Fri, 17 Jun 2011 19:55:17 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=197</guid>
		<description><![CDATA[Did you know that AutoMapper&#8217;s Mapper.CreateMap() persists even if you use different options on the mapping? So if you have this in one place in your code: Mapper.CreateMap() .ForMember(x => x.BrainSize, opt => opt.Ignore()); var person = Mapper.Map(anotherPerson); And then later this: Mapper.CreateMap(); anotherPerson.BrainSize = 5000; var einstein = Mapper.Map(anotherPerson); That second usage won&#8217;t map [...]]]></description>
			<content:encoded><![CDATA[<p>Did you know that AutoMapper&#8217;s Mapper.CreateMap<T1, T2>() persists even if you use different options on the mapping? So if you have this in one place in your code:</p>
<pre>
Mapper.CreateMap<Person, Person>()
    .ForMember(x => x.BrainSize, opt => opt.Ignore());
var person = Mapper.Map<Person, Person>(anotherPerson);
</pre>
<p>And then later this:</p>
<pre>
Mapper.CreateMap<Person, Person>();
anotherPerson.BrainSize = 5000;
var einstein = Mapper.Map<Person, Person>(anotherPerson);
</pre>
<p>That second usage won&#8217;t map BrainSize! This is quite confusing&#8230; The cache makes sense but it would be more intuitive if the cache was sensitive to .ForMember() usage and other options so that the exact same mapping is cached not any mapping.</p>
<p>The solution is to use this (potentially both before and after your use of AutoMapper):</p>
<pre>
Mapper.Reset();  // don't want some other area's usage polluting us
// create some mappings here that we don't want to pollute other areas
...
Mapper.Reset();
</pre>
<p>I haven&#8217;t read the source yet but this is a very non-intuitive aspect of AutoMapper. Basically on the level of a deal breaker almost in my opinion.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/06/17/automapper-and-mapper-reset/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC, ModelState and Simple Generic Percent Complete Helper Method</title>
		<link>http://blog.cymen.org/2011/06/09/asp-net-mvc-modelstate-and-simple-generic-percent-complete-helper-method/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asp-net-mvc-modelstate-and-simple-generic-percent-complete-helper-method</link>
		<comments>http://blog.cymen.org/2011/06/09/asp-net-mvc-modelstate-and-simple-generic-percent-complete-helper-method/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 21:08:49 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=189</guid>
		<description><![CDATA[Say you have a number of models that are mostly comprised of plain old CLR objects and you need a rough percent complete calculator. Assuming you are using the standard ASP.NET MVC validation rules (via attributes), here is a rough helper method that can calculate the percentage complete for the model based on the number [...]]]></description>
			<content:encoded><![CDATA[<p>Say you have a number of models that are mostly comprised of plain old CLR objects and you need a rough percent complete calculator. Assuming you are using the standard ASP.NET MVC validation rules (via attributes), here is a rough helper method that can calculate the percentage complete for the model based on the number of fields with validation errors divided by the total number of fields.</p>
<p>&nbsp;</p>
<pre class="brush: csharp">
        public static int PercentComplete(this ModelStateDictionary modelStateDictionary)
        {
            int totalItems = 0;
            int validItems = 0;
            int percentComplete = 0;

            if (modelStateDictionary.IsValid)
            {
                percentComplete = 100;
            }
            else
            {
                foreach (var item in modelStateDictionary)
                {
                    totalItems++;
                    if (item.Value.Errors.Count == 0)
                        validItems++;
                }

                if (totalItems > 0)
                    percentComplete = (100 * validItems) / totalItems;
            }

            return percentComplete;
        }
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/06/09/asp-net-mvc-modelstate-and-simple-generic-percent-complete-helper-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>msdeploy &#8211; custom rules</title>
		<link>http://blog.cymen.org/2011/06/02/msdeploy-custom-rules/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=msdeploy-custom-rules</link>
		<comments>http://blog.cymen.org/2011/06/02/msdeploy-custom-rules/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 18:09:21 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[msdeploy]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=187</guid>
		<description><![CDATA[Writing Custom Rules for Web Deployment Tool This is some very useful information. It&#8217;s somewhat frustrating that msdeploy is similar to rsync but seems much less functional from the stand point of what is baked in for rules. And this approach is unofficial and all that so&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://blogs.iis.net/moshaikh/archive/2009/03/03/writing-custom-rules-for-web-deployment-tool.aspx">Writing Custom Rules for Web Deployment Tool</a></p>
<p>This is some very useful information. It&#8217;s somewhat frustrating that msdeploy is similar to rsync but seems much less functional from the stand point of what is baked in for rules. And this approach is unofficial and all that so&#8230; </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2011/06/02/msdeploy-custom-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC Route Registration and Areas</title>
		<link>http://blog.cymen.org/2010/07/09/asp-net-mvc-route-registration-and-areas/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=asp-net-mvc-route-registration-and-areas</link>
		<comments>http://blog.cymen.org/2010/07/09/asp-net-mvc-route-registration-and-areas/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 23:09:47 +0000</pubDate>
		<dc:creator>Cymen</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://blog.cymen.org/?p=162</guid>
		<description><![CDATA[I need to be able to register a route via an instance of RouteCollection to an area. I couldn&#8217;t put it in the normal area route registration as it had to be registered last. It took a while to figure out that this works: public static void RegisterAreaRoute(RouteCollection routes) { routes.MapRoute( "MyRouteName", "{*path}", new { [...]]]></description>
			<content:encoded><![CDATA[<p>I need to be able to register a route via an instance of RouteCollection to an area. I couldn&#8217;t put it in the normal area route registration as it had to be registered last. It took a while to figure out that this works:</p>
<pre class="brush: c-sharp">
public static void RegisterAreaRoute(RouteCollection routes) {
    routes.MapRoute(
        "MyRouteName",
        "{*path}",
        new
        {
            // options
        },
        new[] { "_Namespace_.Areas._Area_Name_.Controllers" }
    ).DataTokens.Add("area", _Area_Name_);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cymen.org/2010/07/09/asp-net-mvc-route-registration-and-areas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

