<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ahmadreza&#039;s Notes</title>
	<atom:link href="https://ahmadrezaa.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://ahmadrezaa.wordpress.com</link>
	<description>On Software Development</description>
	<lastBuildDate>Tue, 04 Nov 2014 12:24:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<site xmlns="com-wordpress:feed-additions:1">21473098</site><cloud domain='ahmadrezaa.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://s2.wp.com/i/webclip.png</url>
		<title>Ahmadreza&#039;s Notes</title>
		<link>https://ahmadrezaa.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://ahmadrezaa.wordpress.com/osd.xml" title="Ahmadreza&#039;s Notes" />
	<atom:link rel='hub' href='https://ahmadrezaa.wordpress.com/?pushpress=hub'/>
	<item>
		<title>First week at number one consulting company in Australia</title>
		<link>https://ahmadrezaa.wordpress.com/2014/11/04/first-week-at-number-one-consulting-company-in-australia/</link>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Tue, 04 Nov 2014 12:22:54 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Readify]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=276</guid>

					<description><![CDATA[I have been in IT industry for fifteen years and for last three and half years I been in consulting firm. I really like the type of work I usually do as a consultant. Issues that we have to deal with are usually different and more challenging. Systems we have to build requires more focus [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I have been in IT industry for fifteen years and for last three and half years I been in consulting firm. I really like the type of work I usually do as a consultant. Issues that we have to deal with are usually different and more challenging. Systems we have to build requires more focus and special architecture and extra care. In this industry I do meet new people and a really like to pair with others, teach and of-course learn new things. </p>
<p>Last week, I started working for a new company called Readify. Programmers in Australia, specially Microsoft stack programmers, would probably know Readify. But for those who don&#8217;t know, Readify is the best Microsoft Consulting company in Australia which has offices across major states in Australia (NSW, VIC, QLD, Perth and SA). I’ve been interested to work for Reaidfy since I came to Australia. But it happened for me after four and half years. Many thanks to my good friend <a href="https://twitter.com/hadi_es">Hadi Eskandari</a> who mentored me during last year when I was studying so hard to get through.</p>
<p>Anyway, If you want a challenging workplace, pairing with smart and knowledgeable developers and using bleeding edge technologies, probably Readify is one of places you might want to consider to work in Australia. During past few years great developers have been working for Readify. To name a couple of them, <a href="https://twitter.com/DamianEdwards">Damien Edwards</a> who is currently program manager at Microsoft at ASP.net team and <a href="https://twitter.com/paulstovell">Paul Stovell</a> creator of famous deployment tool called Octopus Deploy. And many others who still work for Readify.</p>
<p>Its an opportunity to work in challenging environment with high quality expectation. When a client choose Readify, most likely there is a challenging issue or project. I as a consultant have the opportunity to work with highly skilled and smart people in different project.</p>
<p>Now, let me tell you about first impressions I’ve got since I’ve started with them. I signed the contract nearly six weeks ago and immediately a couple of days after, I got access to my corporate email and other documents so I got myself familiarized with the processes and introduced myself to the team before actually starting new job. Everything was surprisingly well organised and open. </p>
<p>I got my allowance to buy gears and get ready for my new position at Readify. The allowance is enough to buy a decent ultrabook or mac book pro which gets renewed every two years. I decided to buy a Surface pro 3 which I will blog about that later. Anyway, I had a chance to get everything ready before the very first day.</p>
<p>Day one when I went to Readify’s nice office in the city everybody were welcoming to me and helping me to get started. There is is buddy system at Readify which helps new Readifarian get through all induction stuff faster. My buddy <a href="https://twitter.com/jorgefioranelli">Jorge</a> helped me to get through all the stuff quickly. I had a chance to have a chat with one of the principal consultants (<a href="https://twitter.com/rbanks54">Richard Banks</a>). Although principal consultants are busy but they are available to help developers and other consultants. Basically we can reach them quite easily. During past week I had a change to meet other principal consultants and senior consultant and catch up with them. </p>
<p>At Readify everybody gets number of days annually to work on professional development, we can self study new technologies, prepare for exam, write an article or get ready for presentation. First few days I had a chance to look at new interesting changes in mobile area with Xamarin products. Xamarin has gained a lot of attention recently especially in enterprise. I will write more about them later.</p>
<p>So far it has been great experience working at Readify and I have just started my first gig this week which is going to be awesome.</p>
<p>Stay tuned I’ll blog more to share technical and non-technical stuff.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">276</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>
	</item>
		<item>
		<title>AutoMapper and mapping Expressions</title>
		<link>https://ahmadrezaa.wordpress.com/2014/09/20/automapper-and-mapping-expressions/</link>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Sat, 20 Sep 2014 00:19:45 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[AutoMapper]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MongoDB]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=258</guid>

					<description><![CDATA[AutoMapper is a great library helps developer to almost get rid of left hand right hand assignment between different types. In real world C# .net applications there are lots of scenarios developer wants to map different types of objects to each other. Normally it happens in system boundaries, like between UI and services or between [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>AutoMapper is a great library helps developer to almost get rid of left hand right hand assignment between different types. In real world C# .net applications there are lots of scenarios developer wants to map different types of objects to each other. Normally it happens in system boundaries, like between UI and services or between ViewModel and Model. Without AutoMapper you have to do it for all properties every time you want to map to objects. This task is cumbersome, hard to test and bug prone. Automapper with does this for you with minimum cost. It has its own conventiones to facilitate type mapping. On top of built in defaults you as developer can teach Automapper new tricks.</p>
<p>You can find lots of blogs and articles of how to use AutoMapper. In fact <a href="https://github.com/AutoMapper/AutoMapper/wiki/Getting-started">this</a>, <a href="http://cpratt.co/using-automapper-getting-started/">this</a> and <a href="http://lostechies.com/jimmybogard/2009/01/23/automapper-the-object-object-mapper/">this</a> can help you.</p>
<p>Following is also an example of rather complex mapping between Source and Destination types.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14780314" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexpression_1-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="blog_AutomapperExpression_1.cs content, created by ahmad2x4 on 07:08AM on September 27, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="blog_AutomapperExpression_1.cs">
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexpression_1-cs-LC1" class="blob-code blob-code-inner js-file-line">public class Source</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexpression_1-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexpression_1-cs-LC3" class="blob-code blob-code-inner js-file-line">    public string FullName { get; set; }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexpression_1-cs-LC4" class="blob-code blob-code-inner js-file-line">    public int NumberOfYears { get; set; }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperexpression_1-cs-LC5" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperexpression_1-cs-LC6" class="blob-code blob-code-inner js-file-line">public class Destination</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperexpression_1-cs-LC7" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperexpression_1-cs-LC8" class="blob-code blob-code-inner js-file-line">    [Key]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperexpression_1-cs-LC9" class="blob-code blob-code-inner js-file-line">    public int UserId { get; set; }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperexpression_1-cs-LC10" class="blob-code blob-code-inner js-file-line">    public string Name { get; set; }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapperexpression_1-cs-LC11" class="blob-code blob-code-inner js-file-line">    public int Age { get; set; }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapperexpression_1-cs-LC12" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapperexpression_1-cs-LC13" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapperexpression_1-cs-LC14" class="blob-code blob-code-inner js-file-line">public class AutomapperQueriableTest</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapperexpression_1-cs-LC15" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapperexpression_1-cs-LC16" class="blob-code blob-code-inner js-file-line">    public AutomapperQueriableTest()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-blog_automapperexpression_1-cs-LC17" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-blog_automapperexpression_1-cs-LC18" class="blob-code blob-code-inner js-file-line">        Mapper.CreateMap&lt;Source, Destination&gt;()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-blog_automapperexpression_1-cs-LC19" class="blob-code blob-code-inner js-file-line">            .ForMember(d =&gt; d.Name, opt =&gt; opt.MapFrom(s =&gt; s.FullName))</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-blog_automapperexpression_1-cs-LC20" class="blob-code blob-code-inner js-file-line">            .ForMember(d =&gt; d.Age, opt =&gt; opt.MapFrom(s =&gt; s.NumberOfYears));</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-blog_automapperexpression_1-cs-LC21" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-blog_automapperexpression_1-cs-LC22" class="blob-code blob-code-inner js-file-line">        Mapper.CreateMap&lt;Destination, Source&gt;()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-blog_automapperexpression_1-cs-LC23" class="blob-code blob-code-inner js-file-line">            .ForMember(d =&gt; d.FullName, opt =&gt; opt.MapFrom(s =&gt; s.Name))</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-blog_automapperexpression_1-cs-LC24" class="blob-code blob-code-inner js-file-line">            .ForMember(d =&gt; d.NumberOfYears, opt =&gt; opt.MapFrom(s =&gt; s.Age));</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-blog_automapperexpression_1-cs-LC25" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-blog_automapperexpression_1-cs-LC26" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-blog_automapperexpression_1-cs-LC27" class="blob-code blob-code-inner js-file-line">    [Fact]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-blog_automapperexpression_1-cs-LC28" class="blob-code blob-code-inner js-file-line">    public void SimpleMappingTest()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-blog_automapperexpression_1-cs-LC29" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-blog_automapperexpression_1-cs-LC30" class="blob-code blob-code-inner js-file-line">        var fixture = new Fixture();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-blog_automapperexpression_1-cs-LC31" class="blob-code blob-code-inner js-file-line">        var source = fixture.Create&lt;Source&gt;();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-blog_automapperexpression_1-cs-LC32" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-blog_automapperexpression_1-cs-LC33" class="blob-code blob-code-inner js-file-line">        var destination = Mapper.Map&lt;Destination&gt;(source);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-blog_automapperexpression_1-cs-LC34" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-blog_automapperexpression_1-cs-LC35" class="blob-code blob-code-inner js-file-line">        Assert.Equal(source.FullName, destination.Name);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-blog_automapperexpression_1-cs-LC36" class="blob-code blob-code-inner js-file-line">        Assert.Equal(source.NumberOfYears, destination.Age);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-blog_automapperexpression_1-cs-LC37" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L38" class="blob-num js-line-number js-blob-rnum" data-line-number="38"></td>
          <td id="file-blog_automapperexpression_1-cs-LC38" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_1-cs-L39" class="blob-num js-line-number js-blob-rnum" data-line-number="39"></td>
          <td id="file-blog_automapperexpression_1-cs-LC39" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/acb5669edd84beb8aab2/raw/832b1d5ec29ec2a0728c00b43449ca1dcfe3abf4/blog_AutomapperExpression_1.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/acb5669edd84beb8aab2#file-blog_automapperexpression_1-cs" class="Link--inTextBlock">
          blog_AutomapperExpression_1.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>In this example I’ve used xUnit to test and AutoFixture to generate random data. Then in constructor of test class I have created mapping by using static method Mapper.CreateMap. this creates mapping at application domain context and you just need to set this up once and will be available across your application. After that in our test we created a source type and then mapped that to destination type finally checked the destination properties.</p>
<p>This post is not about normal scenario of using Automapper. When you are working with database you’d normally use repositories (arguably you might not need repository). in simple case of repository you would have following code</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14780615" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexpression_2-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="blog_AutomapperExpression_2.cs content, created by ahmad2x4 on 07:36AM on September 27, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="blog_AutomapperExpression_2.cs">
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexpression_2-cs-LC1" class="blob-code blob-code-inner js-file-line">public interface IRepository&lt;T&gt;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexpression_2-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexpression_2-cs-LC3" class="blob-code blob-code-inner js-file-line">    void Add(T obj);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexpression_2-cs-LC4" class="blob-code blob-code-inner js-file-line">    IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperexpression_2-cs-LC5" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/45ba0cf235a5e2a2ef1b/raw/2df324fa9fdf7a0da4520a1c4a096a01f9797791/blog_AutomapperExpression_2.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/45ba0cf235a5e2a2ef1b#file-blog_automapperexpression_2-cs" class="Link--inTextBlock">
          blog_AutomapperExpression_2.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This is a simple interface for repository. Implementation of this doesn&#8217;t even need mapping (you might do the mapping in other layers, though). However if we want to separate domain model from persistence model we could introduce another interface like following:</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14780615" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexpression_2-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="blog_AutomapperExpression_2.cs content, created by ahmad2x4 on 07:36AM on September 27, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="blog_AutomapperExpression_2.cs">
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexpression_2-cs-LC1" class="blob-code blob-code-inner js-file-line">public interface IRepository&lt;T&gt;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexpression_2-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexpression_2-cs-LC3" class="blob-code blob-code-inner js-file-line">    void Add(T obj);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexpression_2-cs-LC4" class="blob-code blob-code-inner js-file-line">    IQueryable&lt;T&gt; Find(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperexpression_2-cs-LC5" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/45ba0cf235a5e2a2ef1b/raw/2df324fa9fdf7a0da4520a1c4a096a01f9797791/blog_AutomapperExpression_2.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/45ba0cf235a5e2a2ef1b#file-blog_automapperexpression_2-cs" class="Link--inTextBlock">
          blog_AutomapperExpression_2.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>In this interface we have to generic types TI and TO. TI is our domain model and TO is our persistence model. For example we pass Source object to Add method but repository implementation would save that in Destination model (See bellow example as Add implementation)</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14780700" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexpression_2-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_automapperExpression_2.cs content, created by ahmad2x4 on 07:48AM on September 27, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_automapperExpression_2.cs">
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexpression_2-cs-LC1" class="blob-code blob-code-inner js-file-line">public void Add(Source source)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexpression_2-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexpression_2-cs-LC3" class="blob-code blob-code-inner js-file-line">    _context.Users.Add(Mapper.Map&lt;Destination&gt;(source));</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_2-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexpression_2-cs-LC4" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/cddb3921e5b672ff7f2a/raw/5f214ac48422d1f2138271977e996426972eea0d/Blog_automapperExpression_2.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/cddb3921e5b672ff7f2a#file-blog_automapperexpression_2-cs" class="Link--inTextBlock">
          Blog_automapperExpression_2.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This is possible if we map Source object to Destination object. Automapper does that for us.</p>
<p>Now, the real issue is when we want to implement Find method which accepts Expression&lt;Func&lt;Source, bool. Mapping an expression of func of type source to expression of func of type destination is more difficult as <a href="https://twitter.com/jbogard">Jimmy Bogard</a> main author of Automapper stated in this <a href="https://groups.google.com/forum/#!topic/automapper-users/oYxpR_f3Hls">mailing list</a>. Anyway, with a quick search I’ve found this question where somebody asked in <a href="http://stackoverflow.com/questions/7424501/automapper-for-funcs-between-selector-types">stackoverflow</a> about AutoMapper for Func’s between selector types. Person how answered this question updated the answer later with answer to Expression mapping with Automapper. The first class which does this magic is an implementation of ExpressionVisitor. This class can traverese an expression tree and replace a single parameter with an arbitrary expression</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14795542" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexpression_4-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_automapperexpression_4.cs content, created by ahmad2x4 on 12:42AM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_automapperexpression_4.cs">
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexpression_4-cs-LC1" class="blob-code blob-code-inner js-file-line">class ParameterReplacer : ExpressionVisitor</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexpression_4-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexpression_4-cs-LC3" class="blob-code blob-code-inner js-file-line">    private readonly ParameterExpression _parameter;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexpression_4-cs-LC4" class="blob-code blob-code-inner js-file-line">    private readonly Expression _replacement;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperexpression_4-cs-LC5" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperexpression_4-cs-LC6" class="blob-code blob-code-inner js-file-line">    private ParameterReplacer(ParameterExpression parameter, Expression replacement)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperexpression_4-cs-LC7" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperexpression_4-cs-LC8" class="blob-code blob-code-inner js-file-line">        _parameter = parameter;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperexpression_4-cs-LC9" class="blob-code blob-code-inner js-file-line">        _replacement = replacement;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperexpression_4-cs-LC10" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapperexpression_4-cs-LC11" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapperexpression_4-cs-LC12" class="blob-code blob-code-inner js-file-line">    public static Expression Replace(Expression expression, ParameterExpression parameter, Expression replacement)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapperexpression_4-cs-LC13" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapperexpression_4-cs-LC14" class="blob-code blob-code-inner js-file-line">        return new ParameterReplacer(parameter, replacement).Visit(expression);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapperexpression_4-cs-LC15" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapperexpression_4-cs-LC16" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-blog_automapperexpression_4-cs-LC17" class="blob-code blob-code-inner js-file-line">    protected override Expression VisitParameter(ParameterExpression parameter)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-blog_automapperexpression_4-cs-LC18" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-blog_automapperexpression_4-cs-LC19" class="blob-code blob-code-inner js-file-line">        if (parameter == _parameter)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-blog_automapperexpression_4-cs-LC20" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-blog_automapperexpression_4-cs-LC21" class="blob-code blob-code-inner js-file-line">            return _replacement;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-blog_automapperexpression_4-cs-LC22" class="blob-code blob-code-inner js-file-line">        }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-blog_automapperexpression_4-cs-LC23" class="blob-code blob-code-inner js-file-line">        return base.VisitParameter(parameter);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-blog_automapperexpression_4-cs-LC24" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexpression_4-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-blog_automapperexpression_4-cs-LC25" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/bce451d879bd731b2de5/raw/0e1f726c511302b45363758f97d8ce26826461c5/Blog_automapperexpression_4.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/bce451d879bd731b2de5#file-blog_automapperexpression_4-cs" class="Link--inTextBlock">
          Blog_automapperexpression_4.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>The second piece that comes into play is an extension method for Expression&lt;Func&lt;Z,Y&gt;&gt; called “Compose”  and it returns a new Expression&lt;Func&lt;X, Y&gt;&gt;.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14795586" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperexttesions_5-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_automapperexttesions_5.cs content, created by ahmad2x4 on 12:49AM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_automapperexttesions_5.cs">
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC1" class="blob-code blob-code-inner js-file-line">public static class FunctionCompositionExtensions</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC3" class="blob-code blob-code-inner js-file-line">    public static Expression&lt;Func&lt;TX, TY&gt;&gt; Compose&lt;TX, TY, TZ&gt;(this Expression&lt;Func&lt;TZ, TY&gt;&gt; outer, Expression&lt;Func&lt;TX, TZ&gt;&gt; inner)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC4" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC5" class="blob-code blob-code-inner js-file-line">        return Expression.Lambda&lt;Func&lt;TX, TY&gt;&gt;(</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC6" class="blob-code blob-code-inner js-file-line">            ParameterReplacer.Replace(outer.Body, outer.Parameters[0], inner.Body),</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC7" class="blob-code blob-code-inner js-file-line">            inner.Parameters[0]);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC8" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperexttesions_5-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperexttesions_5-cs-LC9" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/8f586c3e08a799ef792a/raw/a9d71d9a4be6fbf8555e9eb4b257401546198634/Blog_automapperexttesions_5.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/8f586c3e08a799ef792a#file-blog_automapperexttesions_5-cs" class="Link--inTextBlock">
          Blog_automapperexttesions_5.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>With this extension method and ExpressionVisitor Class I can complete the repository class for. This repository class is implementing SQL Serve implementation. Later on we will implement another repository class for MongoDB as well.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14798821" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapper_expression_6-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_Automapper_expression_6.cs content, created by ahmad2x4 on 07:55AM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_Automapper_expression_6.cs">
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapper_expression_6-cs-LC1" class="blob-code blob-code-inner js-file-line">public class SourceRepositorySql : IRepository&lt;Source, Destination&gt;, IDisposable</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapper_expression_6-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapper_expression_6-cs-LC3" class="blob-code blob-code-inner js-file-line">    private readonly MyContext _context;</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapper_expression_6-cs-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapper_expression_6-cs-LC5" class="blob-code blob-code-inner js-file-line">    public SourceRepositorySql(MyContext context)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapper_expression_6-cs-LC6" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapper_expression_6-cs-LC7" class="blob-code blob-code-inner js-file-line">        _context = context;</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapper_expression_6-cs-LC8" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapper_expression_6-cs-LC9" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapper_expression_6-cs-LC10" class="blob-code blob-code-inner js-file-line">    public void Add(Source source)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapper_expression_6-cs-LC11" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapper_expression_6-cs-LC12" class="blob-code blob-code-inner js-file-line">        _context.Users.Add(Mapper.Map&lt;Destination&gt;(source));</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapper_expression_6-cs-LC13" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapper_expression_6-cs-LC14" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapper_expression_6-cs-LC15" class="blob-code blob-code-inner js-file-line">    public IQueryable&lt;Destination&gt; Find(Expression&lt;Func&lt;Source, bool&gt;&gt; predicate)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapper_expression_6-cs-LC16" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-blog_automapper_expression_6-cs-LC17" class="blob-code blob-code-inner js-file-line">        Expression&lt;Func&lt;Destination, Source&gt;&gt; mapper = Mapper.Engine.CreateMapExpression&lt;Destination, Source&gt;();</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-blog_automapper_expression_6-cs-LC18" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-blog_automapper_expression_6-cs-LC19" class="blob-code blob-code-inner js-file-line">        Expression&lt;Func&lt;Destination, bool&gt;&gt; mappedSelector = predicate.Compose(mapper);</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-blog_automapper_expression_6-cs-LC20" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-blog_automapper_expression_6-cs-LC21" class="blob-code blob-code-inner js-file-line">        return _context.Users.Where(mappedSelector);</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-blog_automapper_expression_6-cs-LC22" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-blog_automapper_expression_6-cs-LC23" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-blog_automapper_expression_6-cs-LC24" class="blob-code blob-code-inner js-file-line">    private bool _disposed;</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-blog_automapper_expression_6-cs-LC25" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-blog_automapper_expression_6-cs-LC26" class="blob-code blob-code-inner js-file-line">    protected virtual void Dispose(bool disposing)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-blog_automapper_expression_6-cs-LC27" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
          <td id="file-blog_automapper_expression_6-cs-LC28" class="blob-code blob-code-inner js-file-line">        if (!_disposed)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
          <td id="file-blog_automapper_expression_6-cs-LC29" class="blob-code blob-code-inner js-file-line">        {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
          <td id="file-blog_automapper_expression_6-cs-LC30" class="blob-code blob-code-inner js-file-line">            if (disposing)</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
          <td id="file-blog_automapper_expression_6-cs-LC31" class="blob-code blob-code-inner js-file-line">            {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
          <td id="file-blog_automapper_expression_6-cs-LC32" class="blob-code blob-code-inner js-file-line">                _context.Dispose();</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
          <td id="file-blog_automapper_expression_6-cs-LC33" class="blob-code blob-code-inner js-file-line">            }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
          <td id="file-blog_automapper_expression_6-cs-LC34" class="blob-code blob-code-inner js-file-line">        }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
          <td id="file-blog_automapper_expression_6-cs-LC35" class="blob-code blob-code-inner js-file-line">        _disposed = true;</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L36" class="blob-num js-line-number js-blob-rnum" data-line-number="36"></td>
          <td id="file-blog_automapper_expression_6-cs-LC36" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L37" class="blob-num js-line-number js-blob-rnum" data-line-number="37"></td>
          <td id="file-blog_automapper_expression_6-cs-LC37" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L38" class="blob-num js-line-number js-blob-rnum" data-line-number="38"></td>
          <td id="file-blog_automapper_expression_6-cs-LC38" class="blob-code blob-code-inner js-file-line">    public void Dispose()</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L39" class="blob-num js-line-number js-blob-rnum" data-line-number="39"></td>
          <td id="file-blog_automapper_expression_6-cs-LC39" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L40" class="blob-num js-line-number js-blob-rnum" data-line-number="40"></td>
          <td id="file-blog_automapper_expression_6-cs-LC40" class="blob-code blob-code-inner js-file-line">        Dispose(true);</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L41" class="blob-num js-line-number js-blob-rnum" data-line-number="41"></td>
          <td id="file-blog_automapper_expression_6-cs-LC41" class="blob-code blob-code-inner js-file-line">        GC.SuppressFinalize(this);</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L42" class="blob-num js-line-number js-blob-rnum" data-line-number="42"></td>
          <td id="file-blog_automapper_expression_6-cs-LC42" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapper_expression_6-cs-L43" class="blob-num js-line-number js-blob-rnum" data-line-number="43"></td>
          <td id="file-blog_automapper_expression_6-cs-LC43" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/63b6c37597c105bfccbe/raw/a83b8ea525ffe7c605b667aab8f6a36f4e5fdcf4/Blog_Automapper_expression_6.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/63b6c37597c105bfccbe#file-blog_automapper_expression_6-cs" class="Link--inTextBlock">
          Blog_Automapper_expression_6.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This is the Entity Framework implementation of our repository. Interesting point is “Find” Method which is taking advantage of Mapper.Engine.CreateMapExpression and Compose extension method. CreateMapExpression in AutoMapper creates an expression for mapping between Source to Destination. In the next line we use the mapper expression and pass this to Compose extension method to build a new expression. And finally we call Where method on Users DBSet and it returns IQueriable of type Destination. I have written a Unit Test to test this.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14801740" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperextension_7-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="blog_automapperextension_7.cs content, created by ahmad2x4 on 11:55AM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="blog_automapperextension_7.cs">
        <tr>
          <td id="file-blog_automapperextension_7-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperextension_7-cs-LC1" class="blob-code blob-code-inner js-file-line">[Fact]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperextension_7-cs-LC2" class="blob-code blob-code-inner js-file-line">[UseDatabase]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperextension_7-cs-LC3" class="blob-code blob-code-inner js-file-line">public void ShouldConvertPredicates()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperextension_7-cs-LC4" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperextension_7-cs-LC5" class="blob-code blob-code-inner js-file-line">    var connStr = ConfigurationManager.ConnectionStrings[&quot;SQLExpressConnection&quot;].ConnectionString;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperextension_7-cs-LC6" class="blob-code blob-code-inner js-file-line">    </td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperextension_7-cs-LC7" class="blob-code blob-code-inner js-file-line">    var myContext = new MyContext(connStr);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperextension_7-cs-LC8" class="blob-code blob-code-inner js-file-line">    using (var repository = new SourceRepositorySql(myContext))</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperextension_7-cs-LC9" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperextension_7-cs-LC10" class="blob-code blob-code-inner js-file-line">        AddRecords(repository);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapperextension_7-cs-LC11" class="blob-code blob-code-inner js-file-line">        myContext.SaveChanges();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapperextension_7-cs-LC12" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapperextension_7-cs-LC13" class="blob-code blob-code-inner js-file-line">        const string name = &quot;Ahmadreza&quot;;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapperextension_7-cs-LC14" class="blob-code blob-code-inner js-file-line">        var query = repository.Find(s =&gt; s.FullName == name &amp;&amp; s.NumberOfYears &gt; 10);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapperextension_7-cs-LC15" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapperextension_7-cs-LC16" class="blob-code blob-code-inner js-file-line">        var result = query.Project().To&lt;Source&gt;().ToList();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-blog_automapperextension_7-cs-LC17" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-blog_automapperextension_7-cs-LC18" class="blob-code blob-code-inner js-file-line">        Assert.Equal(1, result.Count);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-blog_automapperextension_7-cs-LC19" class="blob-code blob-code-inner js-file-line">        Assert.Equal(name, result.First().FullName);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-blog_automapperextension_7-cs-LC20" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-blog_automapperextension_7-cs-LC21" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-blog_automapperextension_7-cs-LC22" class="blob-code blob-code-inner js-file-line">private static void AddRecords(IRepository&lt;Source, Destination&gt; repository)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-blog_automapperextension_7-cs-LC23" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-blog_automapperextension_7-cs-LC24" class="blob-code blob-code-inner js-file-line">    repository.Add(new Source { FullName = &quot;Ahmadreza&quot;, NumberOfYears = 35 });</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-blog_automapperextension_7-cs-LC25" class="blob-code blob-code-inner js-file-line">    repository.Add(new Source { FullName = &quot;Ahmadreza&quot;, NumberOfYears = 9 });</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-blog_automapperextension_7-cs-LC26" class="blob-code blob-code-inner js-file-line">    repository.Add(new Source { FullName = &quot;Jon&quot;, NumberOfYears = 20 });</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_7-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-blog_automapperextension_7-cs-LC27" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/ae925ff32eeec9685cf1/raw/fe598c36bb986dc63b2b8fe4a9a3f24a3ac77c98/blog_automapperextension_7.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/ae925ff32eeec9685cf1#file-blog_automapperextension_7-cs" class="Link--inTextBlock">
          blog_automapperextension_7.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This test successfully passes. As you see I used Source type to add records and query the records. The repository maps the add part to destination and also map predicates expression.</p>
<p>When I debug the code I can see entity framework successfully constructed the query. This is using Destination type.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14801792" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperextension_07-sql" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-tsql  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_automapperextension_07.sql content, created by ahmad2x4 on 12:00PM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_automapperextension_07.sql">
        <tr>
          <td id="file-blog_automapperextension_07-sql-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperextension_07-sql-LC1" class="blob-code blob-code-inner js-file-line">SELECT </td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_07-sql-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperextension_07-sql-LC2" class="blob-code blob-code-inner js-file-line">    [Extent1].[UserId] AS [UserId], </td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_07-sql-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperextension_07-sql-LC3" class="blob-code blob-code-inner js-file-line">    [Extent1].[Name] AS [Name], </td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_07-sql-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperextension_07-sql-LC4" class="blob-code blob-code-inner js-file-line">    [Extent1].[Age] AS [Age]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_07-sql-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperextension_07-sql-LC5" class="blob-code blob-code-inner js-file-line">    FROM [dbo].[Destinations] AS [Extent1]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_07-sql-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperextension_07-sql-LC6" class="blob-code blob-code-inner js-file-line">    WHERE (N&#39;Ahmadreza&#39; = [Extent1].[Name]) AND ([Extent1].[Age] &gt; 10)</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/74257a9267cd9466f9b4/raw/893b56d5991adada76288079b6adfbfd2976529e/Blog_automapperextension_07.sql" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/74257a9267cd9466f9b4#file-blog_automapperextension_07-sql" class="Link--inTextBlock">
          Blog_automapperextension_07.sql
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>Well this is really good, although there is a down side to this approach. This is good and pretty mych abstracted the Destination class and I just work with Source type. However, if I want to join results of two different IQueriables I have to mention the property name of the Model.</p>
<p>Anyway it worked reasonably good with SQL server and EntityFramework. Let see if it works with MongoDB.<br />
In order to connect to MongoDB I use mongo csharp driver and on top of that I’m using <a href="http://mongorepository.codeplex.com">MongoRepository</a>. Now let see if the same scenario works for Repository Implemented for MongoDb.</p>
<p>In following code I have implemented the IRepository for MongoDB.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14802019" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperextension_8-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_AutomapperExtension_8.cs content, created by ahmad2x4 on 12:20PM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_AutomapperExtension_8.cs">
        <tr>
          <td id="file-blog_automapperextension_8-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperextension_8-cs-LC1" class="blob-code blob-code-inner js-file-line">public class SourceRepositoryMongo : IRepository&lt;Source, Destination&gt;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperextension_8-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperextension_8-cs-LC3" class="blob-code blob-code-inner js-file-line">    private readonly MongoRepository&lt;Destination&gt; _mongoRepository;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperextension_8-cs-LC4" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperextension_8-cs-LC5" class="blob-code blob-code-inner js-file-line">    public SourceRepositoryMongo()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperextension_8-cs-LC6" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperextension_8-cs-LC7" class="blob-code blob-code-inner js-file-line">        _mongoRepository = new MongoRepository&lt;Destination&gt;();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperextension_8-cs-LC8" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperextension_8-cs-LC9" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperextension_8-cs-LC10" class="blob-code blob-code-inner js-file-line">    public void Add(Source obj)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapperextension_8-cs-LC11" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapperextension_8-cs-LC12" class="blob-code blob-code-inner js-file-line">        _mongoRepository.Add(Mapper.Map&lt;Destination&gt;(obj));</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapperextension_8-cs-LC13" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapperextension_8-cs-LC14" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapperextension_8-cs-LC15" class="blob-code blob-code-inner js-file-line">    public IQueryable&lt;Destination&gt; Find(Expression&lt;Func&lt;Source, bool&gt;&gt; predicate)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapperextension_8-cs-LC16" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
          <td id="file-blog_automapperextension_8-cs-LC17" class="blob-code blob-code-inner js-file-line">        Expression&lt;Func&lt;Destination, Source&gt;&gt; mapper = Mapper.Engine.CreateMapExpression&lt;Destination, Source&gt;();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
          <td id="file-blog_automapperextension_8-cs-LC18" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
          <td id="file-blog_automapperextension_8-cs-LC19" class="blob-code blob-code-inner js-file-line">        Expression&lt;Func&lt;Destination, bool&gt;&gt; mappedSelector = predicate.Compose(mapper);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
          <td id="file-blog_automapperextension_8-cs-LC20" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
          <td id="file-blog_automapperextension_8-cs-LC21" class="blob-code blob-code-inner js-file-line">        return _mongoRepository.Where(mappedSelector);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
          <td id="file-blog_automapperextension_8-cs-LC22" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
          <td id="file-blog_automapperextension_8-cs-LC23" class="blob-code blob-code-inner js-file-line">    public IQueryable&lt;Destination&gt; Find(Expression&lt;Func&lt;Destination, bool&gt;&gt; predicate)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
          <td id="file-blog_automapperextension_8-cs-LC24" class="blob-code blob-code-inner js-file-line">    {</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
          <td id="file-blog_automapperextension_8-cs-LC25" class="blob-code blob-code-inner js-file-line">        return _mongoRepository.Where(predicate);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
          <td id="file-blog_automapperextension_8-cs-LC26" class="blob-code blob-code-inner js-file-line">    }</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_8-cs-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
          <td id="file-blog_automapperextension_8-cs-LC27" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/3d4269858753f6e01428/raw/7106e4ccdc363dfe85233d264b2e1d954fd53554/Blog_AutomapperExtension_8.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/3d4269858753f6e01428#file-blog_automapperextension_8-cs" class="Link--inTextBlock">
          Blog_AutomapperExtension_8.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>The target is to test Find(Expression&lt;Func&lt;Source, bool&gt;&gt; predicate) and see if it works, however, I have added another Find with Find(Expression&lt;Func&lt;Destination, bool&gt;&gt; predicate) signature and I will describe why I added that method.</p>
<p>Now the unit test to test this implementation.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14802093" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperextension_9-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_Automapperextension_9.cs content, created by ahmad2x4 on 12:25PM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_Automapperextension_9.cs">
        <tr>
          <td id="file-blog_automapperextension_9-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperextension_9-cs-LC1" class="blob-code blob-code-inner js-file-line">[Fact]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperextension_9-cs-LC2" class="blob-code blob-code-inner js-file-line">[UseMongo]</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperextension_9-cs-LC3" class="blob-code blob-code-inner js-file-line">public void MongoTest()</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperextension_9-cs-LC4" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperextension_9-cs-LC5" class="blob-code blob-code-inner js-file-line">    var repositoryMongo = new SourceRepositoryMongo();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperextension_9-cs-LC6" class="blob-code blob-code-inner js-file-line">    AddRecords(repositoryMongo);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperextension_9-cs-LC7" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperextension_9-cs-LC8" class="blob-code blob-code-inner js-file-line">    var name = &quot;Ahmadreza&quot;;</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperextension_9-cs-LC9" class="blob-code blob-code-inner js-file-line">    //var query = repositoryMongo.Find(s =&gt; s.Name == name &amp;&amp; s.Age &gt; 10);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperextension_9-cs-LC10" class="blob-code blob-code-inner js-file-line">    var query = repositoryMongo.Find(s =&gt; s.FullName == name &amp;&amp; s.NumberOfYears &gt; 10);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
          <td id="file-blog_automapperextension_9-cs-LC11" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
          <td id="file-blog_automapperextension_9-cs-LC12" class="blob-code blob-code-inner js-file-line">    var result = query.Project().To&lt;Source&gt;();</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
          <td id="file-blog_automapperextension_9-cs-LC13" class="blob-code blob-code-inner js-file-line">
</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
          <td id="file-blog_automapperextension_9-cs-LC14" class="blob-code blob-code-inner js-file-line">    Assert.Equal(1, result.Count());</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
          <td id="file-blog_automapperextension_9-cs-LC15" class="blob-code blob-code-inner js-file-line">    Assert.Equal(name, result.First().FullName);</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension_9-cs-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
          <td id="file-blog_automapperextension_9-cs-LC16" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/ec769fe5d372116ad0c2/raw/cba1babffdc956928e2f2203a297e16fcfbf0576/Blog_Automapperextension_9.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/ec769fe5d372116ad0c2#file-blog_automapperextension_9-cs" class="Link--inTextBlock">
          Blog_Automapperextension_9.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This test fails!. This complains about full name not having serialization information</p>
<pre class="csharpcode">Additional information: Unable to determine the serialization information for the expression: <span class="kwrd">&lt;</span><span class="html">MemberInitExpression</span><span class="kwrd">&gt;</span>.FullName.</pre>
<p>This approach doesn&#8217;t work for MongoDB (probably Mongo CSharp driver could not handle that). I have written another method that accepts Expression&lt;Func&lt;Destination, bool&gt;&gt; predicate, If you uncomment this method call and comment the other in unit test it’ll work.</p>
<p>Having looked at transformed expression shows that this approach does not completely re-write the expression.</p>
<style>.gist table { margin-bottom: 0; }</style><div style="tab-size: 8" id="gist14802402" class="gist">
    <div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
      <div class="gist-data">
        
<div class="js-gist-file-update-container js-task-list-container">
      <div id="file-blog_automapperextension10-cs" class="file my-2">
    
    <div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-c  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Blog_AutomapperExtension10.cs content, created by ahmad2x4 on 12:44PM on September 28, 2014."
    >

        
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">

  <template class="js-file-alert-template">
  <div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
    <span>
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a>
    </span>


  <div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters
</a>
</div>
</div></template>
<template class="js-line-alert-template">
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e">
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg>
</span></template>

  <table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Blog_AutomapperExtension10.cs">
        <tr>
          <td id="file-blog_automapperextension10-cs-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
          <td id="file-blog_automapperextension10-cs-LC1" class="blob-code blob-code-inner js-file-line">.Lambda #Lambda1&lt;System.Func`2[AutomapperQuesriableTest.Destination,System.Boolean]&gt;(AutomapperQuesriableTest.Destination $dto)</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
          <td id="file-blog_automapperextension10-cs-LC2" class="blob-code blob-code-inner js-file-line">{</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
          <td id="file-blog_automapperextension10-cs-LC3" class="blob-code blob-code-inner js-file-line">    (.New AutomapperQuesriableTest.Source(){</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
          <td id="file-blog_automapperextension10-cs-LC4" class="blob-code blob-code-inner js-file-line">        FullName = $dto.Name,</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
          <td id="file-blog_automapperextension10-cs-LC5" class="blob-code blob-code-inner js-file-line">        NumberOfYears = $dto.Age</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
          <td id="file-blog_automapperextension10-cs-LC6" class="blob-code blob-code-inner js-file-line">    }).FullName == &quot;ahmadreza&quot; &amp;&amp; (.New AutomapperQuesriableTest.Source(){</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
          <td id="file-blog_automapperextension10-cs-LC7" class="blob-code blob-code-inner js-file-line">        FullName = $dto.Name,</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
          <td id="file-blog_automapperextension10-cs-LC8" class="blob-code blob-code-inner js-file-line">        NumberOfYears = $dto.Age</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
          <td id="file-blog_automapperextension10-cs-LC9" class="blob-code blob-code-inner js-file-line">    }).NumberOfYears &gt; 10</td>
        </tr>
        <tr>
          <td id="file-blog_automapperextension10-cs-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
          <td id="file-blog_automapperextension10-cs-LC10" class="blob-code blob-code-inner js-file-line">}</td>
        </tr>
  </table>
</div>


    </div>

  </div>

</div>

      </div>
      <div class="gist-meta">
        <a href="https://gist.github.com/ahmad2x4/ed12e9e4538646cfaf5e/raw/41dc925027b6972ec10ea18a354a8684023fb11f/Blog_AutomapperExtension10.cs" style="float:right" class="Link--inTextBlock">view raw</a>
        <a href="https://gist.github.com/ahmad2x4/ed12e9e4538646cfaf5e#file-blog_automapperextension10-cs" class="Link--inTextBlock">
          Blog_AutomapperExtension10.cs
        </a>
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
    </div>
</div>

<p>This is just wrapper around another lambda that does the mapping. Interestingly EntityFramework handles that very well but MongoDB CSharp Driver not.</p>
<p>On option it to find a way to rewrite the whole expression based on the mapping configuration. But as Jimmy mentioned it could be quite hard to implement.</p>
<p>You can find the full implementation and tests <a href="https://gist.github.com/ahmad2x4/90d341b36f94325d596e">here</a></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">258</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>
	</item>
		<item>
		<title>Value type field members are in heap</title>
		<link>https://ahmadrezaa.wordpress.com/2014/01/31/value-type-field-members-are-in-heap/</link>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Thu, 30 Jan 2014 13:15:44 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[ValueType]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=250</guid>

					<description><![CDATA[I think developers are a bit confused about the concept of ValueTypes and Reference Type. Often I hear that all primitive data types and value types are in stack. First of all it is thread’s stack and it is not a single stack for application. Secondly, when we say value types are in stack it [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I think developers are a bit confused about the concept of ValueTypes and Reference Type. Often I hear that all primitive data types and value types are in stack. First of all it is thread’s stack and it is not a single stack for application. Secondly, when we say value types are in stack it doesn&#8217;t include field members of the class. Only method parameters and local variable which are value type will be in the thread’s stat. Field members, even if they are value type, will be in heap. Consider following example:</p>
<pre class="brush: csharp; title: ; notranslate">

using System;

namespace ClassLibrary
{
    public class Foo
    {
	public int number = 0;
        public int Bar(int x, int y)
        {
         	int sum = x + y + number
	 	return sum;
        }
    }
}
</pre>
<p>In this example, only x, y, sum and return value are in thread&#8217;s stack and number variable will be held in heap as part of object instance and is accessible using this keyword (which compiler let you omit that).<br />
In other word when we say object instance is created in the heap, what does really stored there? Field members are object state that needs to be stored in object instance.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">250</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>
	</item>
		<item>
		<title>IL superset of all .net languages</title>
		<link>https://ahmadrezaa.wordpress.com/2014/01/22/il-superset-of-all-net-languages/</link>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Wed, 22 Jan 2014 12:14:53 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[IL]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=215</guid>

					<description><![CDATA[In .net world you can choose your language and write your application with best language which matches your requirements. Different languages has different paradigm. For example C# started with static/strongly type paragoge&#160; but over years it has been evolved and now it does support functional programming as well as dynamic. There are many other languages [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>In .net world you can choose your language and write your application with best language which matches your requirements. Different languages has different paradigm. For example C# started with static/strongly type paragoge&#160; but over years it has been evolved and now it does support functional programming as well as dynamic. There are many other languages like F#, Visual Basic and fortran. </p>
<p>It is compiler responsibility to generate assembly with proper intermediate language so that CLR (Common Language Runtime) would be able to generate native code (using JIT) and run at run time. Each compilers support subset if intermediate language and provide its flavour using syntactic sugars in the language.</p>
<p><a href="https://ahmadrezaa.wordpress.com/wp-content/uploads/2014/01/netlanguages.png"><img title=".Net Languages" style="float:none;margin-left:auto;display:block;margin-right:auto;border-width:0;" border="0" alt=".Net Languages" src="https://ahmadrezaa.wordpress.com/wp-content/uploads/2014/01/netlanguages_thumb.png?w=487&#038;h=484" width="487" height="484" /></a> </p>
</p>
<p>CLR allows language integration as long as written code comply with Common Language Specification. It means because of differences between languages, if you want to use types generated by other languages, you need to use certain feature of language that are guaranteed to be available in other languages. CLS is the lowest common denominator of language features provided by Microsoft.</p>
<p>However, today I don&#8217;t want to highlight CLS, instead I want to mentions that Intermediate Language in some cases has more features which languages like C# didn&#8217;t bother to implement. </p>
<p>For example we all know about access modifiers in C#. There is public, private, internal and protected</p>
<table cellspacing="0" cellpadding="2" width="522" border="2">
<tbody>
<tr>
<td valign="top" width="126">Access Modifiers</td>
<td valign="top" width="392">Description</td>
</tr>
<tr>
<td valign="top" width="129">public</td>
<td valign="top" width="389"><em>public access is the most permissive access level. There is no restrictions on accessing public members</em></td>
</tr>
<tr>
<td valign="top" width="132">private</td>
<td valign="top" width="387"><em>private access is the least permissive access level, private members are accessible only within the body of the class or the struct in which they are declared</em></td>
</tr>
<tr>
<td valign="top" width="134">internal</td>
<td valign="top" width="386"><em>internal types or members are accessible only within files in the same assembly</em></td>
</tr>
<tr>
<td valign="top" width="135">protected</td>
<td valign="top" width="385"><em>A protected member is accessible within its class and by derived class instances</em></td>
</tr>
<tr>
<td valign="top" width="136">protected internal</td>
<td valign="top" width="384">visible to derived classes and those of the same assembly</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>OK, now lets have a look on access modifiers in IL</p>
<table cellspacing="0" cellpadding="2" width="520" border="2">
<tbody>
<tr>
<td valign="top" width="136">IL Access Modifier</td>
<td valign="top" width="250">Description</td>
<td valign="top" width="130">C# equivalent</td>
</tr>
<tr>
<td valign="top" width="138">Private</td>
<td valign="top" width="248">The member is accessible only by other members in the same class or struct</td>
<td valign="top" width="131">private</td>
</tr>
<tr>
<td valign="top" width="139">Family</td>
<td valign="top" width="247">The member is accessible by derived types.</td>
<td valign="top" width="131">protected</td>
</tr>
<tr>
<td valign="top" width="140">famandassem</td>
<td valign="top" width="246">The member is accessible by derived types, but only if the derived type is defined in the same assembly.</td>
<td valign="top" width="131">N/A</td>
</tr>
<tr>
<td valign="top" width="141">Assembly</td>
<td valign="top" width="246">The member is accessible by any code in the same assembly</td>
<td valign="top" width="131">internal</td>
</tr>
<tr>
<td valign="top" width="141">famorassem</td>
<td valign="top" width="246">The member is accessible by derived types in any assembly and also by any types within same assembly</td>
<td valign="top" width="131">protected internal</td>
</tr>
<tr>
<td valign="top" width="141">Public</td>
<td valign="top" width="246">The member is accessible by any code in any assembly</td>
<td valign="top" width="132">public</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p>As you might have noticed there is no C# equivalent for family or assembly although IL has this keyword. there is not such a thing in VB.Net as well. Now lets have a closer look on a simple sample and generated IL code.</p>
<p>Assume we have two simple project in a solution. A Console Application and a Class Library which console application referenced that. In the Class Library we have a simple class called Super and in console application, beside Program class we have a class derived from Super class called Sub. Lets have a look on their codes.</p>
<p>&#160;</p>
<p>Super class source code</p>
<p>  <PRE></p>
<pre class="brush: csharp; title: ; notranslate">

using System;

namespace ClassLibrary
{
    public class Super
    {
        protected internal void Foo()
        {
            Console.WriteLine(&quot;Foo&quot;);
        }

        internal void Method()
        {
            
        }
    }
}

</pre>
<p></PRE></p>
<p>Other class in ClassLibrary Project</p>
<p>&#160;</p>
<p><PRE></p>
<pre class="brush: csharp; title: ; notranslate">
namespace ClassLibrary
{
    public class Other
    {
        public void Test()
        {
            var super = new Super();
            super.Foo();
        }
        
    }
}
</pre>
<p></PRE></p>
<p>&#160;</p>
<p>Sub class in ConsoleApplication</p>
<p>&#160;</p>
<p><PRE></p>
<pre class="brush: csharp; title: ; notranslate">
using System;
using ClassLibrary;

namespace ConsoleApplication
{
    public class Sub : Super
    {
        public void Bar()
        {
            Foo();
            Console.WriteLine(&quot;Bar&quot;);
        }
    }
}
</pre>
<p></PRE></p>
<p>&#160;</p>
<p>Program.cs class in ConsoleApplication</p>
<p><PRE></p>
<pre class="brush: csharp; title: ; notranslate">
using System;

namespace ConsoleApplication
{
    class Program
    {
        static void Main()
        {
            var sub = new Sub();
            sub.Bar();
            Console.ReadLine();
            
        }
    }
}
</pre>
<p></PRE></p>
<p>Sub class simply calls Foo method which is defined as “protected internal” which in C# means family <u>or </u>assembly in IL. If you build and run application you’ll see that Sub class successfully accesses because although it is not in the same assembly but But sub is actually derived from Super class. the output will be </p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
Foo 
Bar
</pre>
<p></PRE></p>
<p>Cool, now let mess around with the IL and see that would happen if we change that.</p>
<p>If you want to dump the IL of an assembly you can simply open a Visual Studio Command and then go to the folder your assembly exist and run ildasm with following parameters</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">

ildasm ConsoleApplication.exe /out:ConsoleApplication.il

ildasm ClassLibrary.dll /out:ClassLibrary.il

</pre>
<p></PRE></p>
<p>Now, you can open ClassLibrary.il and have a look into generated code. Actually it is a bit long so I just copied part that I’m interested in which is Foo method </p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
  .method famandassem hidebysig instance void 
          Foo() cil managed
  {
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      &quot;Foo&quot;
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Super::Foo
</pre>
<p></PRE></p>
<p>You see that protected internal compiled to famorassem. What would happen if we change this to famandassem and assemble this IL again? Answer is CLR will prevent calling this method and will raise MethodAccessException exception when we call this from console application. But let put this into action and actually test this. So, change the famorassem to famandassem and save the file and use ilasm.exe from Visual Studio Command to assemble this code again. Before assembling this you can delete current ClassLibrary.Dll. To assemble use following command parameters:</p>
<p>&#160;</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
ilasm ClassLibrary.il /out:ClassLibrary.dll /dll
</pre>
<p></PRE></p>
<p>If you get Operation completed successfully at the end new class library is generated. If you run ConsoleApplication.exe you’ll get following exception because of violating the access level defined in the IL</p>
<p>&#160;</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
Unhandled Exception: System.MethodAccessException: Attempt by method 'ConsoleApp
lication.Sub.Bar()' to access method 'ClassLibrary.Super.Foo()' failed.
   at ConsoleApplication.Sub.Bar() in f:\Users\ara\Documents\Visual Studio 2013\
Projects\ConsoleApplication3\ConsoleApplication3\Sub.cs:line 10
   at ConsoleApplication.Program.Main() in f:\Users\ara\Documents\Visual Studio
2013\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 10
</pre>
<p></PRE></p>
<p>&#160;</p>
<p>famandassem means that only derived class within same assembly will have access to that member. Just for the sake of testing lets Super to the ConsoleApplication and see if it works.</p>
<p>Open ClassLIbrary.il in a text editor. Copy following IL code which is Super class il code</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit ClassLibrary.Other
       extends [mscorlib]System.Object
{
  .method public hidebysig instance void 
          Test() cil managed
  {
    // Code size       15 (0xf)
    .maxstack  1
    .locals init ([0] class ClassLibrary.Super super)
    IL_0000:  nop
    IL_0001:  newobj     instance void ClassLibrary.Super::.ctor()
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  callvirt   instance void ClassLibrary.Super::Foo()
    IL_000d:  nop
    IL_000e:  ret
  } // end of method Other::Test

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Other::.ctor

} // end of class ClassLibrary.Other

.class public auto ansi beforefieldinit ClassLibrary.Super
       extends [mscorlib]System.Object
{
  .method famandassem hidebysig instance void 
          Foo() cil managed
  {
    // Code size       13 (0xd)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ldstr      &quot;Foo&quot;
    IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000b:  nop
    IL_000c:  ret
  } // end of method Super::Foo

  .method assembly hidebysig instance void 
          Method() cil managed
  {
    // Code size       2 (0x2)
    .maxstack  8
    IL_0000:  nop
    IL_0001:  ret
  } // end of method Super::Method

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method Super::.ctor

} // end of class ClassLibrary.Super


// =============================================================

</pre>
<p></PRE></p>
<p>Now open ConsoleApplication.il and paste this code at the end after end of Class ConsoleApplication.Sub. </p>
<p>We need to do one more thing and get rid off reference to ClassLibrary.il which we don&#8217;t need anymore. So right in the beginning there is some code for that. Delete that piece of code and save the file.</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
.assembly extern ClassLibrary
{
  .ver 1:0:0:0
}
</pre>
<p></PRE></p>
<p>One more thing to do and it is replacing all [ClassLibrary] with empty string.</p>
<p>Delete console application and assemble the il to make new ConsoleApplication.exe using following command</p>
<p><PRE></p>
<pre class="brush: plain; title: ; notranslate">
ilasm ConsoleApplication.il /exe /out:ConsoleApplication.exe
</pre>
<p></PRE></p>
<p>Now we have new ConsoleApplication which doesn&#8217;t have reference to ClassLibrary. Run ConsoleApplication.exe you see it runs successfully.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">215</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>

		<media:content url="https://ahmadrezaa.wordpress.com/wp-content/uploads/2014/01/netlanguages_thumb.png" medium="image">
			<media:title type="html">.Net Languages</media:title>
		</media:content>
	</item>
		<item>
		<title>Quick Note: Memory Leak Or Missing Configuration</title>
		<link>https://ahmadrezaa.wordpress.com/2011/11/14/quick-note-memory-leak-or-missing-configuration/</link>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Mon, 14 Nov 2011 06:24:01 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[SQL Server]]></category>
		<guid isPermaLink="false">http://ahmadreza.com/?p=203</guid>

					<description><![CDATA[Imagine you have multiple instances of SQL server on your server and one of them is using almost all of available memory and second one is facing memory shortage. What are possible Cause for this symptom. Does one of application have memory leak? The answer is NO, When you install multiple instances of SQL server on a [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Imagine you have multiple instances of SQL server on your server and one of them is using almost all of available memory and second one is facing memory shortage. What are possible Cause for this symptom. Does one of application have memory leak?</p>
<p>The answer is <strong>NO</strong>, When you install multiple instances of SQL server on a single server you have to consider memory allocation for each instances because windows does not balance memory across applications with the memory notification.</p>
<p>The first instance with a work load will  used huge portion of memory (Especially when you have actual data &#8211; not testing &#8211; on that instance).</p>
<p>Three approaches are available for <a href="http://msdn.microsoft.com/en-us/library/ms178067.aspx">Server Memory Option</a> documented in the section &#8220;Sunning Multiple Instances of SQL server&#8221; and if you have selected third one which is &#8220;Do nothing&#8221;, you might have same problem.</p>
<blockquote>
<ul>
<li><em>Do nothing (not recommended). The first instances presented with a workload will tend to allocate all of memory. Idle instances or instances started later may end up running with only a minimal amount of memory available. SQL Server makes no attempt to balance memory usage across instances. All instances will, however, respond to Windows Memory Notification signals to adjust the size of their buffer pools. As of Windows Server 2003 SP1, Windows does not balance memory across applications with the Memory Notification API. It merely provides global feedback as to the availability of memory on the system.</em></li>
</ul>
</blockquote>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">203</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>
	</item>
		<item>
		<title>How to sign the XBAP with your own certificate</title>
		<link>https://ahmadrezaa.wordpress.com/2011/05/20/how-to-sign-the-xbap-with-your-own-certificate/</link>
					<comments>https://ahmadrezaa.wordpress.com/2011/05/20/how-to-sign-the-xbap-with-your-own-certificate/#comments</comments>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Thu, 19 May 2011 23:30:39 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[XBAP]]></category>
		<guid isPermaLink="false">http://ahmadreza.com/?p=171</guid>

					<description><![CDATA[I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><em>I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type of projects is different in other versions of IIS and .Net frameworks and they are not is subject of this post series.</em></p>
<ol>
<li><a title="How to create a simple Browser Enabled WPF application" href="https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/">How to create a simple Browser Enables WPF application</a></li>
<li><a title="How to host a windows form application inside XBAP" href="http://ahmadreza.com/2011/05/17/how-to-host-a-windows-form-application-inside-xbap/">How to host a windows form application inside XBAP</a></li>
<li>How to sign the XBAP with your own certificate</li>
</ol>
<div><strong>Note: Making a browser enabled application as full trust according to this method is not completely secured. This can be used for testing purposes in testing environments. Please select proper certificates and known trusted root certification authorities.</strong></div>
<p>Before starting I think its better to have same understanding of file extension that we are going to talk about.</p>
<p><strong>.cer file: </strong>Apublic key which is given by Certificate Authority</p>
<p><strong>.pvk file: </strong>This file is your private key and should keep it confidential</p>
<p><strong>.pfx file: </strong>This is a Personal Information Exchange file and again you should keep it confidential because it contains</p>
<p>We have created a simple WPF Browser application and a simple windows application which is hosted inside the XBAP application. When we created WPF application Visual Studio automatically create a .pfx (Which is used for signing ClickOnce manifest).</p>
<p>To create your own certificate you need to follow these steps:</p>
<p><strong>Step 1:</strong> Creating your key pairs (Public and Private)</p>
<p>open Visual Studio Command Prompt (2010) and then goto your application path and type following command</p>
<pre class="brush: plain; title: ; notranslate">

makecert -n &quot;CN=Your Company Name&quot; -r -sv Key.pvk Key.cer

</pre>
<p>A password dialog box will be displayed and you set your own password. This command creates two files one private key and one certificate.</p>
<p><strong>Step2:</strong> Then you need to create PFX file which is used for signing ClickOnce manifest and contains both private and public key.</p>
<pre class="brush: plain; title: ; notranslate">

pvk2pfx.exe -pvk Key.pvk -spc Key.cer -pfx KeyPFX.pfx -po [password]

</pre>
<p>Put your own password as [password] and this command will create a PFX file</p>
<p><strong>Step 3:</strong> Back to the solution explorer delete &#8220;SimpleBrowserApplication_TemporaryKey.pfx&#8221; and goto Application property page and select signing tab. Click on &#8220;Select from file&#8221; and select the PFX file you have just created.</p>
<p><a href="http://gkasoq.bay.livefilestore.com/y1pFsKJJV5SbKmeWkxSC7iJ-g2laeGSY_VkVN5ZGWCPeku6dJKdnN4sDdWnqkM3zLpSX-39bZVQE0MS7sb7ZGyYetm_jBRPfPoO/01SignTheClickOnce.png?psid=1"><img class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pFsKJJV5SbKmeWkxSC7iJ-g2laeGSY_VkVN5ZGWCPeku6dJKdnN4sDdWnqkM3zLpSX-39bZVQE0MS7sb7ZGyYetm_jBRPfPoO/01SignTheClickOnce.png" alt="" width="550" height="470" /></a></p>
<p><strong>Step 4: </strong>Just like <a title="How to create a simple Browser Enabled WPF application" href="http://ahmadreza.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/">before</a>  publish it to you server.</p>
<p><strong>Step 5: </strong>Give certificate to the client and register the certificate on the client machine. To do this double-clicking on .cer file. You will see following window. Click on Install Certificate button.</p>
<p><img class="alignnone" src="https://i0.wp.com/public.bay.livefilestore.com/y1pZFqlYjWUc6WMxKhEZnvPIObkf7syrmkj_3GrXsmwEKe44a3ON90H9L8IQpQ612HM2gk0BTrlQKBnZCZSwytECg/02InstallCertificate.png" alt="" width="410" height="483" /></p>
<p>Follow installation wizard and click Next on the first window.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/public.bay.livefilestore.com/y1plD0sqjQ4lZqiA_weDaMseqiroigdUv6i-Dj8N7ClONOPN6NPPOHYEODk7yMJyVCI79cK5R4bmnArBfQ55dK0Hw/03CertificateImportWizard.png" alt="" width="501" height="394" /></p>
<p>In this window select &#8220;Place all certificates in the following store&#8221;  and then select &#8220;Browse&#8230;&#8221; button.</p>
<p><em><img loading="lazy" src="https://i0.wp.com/public.bay.livefilestore.com/y1plD0sqjQ4lZqx_94u5tjllRK8ghYlHog30qKU-ajAV28ST8V0gFVMzLqnF7eVbInn0RtvugNkoBiu24Q7lrTsCw/04SelectCertificateStore.png" alt="" width="285" height="263" /></em></p>
<p><em></em>In this window select &#8220;Trusted Publishers&#8221; and then click Ok. Select &#8220;Next&#8221; previous windows and then select finish.</p>
<p><strong>Step 6: </strong>Redo the step 5 but this time select &#8220;Trusted Root Certification Authorities&#8221; as the certificate store.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/public.bay.livefilestore.com/y1puBVImTKJcP2bDTJu6iTMgASmHIEyxsqH703mZ9lyE2r91rlcRCYrr6khI_3V8KuctJ-wljsSVNBELW4DhxwROA/05TrustedRoot.png" alt="" width="283" height="262" /></p>
<p>Now you have enabled your client to accept this XBAP application as full-trust application.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ahmadrezaa.wordpress.com/2011/05/20/how-to-sign-the-xbap-with-your-own-certificate/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">171</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pFsKJJV5SbKmeWkxSC7iJ-g2laeGSY_VkVN5ZGWCPeku6dJKdnN4sDdWnqkM3zLpSX-39bZVQE0MS7sb7ZGyYetm_jBRPfPoO/01SignTheClickOnce.png?psid=1" medium="image" />

		<media:content url="http://public.bay.livefilestore.com/y1pZFqlYjWUc6WMxKhEZnvPIObkf7syrmkj_3GrXsmwEKe44a3ON90H9L8IQpQ612HM2gk0BTrlQKBnZCZSwytECg/02InstallCertificate.png?psid=1" medium="image" />

		<media:content url="http://public.bay.livefilestore.com/y1plD0sqjQ4lZqiA_weDaMseqiroigdUv6i-Dj8N7ClONOPN6NPPOHYEODk7yMJyVCI79cK5R4bmnArBfQ55dK0Hw/03CertificateImportWizard.png?psid=1" medium="image" />

		<media:content url="http://public.bay.livefilestore.com/y1plD0sqjQ4lZqx_94u5tjllRK8ghYlHog30qKU-ajAV28ST8V0gFVMzLqnF7eVbInn0RtvugNkoBiu24Q7lrTsCw/04SelectCertificateStore.png?psid=1" medium="image" />

		<media:content url="http://public.bay.livefilestore.com/y1puBVImTKJcP2bDTJu6iTMgASmHIEyxsqH703mZ9lyE2r91rlcRCYrr6khI_3V8KuctJ-wljsSVNBELW4DhxwROA/05TrustedRoot.png?psid=1" medium="image" />
	</item>
		<item>
		<title>How to host a windows form application inside XBAP</title>
		<link>https://ahmadrezaa.wordpress.com/2011/05/17/how-to-host-a-windows-form-application-inside-xbap/</link>
					<comments>https://ahmadrezaa.wordpress.com/2011/05/17/how-to-host-a-windows-form-application-inside-xbap/#comments</comments>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Mon, 16 May 2011 23:29:02 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[XBAP]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=159</guid>

					<description><![CDATA[I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><em>I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type of projects is different in other versions of IIS and .Net frameworks and they are not is subject of this post series.</em></p>
<ol>
<li><a title="How to create a simple Browser Enabled WPF application" href="https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/">How to create a simple Browser Enables WPF application</a></li>
<li>How to host a windows form application inside XBAP</li>
<li><a title="How to sign the XBAP with your own certificate" href="http://ahmadreza.com/2011/05/20/how-to-sign-the-xbap-with-your-own-certificate/">How to sign the XBAP with your own certificate</a></li>
</ol>
<div>Imagine we have an existing windows form application or you are developing a windows form application. The question is how you can make your windows application browser enabled. The answer is Browser Enabled WPF application. But usually when we create a WPF application we have to use WPF elements inside Xaml. &#8220;WindowsFormsIntegration&#8221; will help you to host your windows form application inside your WPF.</div>
<div>For rest of this post I consider you already have previous <a title="How to create a simple Browser Enabled WPF application" href="https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/">post</a> sample, because I&#8217;m going to update the same project. Lets open the SimpleBrowserApplication and add a reference to &#8220;WindowsFormsIntegration&#8221;. As the second step I want to add new project to our solution of type &#8220;Windows Form Application&#8221; and I call it &#8220;WinFormSample&#8221;.</div>
<div><strong>Note: </strong>Make sure when you are creating windows form application you selected .Net 3.5 because we are going to reference this project inside WPF project and they should be compatible.</div>
<div><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pTR0DEBuQvABWLQzd6YhAQZuWSbC9O6dOBDZc0k0ikuOoYd9DcKUc8nMOo1HM4y7M1aeLGHg3mlzj61BTkYuIIVH73rTHoSwf/01WinProject.png" alt="" width="687" height="516" /></div>
<div>That is how our solution looks like after adding windows form application and couple of simple controls on it. Next step we have to reference WinFormSample in SimpleBrowser application. Now our WPF application has got two more references which are &#8220;WindowsFormsIntegrations&#8221; and &#8220;WinFormSample&#8221;. Now we have to change Page1.xaml and put the following StackPanel instead of previous &lt;Grid&gt;</div>
<pre class="brush: xml; title: ; notranslate">
&lt;StackPanel x:Name=&quot;stackPanel&quot;&gt;
&lt;/StackPanel&gt;
</pre>
<p>Actually this is a place holder for WindowsFormsHost that we are going to place in main form. For next step we need add System.Windows.Froms reference to WBP project. Then  we have to change page1.xaml code as follow.</p>
<pre class="brush: csharp; title: ; notranslate">
public partial class Page1 : Page
{
	private readonly Form1 mainForm = new Form1();
	WindowsFormsHost windowsFormsHost;

	public Page1()
	{
		InitializeComponent();
		AddWindowsForm();
	}
	private void AddWindowsForm()
	{
		windowsFormsHost = new WindowsFormsHost();

		stackPanel.Children.Add(windowsFormsHost);

		// If you don't write this line you'll get &quot;The child control cannot be a top-level form&quot; exception
		mainForm.TopLevel = false;
		windowsFormsHost.Child = mainForm;
	}

}
</pre>
<p>We created a WindowsFormHost and added this control into stackPanel Child list and set the child property of windowsFormsMost to mainForm which is already instantiated of Form1.</p>
<p>One of important thing is setting mainForm.TopLevel to false. Because if you don&#8217;t do that you will get an exception and if you dive into innerexeptions you will find out that main reason is System.ArgumentException: The child control cannot be a top-level form.</p>
<p>If you run this application you&#8217;ll see following browser window which hosts Form1.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pghRlG0xxzMlDb6UuxaxWxJIBslm4JuJx94sfUGjwQ_B65isf3BvSQXnDmkUv1LeLqc3Y35rgRKRKmh0zuuGUzynNDfxp1AXD/02WinFormInBrowser.png" alt="" width="295" height="339" /></p>
<p>The point is when you run this application from visual studio it runs in My Computer Zone so there is no problem for security. According Microsoft document &#8220;<a href="http://msdn.microsoft.com/en-us/library/aa970910.aspx">WPF Partial Trust Security</a>&#8221; section &#8220;Partial Trust Programming&#8221; when you run WPF application which requires full trust and current zone is &#8220;My Computer&#8221; behavior is &#8220;Automatic full trust&#8221; and for getting full trust no action is required.</p>
<p>But if you publish this project and try to browse this application you will get Trust Not Granted error. Because application will request for full trust and it fails with &#8220;Trust Not Granted&#8221;. In order to get full trust is signing XBAP with certificate.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pnin42vXvjTHQJ4c0_gDomTTngNGWiMBNIholYltYHRJ6rLN5jJBlD1jzXGP8Ynlg30JtoWPAqtUiz9XwM_HAwpMK5rVy7wYh/03TrustNotGranted.png" alt="" width="429" height="292" /></p>
<p>In the next post we will see how to sign XBAP with your own certificate and make it work.</p>
<p>The source code of this application is also available you can download it <a href="http://gkasoq.bay.livefilestore.com/y1pxaDZrXteX6D_gi2J4LOV9id05FJDax0nnSWPCp0-QXe2hd0glJoGIlxVkTt-viR292pddb7rs3MomJimgtH_MpmTMc3XuDS2/SimpleBrowserApplication.zip?download&amp;psid=1">here</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://ahmadrezaa.wordpress.com/2011/05/17/how-to-host-a-windows-form-application-inside-xbap/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">159</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pTR0DEBuQvABWLQzd6YhAQZuWSbC9O6dOBDZc0k0ikuOoYd9DcKUc8nMOo1HM4y7M1aeLGHg3mlzj61BTkYuIIVH73rTHoSwf/01WinProject.png?psid=1" medium="image" />

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pghRlG0xxzMlDb6UuxaxWxJIBslm4JuJx94sfUGjwQ_B65isf3BvSQXnDmkUv1LeLqc3Y35rgRKRKmh0zuuGUzynNDfxp1AXD/02WinFormInBrowser.png?psid=1" medium="image" />

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pnin42vXvjTHQJ4c0_gDomTTngNGWiMBNIholYltYHRJ6rLN5jJBlD1jzXGP8Ynlg30JtoWPAqtUiz9XwM_HAwpMK5rVy7wYh/03TrustNotGranted.png?psid=1" medium="image" />
	</item>
		<item>
		<title>How to create a simple Browser Enabled WPF application</title>
		<link>https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/</link>
					<comments>https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/#comments</comments>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Thu, 12 May 2011 04:59:38 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[XBAP]]></category>
		<guid isPermaLink="false">http://ahmadrezaa.wordpress.com/?p=144</guid>

					<description><![CDATA[I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><em>I am writing a series of posts about WPF Browser Application, XBAP  and configuration tips. I&#8217;m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type of projects is different in other versions of IIS and .Net frameworks and they are not is subject of this post series.</em></p>
<ol>
<li>How to create a simple Browser Enables WPF application</li>
<li><a title="How to host a windows form application inside XBAP" href="http://ahmadreza.com/2011/05/17/how-to-host-a-windows-form-application-inside-xbap/">How to host a windows form application inside XBAP</a></li>
<li><a title="How to sign the XBAP with your own certificate" href="http://ahmadreza.com/2011/05/20/how-to-sign-the-xbap-with-your-own-certificate/">How to sign the XBAP with your own certificate</a></li>
</ol>
<p>Firstly run Visual Studio 2010 and select new project from file menu</p>
<p><img class="alignnone" title="WPF Browser Application Template" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pTyvopu7LF2C48XH1rU6cCIppkTTSRI2zbiG4IymcFYNyDvDYOAuiDPGP_BbwZCwfe_Msl-NT15LmvikS4opJ6IjFau3VEDJa/01ProjectType.png" alt="" width="600" /></p>
<p>To create simple WPF browser application you need to select &#8220;WPF Browser Application&#8221; template from project templates. Once project template is created open Page1.xaml Xaml code and change Grid into following code</p>
<pre class="brush: xml; title: ; notranslate">
    &lt;Grid&gt;
        &lt;Rectangle
            Fill=&quot;#33CC66&quot;
            Width=&quot;2in&quot;       Height=&quot;1in&quot;
            Canvas.Top=&quot;25&quot;          Canvas.Left=&quot;50&quot;
            StrokeThickness=&quot;6px&quot; Stroke=&quot;Orange&quot; /&gt;
    &lt;/Grid&gt;
</pre>
<p>This Xaml code will create a simple rectangle with border and if you run this application, It will show following shape inside your browser.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pno2mRmcxK60x2XCCUpEH8AdIRl57EHzXxvUEScNJyTbdTGqN0TRpA25pd4ooBhFy-cQ9A7mOlpMz081IkhZQlhg9rhsmFeO8/02BrowserRectangle.png" alt="" width="289" height="333" /></p>
<p><strong>Deploying a WPF application</strong></p>
<p>There are multiple ways to do that. Simply you can publish your application using visual studio. Right-click on project and select publish menu. Publish wizard is displayed. Follow the steps until the end of publish steps.</p>
<p>You must follow Microsoft instruction for <a href="http://msdn.microsoft.com/en-us/library/ms752346.aspx">How to: Configure IIS 5.0 and IIS 6.0 to Deploy WPF Applications</a> to configure your server and client requires Internet Explorer plus .Net Framework to run this application.</p>
<p>Basically internet application which runs inside the browsers should have restricted access to critical resources. It means WPF browser application -By default- should respect to these restrictions so that client can make sure there is no harm to execute this application. Browser Enabled application by default is marked as partially trusted and ClickOnce security setting is set to Internet Zone so that your application will be running on the client browser without any problem.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1p2yMA3hrhMwt0iBWIUIIxE0kfNCGovL0YPJE27avS6fATUyXgPmATXdE8EH0G4w5m-wsW9E84rBfviY_Vt0AFxzHer8bMiQrn/03ProjectSecurity.png" alt="" width="547" height="313" /></p>
<p>As you see &#8220;Enable ClickOnce security settings&#8221; and &#8220;This is a partial trust application&#8221; are ticked by default and and ClickOnce manifests is signed by a temporary key. Which means on one your application will be restricted to security permission which is fully described in this <a href="http://msdn.microsoft.com/en-us/library/aa970910.aspx">document</a>. On the other hand your application will be executed on client browser without any other additional configuration.</p>
<p><img loading="lazy" class="alignnone" src="https://i0.wp.com/gkasoq.bay.livefilestore.com/y1pKjv-M2LBhfxzwWwSd_L29SOoyeqW5BcSkFwkyOQmjzksBojy0rFiaCALaIDovh5S7ieC6j-sV7cqiPiULDVI3idjIL9e3EwZ/04InternetZone.png" alt="" width="315" height="337" /></p>
<p>Above picture shows that you can run This simple WPF Browser Enabled application in &#8220;Internet Zone&#8221;.</p>
<p>The main reason of writing this post series for me is hosting windows form application inside XBAP application and running inside browser. In next post we will see how to host an existing windows form application within a WPF Browser application.</p>
<p><em>Sample project is also available you can download it <a href="http://gkasoq.bay.livefilestore.com/y1pMj5rWwxWH0xh978PfXKv51b9cKKKJ2zKKHaycMMZbHVm6kHqLQbIOej8nUumLrJYZlMQbeCSbDSxCQier38MWVQvihSHve5O/SimpleBrowserApplication.zip?download&amp;psid=1">here</a></em></p>
]]></content:encoded>
					
					<wfw:commentRss>https://ahmadrezaa.wordpress.com/2011/05/12/how-to-create-a-simple-browser-enabled-wpf-application/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">144</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pTyvopu7LF2C48XH1rU6cCIppkTTSRI2zbiG4IymcFYNyDvDYOAuiDPGP_BbwZCwfe_Msl-NT15LmvikS4opJ6IjFau3VEDJa/01ProjectType.png?psid=1" medium="image">
			<media:title type="html">WPF Browser Application Template</media:title>
		</media:content>

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pno2mRmcxK60x2XCCUpEH8AdIRl57EHzXxvUEScNJyTbdTGqN0TRpA25pd4ooBhFy-cQ9A7mOlpMz081IkhZQlhg9rhsmFeO8/02BrowserRectangle.png?psid=1" medium="image" />

		<media:content url="http://gkasoq.bay.livefilestore.com/y1p2yMA3hrhMwt0iBWIUIIxE0kfNCGovL0YPJE27avS6fATUyXgPmATXdE8EH0G4w5m-wsW9E84rBfviY_Vt0AFxzHer8bMiQrn/03ProjectSecurity.png?psid=1" medium="image" />

		<media:content url="http://gkasoq.bay.livefilestore.com/y1pKjv-M2LBhfxzwWwSd_L29SOoyeqW5BcSkFwkyOQmjzksBojy0rFiaCALaIDovh5S7ieC6j-sV7cqiPiULDVI3idjIL9e3EwZ/04InternetZone.png?psid=1" medium="image" />
	</item>
		<item>
		<title>HOW TO propagate WCF Impersonation to COM objects</title>
		<link>https://ahmadrezaa.wordpress.com/2010/09/16/how-to-propagate-wcf-impersonation-to-com-objects/</link>
					<comments>https://ahmadrezaa.wordpress.com/2010/09/16/how-to-propagate-wcf-impersonation-to-com-objects/#comments</comments>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Thu, 16 Sep 2010 17:19:00 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Impersonation]]></category>
		<category><![CDATA[WCF]]></category>
		<guid isPermaLink="false">/blog/post/WCF-Impersonation-is-not-propagated-to-COM-calls.aspx</guid>

					<description><![CDATA[I was working on a project to write a wrapper on a COM component in WCF. The COM object needs impersonation in some levels to provide certain functionalities to impersonated user. Basically impersonation in .Net application doesn&#8217;t propagate COM calls. I’ve seen following sentence in How To: Use Impersonation and Delegation in ASP.NET 2.0 The [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I was working on a project to write a wrapper on a COM component in WCF. The COM object needs impersonation in some levels to provide certain functionalities to impersonated user. Basically impersonation in .Net application doesn&#8217;t propagate COM calls. I’ve seen following sentence in<a href="http://msdn.microsoft.com/en-us/library/ff647404.aspx"> How To: Use Impersonation and Delegation in ASP.NET 2.0</a></p>
<blockquote><p><em>The impersonation token does not propagate across threads if you use COM interop with components that have incompatible threading models, or if you use unmanaged techniques to create new threads</em></p></blockquote>
<p>Actually I have a COM component and I want to use this component in my WCF Services. One of classes in COM object has got a property which returns current user. I wrote a console application to test impersonation and see if impersonation does propagate to COM object or not. I wrote a class for impersonation and  it has a method called “ImpersonateUser” that impersonates to passed username and password.  Following code returns different username for .net and COM object. It means it doesn’t propagate to COM object.</p>
<pre class="brush: csharp; title: ; notranslate">
static void Main(string[] args)
{
	DoWork();
	Console.ReadLine();
}

private static void DoWork()
{
	ImpersonateClass impersonate = new ImpersonateClass();
	using (impersonate.ImpersonateUser(&quot;AnotherUser&quot;,
					&quot;Domain&quot;,
					&quot;passw0rd&quot;))
	{
		COMTESTLib.TestClass obj = new COMTESTLib.TestClass();
		Console.WriteLine(string.Format(&quot;COM:{0} .Net:{1}&quot;, obj.CurrentUserName,
			System.Security.Principal.WindowsIdentity.GetCurrent().Name));
	}
}
//Returns:COM:DOMAIN\CurrentUser .Net:DOMAIN\AnotherUser&lt;/pre&gt;
</pre>
<p><span style="widows:2;text-transform:none;text-indent:0;border-collapse:separate;font:16px 'Times New Roman';white-space:normal;orphans:2;letter-spacing:normal;color:#000000;word-spacing:0;"><span style="line-height:16px;font-family:'Segoe UI', 'Lucida Grande', verdana, arial, helvetica, sans-serif;color:#333333;font-size:13px;">But I used CoInitializeSecurity to initialize security with impersonation and cloaking. CoInitializeSecurity should be called before any marshalling so I called CoInitializeSecurity as first function call. </span></span></p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:c114ad9b-6da4-444f-8201-ac3a332b2ea0" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp; title: ; notranslate">
[DllImport(&quot;ole32.dll&quot;)]
public static extern int CoInitializeSecurity(IntPtr pVoid, int
cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RpcAuthnLevel level,
RpcImpLevel impers, IntPtr pAuthList, int dwCapabilities, IntPtr
pReserved3);

static void Main(string[] args)
{
	int result = CoInitializeSecurity(IntPtr.Zero, -1,
	IntPtr.Zero, IntPtr.Zero,
	RpcAuthnLevel.Connect, RpcImpLevel.Impersonate,
	IntPtr.Zero, Convert.ToInt32(EoAuthnCap.DynamicCloaking), IntPtr.Zero);

	DoWork();
	Console.ReadLine();
}

private static void DoWork()
{
	ImpersonateClass impersonate = new ImpersonateClass();
	using (impersonate.ImpersonateUser(&quot;AnotherUser&quot;,
					&quot;domain&quot;,
					&quot;passw0rd&quot;))
	{
		COMTESTLib.TestClass obj = new COMTESTLib.TestClass();
		Console.WriteLine(string.Format(&quot;COM:{0} .Net:{1}&quot;, obj.CurrentUserName,
			System.Security.Principal.WindowsIdentity.GetCurrent().Name));
	}
}
//Returns:COM:DOMAIN\AnotherUser .Net:DOMAIN\AnotherUser&lt;/pre&gt;
</pre>
</div>
<p><span style="widows:2;text-transform:none;text-indent:0;border-collapse:separate;font:16px 'Times New Roman';white-space:normal;orphans:2;letter-spacing:normal;color:#000000;word-spacing:0;"><span style="line-height:16px;font-family:'Segoe UI', 'Lucida Grande', verdana, arial, helvetica, sans-serif;color:#333333;font-size:13px;">Somebody suggested using built-in impersonation method for WCF so I changed my service to following code by setting [OperationBehavior(Impersonation = ImpersonationOption.Required)] and some changes in Web.Congif to make it work. </span></span></p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:754a31d4-cb85-4816-8422-473daeac6f5b" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp; title: ; notranslate">
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string GetUserNames()
{
	return DoWork();
}

private string DoWork()
{
	string result;

	COMTESTLib.TestClass obj = new COMTESTLib.TestClass();
	result = string.Format(&quot;COM:{0} .Net:{1}&quot;, obj.CurrentUserName,
		System.Security.Principal.WindowsIdentity.GetCurrent().Name);

	return result;
}
</pre>
</div>
<p>I passed user credentials from client for impersonation.</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:ede90d67-9613-47db-a6b6-95acdd227e4b" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp; title: ; notranslate">
static void Main(string[] args)
{
	ServiceReference.ServiceImpersonateClient client = new ServiceReference.ServiceImpersonateClient();
	client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
	client.ClientCredentials.Windows.ClientCredential.Domain = &quot;Domain&quot;;
	client.ClientCredentials.Windows.ClientCredential.UserName = &quot;AnotherUser&quot;;
	client.ClientCredentials.Windows.ClientCredential.Password = &quot;Passw0rd&quot;;

	string str = client.GetUserNames();
	Console.WriteLine(str);
	Console.ReadLine();
	client.Close();
}
// Returns COM:DOMAIN\CurrentUser .Net:DOMAIN\AnotherUser
</pre>
</div>
<p>But it returns different usernames which means impersonation of WCF does not propagate to COM object.</p>
<p>Ok I posted this issue in <a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/ec442221-f750-4253-ac12-b41ab6e4ba1b/#c7b7fa20-b9b7-4576-9ca5-c2c21c6cf909">WCF Forum</a> and I’ve got a simple response but it gave me a good clue. Allen Chen (The moderator) suggested me to use self-hosting for WCF instead of IIS hosting.</p>
<p>Self-hosted WCF was great idea!  Actually it works on self-hosted. But my WCF service should be unattended so that I created a managed service for WCF hosting. To make it work I’ve created three projects. One WCF service library which is used in managed service application. And the third one is a console application for testing. I’ve called CoInitializeSecurity on constructor of my service class as following</p>
<div id="scid:f32c3428-b7e9-4f15-a8ea-c502c7ff2e88:f23013c2-8c2c-43f6-a79c-80ad220917d7" class="wlWriterSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre class="brush: csharp; title: ; notranslate">
private ServiceHost serviceHost;
 public WCFServiceHost()
 {
  int result = CoInitializeSecurity(IntPtr.Zero, -1,
  IntPtr.Zero, IntPtr.Zero,
  RpcAuthnLevel.Connect, RpcImpLevel.Impersonate,
  IntPtr.Zero, Convert.ToInt32(EoAuthnCap.DynamicCloaking), IntPtr.Zero);
  //EventLog.WriteEntry(result.ToString());
  InitializeComponent();
 }

 protected override void OnStart(string[] args)
 {
  if (serviceHost != null)
  {
  serviceHost.Close();
  }

  // Create a ServiceHost for the CalculatorService type and
  // provide the base address.
  serviceHost = new ServiceHost(typeof(ServiceImpersonate));

  // Open the ServiceHostBase to create listeners and start
  // listening for messages.

  serviceHost.Open();

 }

 protected override void OnStop()
 {
  if (serviceHost != null)
  {
  serviceHost.Close();
  serviceHost = null;
  }
 }
</pre>
</div>
<p>It doesn’t work with built in WCF impersonation. But I used my impersonation class inside the service class to impersonate to desired user and it works.</p>
<pre class="brush: csharp; title: ; notranslate">
public string GetUserNames()
 {
  ImpersonateClass impersonate = new ImpersonateClass();

  using (impersonate.ImpersonateUser(&quot;AnotherUser&quot;,
         &quot;Domain&quot;,
          &quot;passw0rd&quot;))
  {
   return DoWork();
  }
 }

 private string DoWork()
 {
  string result;

   COMTESTLib.TestClass obj = new COMTESTLib.TestClass();
   result = string.Format(&quot;COM:{0} .Net:{1}&quot;, obj.CurrentUserName,
       System.Security.Principal.WindowsIdentity.GetCurrent().Name);
  return result;
 }
</pre>
<p>In the console application I simply called my service like following code:</p>
<pre class="brush: csharp; title: ; notranslate">
ServiceReference.ServiceImpersonateClient client = new ServiceReference.ServiceImpersonateClient();
  string str = client.GetUserNames();
  Console.WriteLine(str);
  client.Close();
  Console.ReadLine();
// Returns COM:DOMAIN\ AnotherUser .Net:DOMAIN\AnotherUser
</pre>
<blockquote><p><em>When you host WCF in IIS the process is spawned by IIS and before your code running code managed by IIS will be run in the process, which is almost out of your control. CoInitializeSecurity thus might be called before you calling it.</em></p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://ahmadrezaa.wordpress.com/2010/09/16/how-to-propagate-wcf-impersonation-to-com-objects/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">35</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>
	</item>
		<item>
		<title>Updating my using with Windows Live Writer</title>
		<link>https://ahmadrezaa.wordpress.com/2010/08/04/updating-my-using-with-windows-live-writer/</link>
					<comments>https://ahmadrezaa.wordpress.com/2010/08/04/updating-my-using-with-windows-live-writer/#comments</comments>
		
		<dc:creator><![CDATA[Ahmadreza Atighechi]]></dc:creator>
		<pubDate>Wed, 04 Aug 2010 19:46:00 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Live Writer]]></category>
		<guid isPermaLink="false">/blog/post/Updating-my-blog-with-windows-live-writer.aspx</guid>

					<description><![CDATA[I didn&#8217;t know about this sophisticated application so called “Windows Live Writer”. Actually quiet frequently I was looking for an HTML editor to write my blog post and then update my blog. Because HTML edit which comes with BlogEngine.Net is not easy to use – and its the same problem with all web based HTML [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>I didn&#8217;t know about this sophisticated application so called “<a href="http://explore.live.com/windows-live-writer">Windows Live Writer</a>”. Actually quiet frequently I was looking for an HTML editor to write my blog post and then update my blog. Because HTML edit which comes with BlogEngine.Net is not easy to use – and its the same problem with all web based HTML editor-. To be honest windows live writer is something more than what I expected.</p>
<p>I was reading an article about <a href="http://msdn.microsoft.com/en-au/magazine/ff872392.aspx">AtomPub Using WCF Data</a>, I came across Windows Live Writer which surprisingly supports BlogEngine.Net (Its better to say that BlogEnine.Net supports AtomPub which is protocol for publishing content).</p>
<p>Windows Live Writer easily installed on my machine. After installation completed you will be asked about your blog information and if your blog supports AtomPub then you can continue to enter login information of your blog. With a quick search I found in <a href="http://en.wikipedia.org/wiki/Windows_Live_Writer">WiKi</a>that Windows Live Writer supports BlogEngine.Net so I selected “other blog service” option in this form.</p>
<p align="center"><a href="http://www.ahmadreza.com/blog/image.axd?picture=image.png"><img loading="lazy" style="display:inline;border:0;" title="What blog service do you use" src="http://www.ahmadreza.com/blog/image.axd?picture=image_thumb.png" alt="What blog service do you use" width="404" height="345" border="0" /></a></p>
<p>In next step you’ll be asked about your blog address and username and password.</p>
<p><a href="http://www.ahmadreza.com/blog/image.axd?picture=image_1.png"><img loading="lazy" style="display:block;float:none;margin-left:auto;margin-right:auto;border:0;" title="Add a blog account" src="http://www.ahmadreza.com/blog/image.axd?picture=image_thumb_1.png" alt="Add a blog account" width="404" height="345" border="0" /></a></p>
<p>For me as a developer it was surprising that Windows Live Writer supports plug-ins. first thing I looked for was <a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighlighter</a>. Yes! . I’s already been developed. there is a project in CodePlex named &#8220;<a href="http://wlwsyntaxhighlighter.codeplex.com/">SyntaxHighlighter for Windows Live Writer</a>” you can easily download and set it up.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ahmadrezaa.wordpress.com/2010/08/04/updating-my-using-with-windows-live-writer/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">34</post-id>
		<media:content url="https://1.gravatar.com/avatar/a0149ca53ac1cc05bce8720f85e8e1d48206532c1142402fabe237f90ebfdf6e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ahmadrezaa</media:title>
		</media:content>

		<media:content url="http://www.ahmadreza.com/blog/image.axd?picture=image_thumb.png" medium="image">
			<media:title type="html">What blog service do you use</media:title>
		</media:content>

		<media:content url="http://www.ahmadreza.com/blog/image.axd?picture=image_thumb_1.png" medium="image">
			<media:title type="html">Add a blog account</media:title>
		</media:content>
	</item>
	</channel>
</rss>
