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

<channel>
	<title>Calle Arnesten&#039;s Blog &#187; .NET</title>
	<atom:link href="http://www.arnesten.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arnesten.com</link>
	<description>Web Development, Test Driven Development, NHibernate, ASP.NET MVC, jQuery and more..</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Setting non-public property with type safety (no more strings)</title>
		<link>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/</link>
		<comments>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 12:45:53 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=32</guid>
		<description><![CDATA[Sometimes I have a property where the get-part is public and the set-part is private or protected. Like this: public class Forum { &#160; &#160; public virtual int TopicCount { get; protected set; } &#160; &#160; &#160; // More code... } There can be various reasons for this design. For me it is often when [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I have a property where the get-part is public and the set-part is private or protected. Like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">Forum</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">int</span> TopicCount { <span style="color: blue;">get</span>; <span style="color: blue;">protected</span> <span style="color: blue;">set</span>; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// More code...</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>There can be various reasons for this design. For me it is often when I make database optimizations where I don&#8217;t want to be able to change a property directly in the code, but instead do it directly against the database.</p>
<p>Still, sometimes I want to set the value in unit tests. How do I do that?</p>
<p>Before C# 3.0 I had to rely on having a string with the property name and then use reflection. The main disadvantage with this approach is that if you rename the property you have to make sure that the string is also changed, which is time-consuming and error-prone.</p>
<p>So, with C# 3.0 you no longer need that string. You could use the new Expression-tree feature like this instead:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> SetProperty&lt;TObj, TValue&gt;(TObj obj, 
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: #2b91af;">Expression</span>&lt;<span style="color: #2b91af;">Func</span>&lt;TObj, TValue&gt;&gt; property, TValue value)
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">var</span> propertyInfo = (<span style="color: #2b91af;">PropertyInfo</span>)((<span style="color: #2b91af;">MemberExpression</span>)property.Body).Member;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; propertyInfo.SetValue(obj, value, <span style="color: blue;">null</span>);
<p style="margin: 0px;">}
</div>
</pre>
<p>I have placed this method in my BaseTest class, since I only use it in my tests. Now you can write code like this to set the property:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> forum = <span style="color: blue;">new</span> <span style="color: #2b91af;">Forum</span>();
<p style="margin: 0px;">SetProperty(forum, f =&gt; f.TopicCount, 2);
</div>
</pre>
<p>This is only a scratch on the surface of what you can accomplish with Expression-trees.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing your NHibernate data access code using SQLite</title>
		<link>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/</link>
		<comments>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 21:28:54 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=30</guid>
		<description><![CDATA[Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. But, despite that, I’ve let code close to the data access be left [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. </p>
<p>But, despite that, I’ve let code close to the data access be left untested. This has been due to the pain I thought it would be to create and maintain those tests. And also I’ve thought that those tests would be way to slow to have among my other unit tests (which we want to be fast, so that we run them often).</p>
<p>For the first two years of my career this was not a big problem. We rarely had bugs in our data access code as it mostly consisted of simple, straight forward code. So I hadn’t really given it much thought. Until lately that is.</p>
<p>Then we started to get performance issues as we got some really large organizations to use or software. To resolve the performance issues we made our data access smarter so it could make more efficient queries to the database. As smarter often means more code, or at least added complexity, I saw the need of automatic tests to guarantee the quality of the code.</p>
<p>So, I read some articles on using SQLite with NHibernate. After some pain setting it all up, I accomplished my goal of easy data access testing. So say that I want to test GetCustomerByName in the following class (in real life I wouldn&#8217;t test methods that are this simple, but it&#8217;s just to show the idea):</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerDao</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">INHibernateManager</span> _sessionManager;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> CustomerDao(<span style="color: #2b91af;">INHibernateManager</span> sessionManager)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; _sessionManager = sessionManager;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">Customer</span> GetCustomerByName(<span style="color: blue;">string</span> name)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> session = _sessionManager.CurrentSession;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> session.CreateQuery(<span style="color: #a31515;">&quot;from Customer where Name = :name&quot;</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SetParameter(<span style="color: #a31515;">&quot;name&quot;</span>, name)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UniqueResult&lt;<span style="color: #2b91af;">Customer</span>&gt;();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>Then I can write a test like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerDaoTest</span> : <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; [<span style="color: #2b91af;">Test</span>]
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> CanGetCustomerByName()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Init();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerA = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;A&quot;</span> };
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerB = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;B&quot;</span> };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Save(customerA, customerB);&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerDao = <span style="color: blue;">new</span> <span style="color: #2b91af;">CustomerDao</span>(Database);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> result = customerDao.GetCustomerByName(<span style="color: #a31515;">&quot;B&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Assert</span>.That(result, <span style="color: #2b91af;">Is</span>.EqualTo(customerB));
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>For this to work I have a class called TestDatabase with the following code:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">TestDatabase</span> : <span style="color: #2b91af;">INHibernateManager</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ISessionFactory</span> m_sessionFactory;&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">SchemaExport</span> SchemaExport;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: #2b91af;">ISession</span> m_session;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Init()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_sessionFactory == <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = CreateNHibernateConfig();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_sessionFactory = config.BuildSessionFactory();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SchemaExport = <span style="color: blue;">new</span> <span style="color: #2b91af;">SchemaExport</span>(config);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_session != <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_session.Close();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session = m_sessionFactory.OpenSession();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session.BeginTransaction();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> connection = m_session.Connection;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; SchemaExport.Execute(<span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>, <span style="color: blue;">false</span>, connection, <span style="color: blue;">null</span>);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">Configuration</span> CreateNHibernateConfig()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = <span style="color: blue;">new</span> <span style="color: #2b91af;">Configuration</span>();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.Properties.Clear();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.driver_class&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;NHibernate.Driver.SQLite20Driver&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.dialect&quot;</span>, <span style="color: #a31515;">&quot;NHibernate.Dialect.SQLiteDialect&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.connection_string&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;Data Source=:memory:;Version=3;New=True;&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.cache.use_second_level_cache&quot;</span>, <span style="color: #a31515;">&quot;false&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.hbm2ddl.auto&quot;</span>, <span style="color: #a31515;">&quot;create&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.AddAssembly(<span style="color: #a31515;">&quot;MyAssembly&quot;</span>); <span style="color: green;">// Change this to your assembly</span>
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> config;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ISession</span> Session
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Save(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] entities)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> entity <span style="color: blue;">in</span> entities)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session.Save(entity);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Session.Flush();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: #2b91af;">ISession</span> <span style="color: #2b91af;">INHibernateManager</span>.CurrentSession
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>And I’ve also added the following field to my BaseTest class:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;">[<span style="color: #2b91af;">TestFixture</span>]
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">TestDatabase</span> Database = <span style="color: blue;">new</span> <span style="color: #2b91af;">TestDatabase</span>();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// More code here...</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>So, in summary, I&#8217;ve shown a pretty easy way of doing database testing. Even though the method under test was really simple in the example, the same technique could be used for much more complex scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Home from Öredev 2008</title>
		<link>http://www.arnesten.com/2008/11/23/home-from-oredev-2008/</link>
		<comments>http://www.arnesten.com/2008/11/23/home-from-oredev-2008/#comments</comments>
		<pubDate>Sun, 23 Nov 2008 18:31:41 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=26</guid>
		<description><![CDATA[Friday night I came home from Öredev, a 3 day conference in Malmö. Overall, a great experience which has made me and the team I work with inspired. The following are the most interesting experiences that I will take with me: The Power of Value by Dan Bergh Johnsson. One of the best sessions of [...]]]></description>
			<content:encoded><![CDATA[<p>Friday night I came home from <a href="http://www.oredev.se">Öredev</a>, a 3 day conference in Malmö. Overall, a great experience which has made me and the team I work with inspired.</p>
<p>The following are the most interesting experiences that I will take with me:</p>
<ul>
<li><b>The Power of Value by Dan Bergh Johnsson</b>. One of the best sessions of Öredev. An eye-opener for what value objects can do. A value object is an immutable object where you are not interested in tracking its identity. I will now begin to think more of this when developing.
</li>
<li><b>Agile Architecture by James Coplien</b>. James Coplien was a strange and arrogant person, for example claiming that he was 1 of 15 in the world who really understood object-orientation. But his ideas how architecture should be was really, really interesting. I think he is on to something with his gluing of Methodful Roles, Identifiers and Methodless Roles onto simple classes. Will definitely need to study this more as I only got a vague understanding of it.
</li>
<li><b>Clean Code by Robert C. Martin</b>. Uncle Bob (as he is also called) made two inspiring talks: the keynote and Clean Code III. I&#8217;ve had the pleasure of seeing him before at <a href="http://www.arnesten.com/2007/09/30/jaoo-2007-%e2%80%93-tdd-tutorial-with-robert-c-martin/">JAOO 2007</a>, but despite that got more tips for writing good code. The rest of the team I work with, were also inspired, so we will try to enforce a clean code policy at work.
</li>
<li><b>The Pomodoro Technique by Staffan Nöteberg</b>. Staffan was a good speaker, using dolls and hats to make the speech fun. The Pomodoro technique is a process for improving one&#8217;s personal work. It&#8217;s main idea is to only be allowed to focus on one thing at a time. Will try this soon, both at work and at home.
</li>
<li><b>The Lean thinking</b>. Lean was mentioned by many speakers, Scott Bellware and James Coplien for example. It seems to be the new buzzword. I need to check up more on this, but I think I got the basic idea of it at least. Which is produce more value and eliminate waste.
</li>
<li><b>Behaviour Driven Design</b>. Got some ideas in this area from various sessions. MSpec as a framework looks interesting, especially the HTML-reports it can generate. AutoHotkey seems like a good utility for writing underscores more efficiently. Will start to use BDD-style both at work and at home.
</li>
<li><b>T-person</b>. Was mentioned by Scott Bellware. It basically is a person which is good at other things than his main speciality. Like a developer that also is skilled at design and testing. Then he can help in both of those areas if needed and also his understanding will make hand-offs smoother.
</li>
<li><b>Design Debt</b>. Got a good scetch model to illustrate how design debt works for people that don&#8217;t understand it (like managers).
</li>
</ul>
<p>I learned many more things during the conference, but I have to stop now. Probably there will be more posts later on as I try out some of the new ideas I&#8217;ve got.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/23/home-from-oredev-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AutoStub parameters using Rhino Mocks</title>
		<link>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/</link>
		<comments>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 19:46:49 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Rhino Mocks]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/</guid>
		<description><![CDATA[For the last years I’ve been using dependency injection for my service classes. More recently (by the introduction of ASP.NET MVC) that have also been the case for my controller classes. I take in their dependencies via the constructor and then let a Dependency Injection/Inversion of Control (DI/IoC) framework automatically resolve and create the classes. [...]]]></description>
			<content:encoded><![CDATA[<p>For the last years I’ve been using dependency injection for my service classes. More recently (by the introduction of ASP.NET MVC) that have also been the case for my controller classes. I take in their dependencies via the constructor and then let a Dependency Injection/Inversion of Control (DI/IoC) framework automatically resolve and create the classes. I’ve been using Castle Windsor for this, which has worked really well.</p>
<p>However, when it comes to testing service/controller classes, I’ve previously created (often stubbed) the parameters needed when testing, and then given them manually to the constructor. Like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> customerDao = Stub&lt;<span style="color: #2b91af;">OrderDao</span>&gt;();
<p style="margin: 0px;"><span style="color: blue;">var</span> orderDao = Stub&lt;<span style="color: #2b91af;">OrderDao</span>&gt;();
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = <span style="color: blue;">new</span> <span style="color: #2b91af;">CustomerController</span>(customerDao, orderDao);
</div>
</pre>
<p>This has proven to be quite fragile as the dependencies to the service/controller class has changed. Often, the case has been that more dependencies have been added as parameters to the constructor. I know, I know, that might be a code smell that the service/controller should be split into smaller classes… But anyway, code smell or not, the fragility of the tests is a problem.</p>
<p>A while back I read about an AutoMocking container, but thought it was a bit cumbersome to have to use both a Mocking framework and a DI/IoC container in my tests. So, I thought that this should be possibly to accomplish with only a mocking framework and a bit of reflection magic. And it actually turned out to be really easy to do.</p>
<p>As a mocking framework I use Rhino Mocks (btw, I love the new AAA syntax in 3.5!). All my test inherit from a class called BaseTest, which is where I decided to put the autostubbing. The code comes here:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;">[<span style="color: #2b91af;">TestFixture</span>]
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T Stub&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters) <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub&lt;T&gt;(parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T Mock&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters) <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateMock&lt;T&gt;(parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T CreateAndAutoStubParameters&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> (T)CreateAndAutoStubParameters(<span style="color: blue;">typeof</span>(T), parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> CreateAndAutoStubParameters(<span style="color: #2b91af;">Type</span> type, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructor = type.GetConstructors(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.Instance)[0];
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> parameterInfos = constructor.GetParameters();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = GetConstructorParameters(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameterInfos, parameters);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> constructor.Invoke(constructorParams.ToArray());
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">object</span>&gt; GetConstructorParameters(<span style="color: #2b91af;">ParameterInfo</span>[] parameterInfos, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">object</span>&gt;();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> parameterInfos)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constructorParams.Add(GetParameter(info, parameters));
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> constructorParams;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> GetParameter(<span style="color: #2b91af;">ParameterInfo</span> info, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">object</span>[] candidateParameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> candidate <span style="color: blue;">in</span> candidateParameters)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (info.ParameterType.IsAssignableFrom(candidate.GetType()))
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> candidate;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> AutoStub(info.ParameterType);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T AutoStub&lt;T&gt;() <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> (T)AutoStub(<span style="color: blue;">typeof</span>(T));
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> AutoStub(<span style="color: #2b91af;">Type</span> type)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (type.IsInterface)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub(type);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructor = type.GetConstructors(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.Instance)[0];
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> parameterInfos = constructor.GetParameters();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = GetConstructorParameters(parameterInfos);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub(type, constructorParams.ToArray());
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>So, now let us assume you want to test a CustomerController with dependencies like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerController</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> CustomerController(<span style="color: #2b91af;">CustomerDao</span> customerDao, <span style="color: #2b91af;">OrderDao</span> orderDao)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: green;">// Code here..</span>
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// Code here..</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>Then you only have to write this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = CreateAndAutoStubParameters&lt;<span style="color: #2b91af;">CustomerController</span>&gt;();
</div>
</pre>
<p>to create the controller with stubs automatically created as constructor parameters.<br />
Often, you might want to have some control over one of the parameters, and then you could write like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> customerDao = AutoStub&lt;<span style="color: #2b91af;">CustomerDao</span>&gt;();
<p style="margin: 0px;">customerDao.Stub(c =&gt; c.GetById(1)).Return(<span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span>());
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = CreateAndAutoStubParameters&lt;<span style="color: #2b91af;">CustomerController</span>&gt;(customerDao);
</div>
</pre>
<p>This way of testing service and controller classes has removed some of the pain of starting to test classes with dependencies that need to be mocked or stubbed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validating HTTP method in ASP.NET MVC</title>
		<link>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/</link>
		<comments>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 09:29:11 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/</guid>
		<description><![CDATA[On the web it&#8217;s considered a good practice that the GET method never changes the state on the server. When state should be changed the POST method should be used. It is good to follow this practice, otherwise there might be problems when search engines tries to visit links (which are GET by default). By [...]]]></description>
			<content:encoded><![CDATA[<p>On the web it&#8217;s considered a good practice that the GET method never changes the state on the server. When state should be changed the POST method should be used. It is good to follow this practice, otherwise there might be problems when search engines tries to visit links (which are GET by default).<br />
By default in ASP.NET MVC any public method on the controller can be accessed both by GET and POST. So I _could_ access my DeleteUser() method by an ordinary link (which is GET). </p>
<p>So, how do I restrict a method in ASP.NET MVC to be only GET or only POST? I could use ActionFilterAttribute and tag each method with it. But tagging every method is a bit noisy in my opinion. The thing I want is GET to be the default way to access methods, and then I want to mark POST methods only with attributes.</p>
<p>In my MVC projects I always have a BaseController, that all my controllers inherit from. With that in place it is actually quite easy to accomplish the above. I just need to override OnActionExecuting like this:</p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> BaseController : Controller
{
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnActionExecuting(ActionExecutingContext filterContext)
    {
        var attributes = filterContext.ActionMethod.GetCustomAttributes(
            <span class="kwrd">typeof</span>(POSTAttribute), <span class="kwrd">false</span>);
        var method = filterContext.HttpContext.Request.HttpMethod;
        <span class="kwrd">if</span> (attributes.Length &gt; 0 &amp;&amp; method != <span class="str">"POST"</span>)
        {
            ThrowIllegalMethodException(<span class="str">"POST"</span>, filterContext);
        }
        <span class="kwrd">else</span> <span class="kwrd">if</span> (attributes.Length == 0 &amp;&amp; method != <span class="str">"GET"</span>)
        {
            ThrowIllegalMethodException(<span class="str">"GET"</span>, filterContext);
        }
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ThrowIllegalMethodException(<span class="kwrd">string</span> expectedHttpMethod,
        ActionExecutingContext filterContext)
    {
        var controllerName = filterContext.Controller.GetType().Name;
        var controllerMethod = filterContext.ActionMethod.Name;
        var httpMethod = filterContext.HttpContext.Request.HttpMethod;

        var message = <span class="kwrd">string</span>.Format(<span class="str">"Only {0} method allowed for {1}.{2}, but {3} was used."</span>,
            expectedHttpMethod, controllerName, controllerMethod, httpMethod);
        <span class="kwrd">throw</span> <span class="kwrd">new</span> SecurityException(message);
    }
}</pre>
<p>And also I need a POSTAttribute which is simplest possible:</p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> POSTAttribute : Attribute
{
}</pre>
<p>So, now I can write [POST] above methods like this: </p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> MyController : BaseController
{
    [POST]
    <span class="kwrd">public</span> ActionResult SaveMyEntity()
    {
        <span class="rem">// Code..</span>
    }
}</pre>
<p>Pretty cool that you can change the behaviour of the MVC framework this easy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Model View Controller from Microsoft</title>
		<link>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/</link>
		<comments>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 21:31:59 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/</guid>
		<description><![CDATA[Just read this. This is great news! I don&#8217;t like the way WebForms work today, I think its hard to get testable code close to the UI. I know there are alternatives like MonoRail, but I find the disadvantage of not having third party components like Telerik too big. But if a framework for Model [...]]]></description>
			<content:encoded><![CDATA[<p>Just read <a href="http://igloocoder.com/archive/2007/10/06/alt.net-day-2-session-3----ms-mvc-preview.aspx">this</a>. This is great news! I don&#8217;t like the way WebForms work today, I think its hard to get testable code close to the UI. I know there are alternatives like <a href="http://www.castleproject.org/monorail/">MonoRail</a>, but I find the disadvantage of not having third party components like <a href="http://www.telerik.com">Telerik</a> too big. But if a framework for Model View Controller comes from Microsoft, I&#8217;m sure that third party component companies will start develop components for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Word automation to embed linked images</title>
		<link>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/</link>
		<comments>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/#comments</comments>
		<pubDate>Thu, 10 May 2007 17:41:52 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Office Open XML]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=4</guid>
		<description><![CDATA[In my current project I generate reports by creating Word 2007 files. You know the file format consisting of zipped XML files? This project is web-based and the users can enter information through rich text editors. These rich text editors produce html which in turn is inserted in the reports (with the altChunk tag for [...]]]></description>
			<content:encoded><![CDATA[<p>In my current project I generate reports by creating Word 2007 files. You know the file format consisting of zipped XML files? This project is web-based and the users can enter information through rich text editors. These rich text editors produce html which in turn is inserted in the reports (with the altChunk tag for those who are interested) . All this can be done without any Word automation which is good (having Office on the server is not supported by Microsoft).</p>
<p>Before today, only textual content with formatting could be entered through these text editors. But today I added the ability to upload and insert images as well. And here the problem started. When generating the reports the images would only be linked to and not embedded inside which is not what I wanted&#8230;</p>
<p>So how did I solve this? I cheated! Before today we were using Word automation for converting Word 2007 documents to Word 97 (using a web service) for backwards compatability. I decided to try and modify this web service to automatically embed linked images. I found an <a href="http://west-wind.com/weblog/posts/1178.aspx" target="_blank">article </a>by Rick Strahl which led me to the solution. Besides the things the article mentionend I also needed to perform the same thing with InlineShapes. Here comes the code:</p>
<pre class="csharpcode">
<span class="kwrd">private</span> <span class="kwrd">void</span> EmbedLinkedImages(Document doc)
{
    <span class="kwrd">foreach</span> (InlineShape shape <span class="kwrd">in</span> doc.InlineShapes)
    {
        <span class="kwrd">if</span> (shape.LinkFormat != <span class="kwrd">null</span>)
        {
            shape.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            shape.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }

    <span class="kwrd">foreach</span> (Field field <span class="kwrd">in</span> doc.Fields)
    {
        <span class="kwrd">if</span> (field.Type == WdFieldType.wdFieldIncludePicture)
        {
            field.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            field.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }

    <span class="kwrd">foreach</span> (Shape shape <span class="kwrd">in</span> doc.Shapes)
    {
        <span class="kwrd">if</span> (shape.LinkFormat != <span class="kwrd">null</span>)
        {
            shape.LinkFormat.SavePictureWithDocument = <span class="kwrd">true</span>;
            shape.LinkFormat.BreakLink();
            doc.UndoClear();
        }
    }
}</pre>
<p>The code is mostly self-explanatory except the doc.UndoClear(). It is there to clear the undo buffer in Word which otherwise can consume a lot of memory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/05/10/using-word-automation-to-embed-linked-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

