<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>IoC</title>
        <link>http://ayende.com/Blog/category/506.aspx</link>
        <description>IoC</description>
        <language>en-US</language>
        <copyright>Ayende Rahien</copyright>
        <managingEditor>Ayende@ayende.com</managingEditor>
        <generator>Subtext Version 1.9.3.51</generator>
        <item>
            <title>Zero friction IoC: Auto registration is mandatory</title>
            <link>http://ayende.com/Blog/archive/2008/05/13/Zero-friction-IoC-Auto-registration-is-mandatory.aspx</link>
            <description>&lt;p&gt;This is the entire Binsor config file for a real application:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;font color="#0000ff"&gt;import&lt;/font&gt; Castle.MonoRail.Framework
&lt;font color="#0000ff"&gt;import&lt;/font&gt; Castle.MonoRail.WindsorExtension
&lt;font color="#0000ff"&gt;import&lt;/font&gt; Rhino.Commons.Facilities from Rhino.Commons.ActiveRecord

&lt;font color="#ff8000"&gt;facility&lt;/font&gt; MonoRailFacility
&lt;font color="#ff8000"&gt;facility&lt;/font&gt; RhinoTransactionFacility
&lt;font color="#ff8000"&gt;facility&lt;/font&gt; ActiveRecordUnitOfWorkFacility:
	assembly = "&lt;span style="color: #8b0000"&gt;HibernatingRhinos&lt;/span&gt;"

&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypesBased of IController("&lt;span style="color: #8b0000"&gt;HibernatingRhinos&lt;/span&gt;"):
	&lt;font color="#ff8000"&gt;component&lt;/font&gt; type.Name, type
	
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypes("&lt;span style="color: #8b0000"&gt;HibernatingRhinos&lt;/span&gt;").WhereNamespaceEq("&lt;span style="color: #8b0000"&gt;HibernatingRhinos.Services&lt;/span&gt;"):
	&lt;font color="#ff8000"&gt;component&lt;/font&gt; type.GetServiceInterface(), type&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And I am pretty confident that I am not going to have to do much in the future with those.&lt;/p&gt;

&lt;p&gt;And yes, you can do it with the fluent registration API as well. &lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10220.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/05/13/Zero-friction-IoC-Auto-registration-is-mandatory.aspx</guid>
            <pubDate>Tue, 13 May 2008 13:08:57 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10220.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/05/13/Zero-friction-IoC-Auto-registration-is-mandatory.aspx#feedback</comments>
            <slash:comments>6</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10220.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Binsor &amp;amp; Auto Registration - Making it even simpler</title>
            <link>http://ayende.com/Blog/archive/2008/05/13/Binsor-amp-Auto-Registration--Making-it-even-simpler.aspx</link>
            <description>&lt;p&gt;Here is the syntax that I am getting at...&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypesBased of IView("&lt;span style="color: #8b0000"&gt;Rhino.Commons.Test&lt;/span&gt;"):
	component type
	
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypesWithAttribute of ControllerAttribute("&lt;span style="color: #8b0000"&gt;Rhino.Commons.Test&lt;/span&gt;"):
	component type
	
&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypes("&lt;span style="color: #8b0000"&gt;Rhino.Commons.Test&lt;/span&gt;") \
	.WhereNamespaceEq("&lt;span style="color: #8b0000"&gt;Rhino.Commons.Test.Binsor&lt;/span&gt;"):
	component type

&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; type &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; AllTypes("&lt;span style="color: #8b0000"&gt;Rhino.Commons.NHibernate&lt;/span&gt;") \
	.Where({ t &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; System.Type | t.Name.Contains("&lt;span style="color: #8b0000"&gt;NHRepository&lt;/span&gt;") }):
	component "&lt;span style="color: #8b0000"&gt;nh.repos&lt;/span&gt;", type.GetSeriveInterface(), type&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this seems to cover just about any scenario that I can think of. Combine that with Binsor's extend facility, and we are more or less done.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10219.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/05/13/Binsor-amp-Auto-Registration--Making-it-even-simpler.aspx</guid>
            <pubDate>Tue, 13 May 2008 05:21:51 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10219.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/05/13/Binsor-amp-Auto-Registration--Making-it-even-simpler.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10219.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Adaptive Domain Models with Rhino Commons</title>
            <link>http://ayende.com/Blog/archive/2008/03/27/Adaptive-Domain-Models-with-Rhino-Commons.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://udidahan.weblogs.us/"&gt;Udi Dahan&lt;/a&gt; has &lt;a href="http://udidahan.weblogs.us/2007/09/16/fetching-strategy-nhibernate-implementation-available/"&gt;been&lt;/a&gt; &lt;a href="http://udidahan.weblogs.us/2008/03/20/qcon-london-2008-recap/"&gt;talking&lt;/a&gt; &lt;a href="http://udidahan.weblogs.us/2007/04/23/fetching-strategy-design/"&gt;about&lt;/a&gt; this for &lt;a href="http://udidahan.weblogs.us/2007/03/06/better-domain-driven-design-implementation/"&gt;a while now&lt;/a&gt;. As usual, he makes sense, but I am working in different enough context that it takes time to assimilate it.&lt;/p&gt;  &lt;p&gt;At any rate, we have been talking about this for a few days, and I finally sat down and decided that I really need to look at it with code. The result of that experiment is that I like this approach, but am still not 100% sold. &lt;/p&gt;  &lt;p&gt;The first idea is that we need to decouple the service layer from our domain implementation. But why? The domain layer is under the service layer, after all. Surely the service layer should be able to reference the domain. The reasoning here is that the domain model play several different roles in most applications. It is the preferred way to access our persistent information (but they should not be aware of persistence), it is the central place for business logic, it is the representation of our notions about the domain, and much more that I am probably leaving aside.&lt;/p&gt;  &lt;p&gt;The problem here is there is a dissonance between the requirements we have here. Let us take a simple example of an Order entity.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AdaptiveDomainModelswithRhinoCommons_9E6D/image_2.png"&gt;&lt;img style="margin: 0px 15px 0px 0px" height="143" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AdaptiveDomainModelswithRhinoCommons_9E6D/image_thumb.png" width="163" align="left" border="0" /&gt;&lt;/a&gt; As you can see, Order has several things that I can do. It can accept an new line, and it can calculate the total cost of the order.&lt;/p&gt;  &lt;p&gt;But those are two distinct responsibilities that are based on the same entity. What is more, they have completely different persistence related requirements.&lt;/p&gt;  &lt;p&gt;I talked about this issue &lt;a href="http://www.ayende.com/Blog/archive/9042.aspx"&gt;here&lt;/a&gt;, over a year ago. &lt;/p&gt;  &lt;p&gt;So, we need to split the responsibilities, so we can take care of each of them independently. But it doesn't make sense to split the Order entity, so instead we will introduce purpose driven interfaces. Now, when we want to talk about the domain, we can view certain aspect of the Order entity in isolation.&lt;/p&gt;  &lt;p&gt;This leads us to the following design:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AdaptiveDomainModelswithRhinoCommons_9E6D/image_4.png"&gt;&lt;img height="185" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AdaptiveDomainModelswithRhinoCommons_9E6D/image_thumb_1.png" width="355" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;And now we can refer to the separate responsibilities independently. Doing this based on the type open up to the &lt;a href="http://www.ayende.com/Blog/archive/2008/03/25/Non-Invasive-API-take-2.aspx"&gt;non invasive API&lt;/a&gt; &lt;a href="http://www.ayende.com/Blog/archive/2008/03/19/Non-invasive-API--Now-with-an-IoC-container.aspx"&gt;approaches&lt;/a&gt; that I talked about &lt;a href="http://www.ayende.com/Blog/archive/2008/03/19/Avoiding-Invasive-API-design.aspx"&gt;before&lt;/a&gt;. You can read Udi's posts about it to learn more about the concepts. Right now I am more interested in discussing the implementation.&lt;/p&gt;  &lt;p&gt;First, the unit of abstraction that we work in is the IRepository&amp;lt;T&amp;gt;, as always. &lt;/p&gt;  &lt;p&gt;The major change with introducing the idea of a ConcreteType to the repository. Now it will try to use the ConcreteType instead of the give typeof(T) that it was created with. This affects all queries done with the repository (of course, if you don't specify ConcreteType, nothing changes).&lt;/p&gt;  &lt;p&gt;The repository got a single new method:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;T Create();&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This allows you to create new instances of the entity without knowing its concrete type. And that is basically it. &lt;/p&gt;  &lt;p&gt;Well, not really :-)&lt;/p&gt;  &lt;p&gt;I introduced two other concepts as well.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IFetchingStrategy&amp;lt;T&amp;gt;
{
	ICriteria Apply(ICriteria criteria);
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;IFetchingStrategy can interfere in the way queries are constructed. As a simple example, you could build a strategy that force eager load of the OrderLines collection when the IOrderCostCalculator is being queried.&lt;/p&gt;

&lt;p&gt;There is not complex configuration involved in setting up IFetchingStrategy. All you need to do is register your strategies in the container, and let the repository do the rest.&lt;/p&gt;

&lt;p&gt;However, doesn't this mean that we now need to explicitly register repositories for all our entities (and for all their interfaces)?&lt;/p&gt;

&lt;p&gt;Well, yes, but no. Technically we need to do that. But we have help, EntitiesToRepositories.Register, so we can just put the following line somewhere in the application startup and we are done.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;EntitiesToRepositories.Register(
	IoC.Container, 
	UnitOfWork.CurrentSession.SessionFactory, 
	&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (NHRepository&amp;lt;&amp;gt;),
	&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (IOrderCostCalculator).Assembly);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this is it, you can start working with this new paradigm with no extra steps.&lt;/p&gt;

&lt;p&gt;As a side benefit, this really pave the way to complex multi tenant applications. &lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10125.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/27/Adaptive-Domain-Models-with-Rhino-Commons.aspx</guid>
            <pubDate>Thu, 27 Mar 2008 09:16:19 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10125.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/27/Adaptive-Domain-Models-with-Rhino-Commons.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10125.aspx</wfw:commentRss>
        </item>
        <item>
            <title>DI &amp;amp; IoC are not about testing!</title>
            <link>http://ayende.com/Blog/archive/2008/03/26/DI-amp-IoC-are-not-about-testing.aspx</link>
            <description>&lt;p&gt;Dependency Injection &amp;amp; Inversion of Control Containers are not about testing!&lt;/p&gt;  &lt;p&gt;I hear this a lot recently. &lt;a href="http://www.hanselminutes.com/default.aspx?showID=122"&gt;Dave Laribee&lt;/a&gt; mentioned that in the Hanselminutes podcast, The creators of Guise seems to think that this is the &lt;a href="http://video.google.com/videoplay?docid=2948853912335655747&amp;amp;q=OSS+Speaker+Series+-+Java+on+Guide&amp;amp;total=4&amp;amp;start=0&amp;amp;num=10&amp;amp;so=0&amp;amp;type=search&amp;amp;plindex=0"&gt;main concern&lt;/a&gt; (01:30 in the video).&lt;/p&gt;  &lt;p&gt;Oh, those techniques help, obviously. But it is not &lt;em&gt;about&lt;/em&gt; unit testing.&lt;/p&gt;  &lt;p&gt;For me, it is like saying that the main purpose interfaces is to enable easier testing.&lt;/p&gt;  &lt;p&gt;DI &amp;amp; IoC are about decoupling, better flexibility, having a central place to go to and increasing the maintainability of out applications. Testing is important, but that is not the first and foremost reason.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10120.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/26/DI-amp-IoC-are-not-about-testing.aspx</guid>
            <pubDate>Tue, 25 Mar 2008 23:16:21 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10120.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/26/DI-amp-IoC-are-not-about-testing.aspx#feedback</comments>
            <slash:comments>21</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10120.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Non Invasive API, take 2</title>
            <link>http://ayende.com/Blog/archive/2008/03/25/Non-Invasive-API-take-2.aspx</link>
            <description>&lt;p&gt;A few days ago I presented my typical way of handling container driven polymorphism. &lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #008000"&gt;// only use for selection&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IRegion&amp;lt;T&amp;gt; : IRegion {}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetRegion(DependencyObject containerElement, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; regionName)
{
	IRegion region = (IRegion) IoC.TryResolve(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRegion&amp;lt;&amp;gt;).MakeGenericType(containerElement.GetType()));

	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (region != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		_regions.Add(regionName, region);
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I got an email with some good questions, and it is worth a second post to clarify. The questions:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The only thing I don’t like is that a lookup in the form of TryResolve(typeof(IRegion&amp;lt;&amp;gt;).MakeGenericType(containerElement.GetType())) would infer an explicit wrapper type for ever possible UI container type. On one hand it gives me the fine grain control I need to specify a different wrapper for an inherited UI container. But on the other hand, it means that ListBox, TreeView, ComboBox and TabControl would all need their own custom wrappers even though they all derive from ItemsControl and all handle adding and removing child entities in the same way.&lt;/p&gt;

  &lt;p&gt;&lt;a name="_MailEndCompose"&gt;But if &lt;/a&gt;containerElement == typeof(ListBox), won’t TryResolve(typeof(IRegion&amp;lt;&amp;gt;).MakeGenericType(containerElement.GetType())) fail to resolve? TryResolve expects an explicit match. It would need to find a registration for IRegion&amp;lt;ListBox&amp;gt;, not IRegion&amp;lt;ItemContainer&amp;gt; right? (Assuming Unity here.)&lt;/p&gt;

  &lt;p&gt;Even if that worked, what if I want all elements that inherit from ItemContainer to be handled by IRegion&amp;lt;ItemContainer&amp;gt; &lt;i&gt;except&lt;/i&gt; TreeView? Say I need to handle TreeView differently for some reason. What’s a nice way to handle the 80% rule generically but still allow an override for special cases?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My response to that is very simple. Take the same approach, but bigger. Here is all we need to make this happen.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetRegion(DependencyObject containerElement, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; regionName)
{
	Type current = containerElement.GetType()
	&lt;span style="color: #0000ff"&gt;while&lt;/span&gt;(current != &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(DependencyObject))
	{
		IRegion region = (IRegion) IoC.TryResolve(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRegion&amp;lt;&amp;gt;).MakeGenericType(current));
	
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (region != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		{
			_regions.Add(regionName, region);
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;
		}
		
		current = current.BaseType;
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, we can register the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;IRegion&amp;lt;TreeView&amp;gt;&lt;/li&gt;

  &lt;li&gt;IRegion&amp;lt;MyCustomRegion&amp;gt;&lt;/li&gt;

  &lt;li&gt;IRegion&amp;lt;ItemsControl&amp;gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And no matter what we will send to SetRegion, we can modify the behavior merely by changing the registration of IRegion&amp;lt;T&amp;gt; implementations. Note that this doesn't assumes any capacity on the container (except the basic resolve-generic-service one).&lt;/p&gt;

&lt;p&gt;This is also a very natural model to follow, in nearly all cases (there is an issue here regarding whatever we should consider interfaces as well, but that is beside the point for this demo, and I would generally not do that anyway).&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10118.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/25/Non-Invasive-API-take-2.aspx</guid>
            <pubDate>Mon, 24 Mar 2008 22:58:25 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10118.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/25/Non-Invasive-API-take-2.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10118.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Non invasive API - Now with an IoC container</title>
            <link>http://ayende.com/Blog/archive/2008/03/19/Non-invasive-API--Now-with-an-IoC-container.aspx</link>
            <description>&lt;p&gt;I talked about ways to avoid &lt;a href="http://www.ayende.com/Blog/archive/2008/03/19/Avoiding-Invasive-API-design.aspx"&gt;invasive API design&lt;/a&gt;, and a lot of people asked about how to handle this with a container. First, I want to make it clear that the previous example assumed that you can't rely on a container, so it used Poor Man IoC to do that. Now that we can assume that there is a container, this is another matter entirely.&lt;/p&gt; &lt;p&gt;The API design now shifts to allow me to select the proper implementation from the container automatically. This generally ends up being either as a naming convention on top of a fixed interface, or as a generic interface with a given type as a parameter. The decision depends on what you are doing, basically, and the capabilities of your tools.&lt;/p&gt; &lt;p&gt;For myself, I strongly favor the generic interface approach, which would give us the following syntax:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IMessageHandler&amp;lt;TMsg&amp;gt;
	where TMsg : IMessage
{
	&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Handle(TMsg msg);
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; EndPoint
{
	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; HandleMsg(IMessage msg)
	{
		&lt;span style="color: #008000"&gt;// this should be cached and turned into a delegate&lt;/span&gt;
		&lt;span style="color: #008000"&gt;// not reflection call&lt;/span&gt;
		GetGenericHandleMsgMethod().MakeGenericMethod(msg.GetType())
			.Invoke(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[]{msg});
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; HandleMsg&amp;lt;TMsg&amp;gt;(TMsg msg)
		where TMsg : IMessage
	{
		IoC.Resolve&amp;lt;IMessageHandler&amp;lt;TMsg&amp;gt;().Handle(msg);
	}

}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;There is some ugliness in invoking the method with the generic parameter, but this allows you to handle a wide variety of cases very easily.&lt;/p&gt;
&lt;p&gt;Let us take another example, this time it is from the Prism.Services.RegionManagerService:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetRegion(DependencyObject containerElement, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; regionName)
{
	IRegion region = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;

	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (containerElement &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; Panel)
	{
		region = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PanelRegion((Panel)containerElement);
	}
	&lt;span style="color: #0000ff"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (containerElement &lt;span style="color: #0000ff"&gt;is&lt;/span&gt; ItemsControl)
	{
		region = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ItemsControlRegion((ItemsControl)containerElement);
	}

	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (region != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		_regions.Add(regionName, region);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;I don't really like this code, let us see what happens when we introduce the idea of the container as a deeply rooted concept:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #008000"&gt;// only use for selection&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IRegion&amp;lt;T&amp;gt; : IRegion {}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SetRegion(DependencyObject containerElement, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; regionName)
{
	IRegion region = (IRegion) IoC.TryResolve(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(IRegion&amp;lt;&amp;gt;).MakeGenericType(containerElement.GetType()));

	&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (region != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		_regions.Add(regionName, region);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now the code is much clearer, we can extend it from the outside, without modifying anything when we add a new region type. &lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10104.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/19/Non-invasive-API--Now-with-an-IoC-container.aspx</guid>
            <pubDate>Wed, 19 Mar 2008 21:37:41 GMT</pubDate>
            <wfw:comment>http://ayende.com/Blog/comments/10104.aspx</wfw:comment>
            <comments>http://ayende.com/Blog/archive/2008/03/19/Non-invasive-API--Now-with-an-IoC-container.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10104.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Evaluating IoC containers</title>
            <link>http://ayende.com/Blog/archive/2008/03/14/Evaluating-IoC-containers.aspx</link>
            <description>&lt;p&gt;&lt;a href="http://www.hanselman.com/blog/"&gt;Scott Hanselman&lt;/a&gt; is learning IoC, and he &lt;a href="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx"&gt;listed&lt;/a&gt; a lot of the IoC frameworks on the CLR. I was also pointed to this &lt;a href="http://weblogs.asp.net/podwysocki/archive/2008/03/04/ioc-containers-unity-and-objectbuilder2-the-saga-continues.aspx"&gt;comparison&lt;/a&gt; IoC containers.&lt;/p&gt; &lt;p&gt;I don't like that comparison, and I am not sure that I can quite explain it. Most of the time, comparison of IoC container focus on simple dependency resolving scenarios and the related configuration.&lt;/p&gt; &lt;p&gt;From my point of view, it is like deciding to use C# or Java after reading a comparison of the syntax of an if statement.&lt;/p&gt; &lt;p&gt;Dependency resolution is the most basic feature that an IoC container can have. &lt;/p&gt; &lt;p&gt;Of the top of my head (and probably biased toward Windsor, since that is what I tend to use), I would say that you need to think about the following points, instead of focusing on the baseline scenario:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Error handling&lt;/li&gt; &lt;li&gt;Generic components&lt;/li&gt; &lt;li&gt;Decorators&lt;/li&gt; &lt;li&gt;Interception&lt;/li&gt; &lt;li&gt;Configuration vs. Convention (in other words, how much smarts does the container have?)&lt;/li&gt; &lt;li&gt;Extensibility scenarios&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://ayende.com/Blog/aggbug/10096.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/03/14/Evaluating-IoC-containers.aspx</guid>
            <pubDate>Fri, 14 Mar 2008 10:11:48 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/03/14/Evaluating-IoC-containers.aspx#feedback</comments>
            <slash:comments>5</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10096.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Interception as an extensibility mechanism</title>
            <link>http://ayende.com/Blog/archive/2008/01/24/Interception-as-an-extensibility-mechanism.aspx</link>
            <description>&lt;p&gt;I got a request to allow system-mode for Rhino Security, something like this:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(Security.ActAsSystem())
{
	&lt;span style="color: #008000"&gt;// in here the security behaves as if you have permission &lt;/span&gt;
	&lt;span style="color: #008000"&gt;// to do everything&lt;/span&gt;
	&lt;span style="color: #008000"&gt;// queries are not enhanced, etc.&lt;/span&gt;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;It is not something that I really want to allow, so I started to think how we can implement this, I came up with the following solution:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; AuthorizationServiceWithActAsSystemSupport : IAuhorizationService
{
	IAuhorizationService inner;

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; AuthorizationServiceWithActAsSystemSupport(IAuhorizationService inner)
	{
		&lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.inner = innner;
	}

	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsActAsSystem
	{
		&lt;span style="color: #0000ff"&gt;get&lt;/span&gt; { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;.Equals(Local.Data["&lt;span style="color: #8b0000"&gt;act.as.system&lt;/span&gt;"]); }
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsAllowed(IUser user, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; operationName)
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(IsActAsSystem)
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;
		&lt;span style="color: #0000ff"&gt;return&lt;/span&gt; inner.IsAllowed(user, operationName);
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AddPermissionsToQuery(IUser user, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; operationName, ICriteria query)
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(IsActAsSystem)
			&lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;
		inner.AddPermissionsToQuery(user, operationName, query);
	}

	&lt;span style="color: #008000"&gt;// .. the rest&lt;/span&gt;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now, all we need to do is register it first:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre&gt;component IAuthorizationService, AuthorizationServiceWithActAsSystemSupport

faciliy RhinoSecurityFacility&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;And that is it. This both answer the requirement and doesn't add the functionality that I don't like to the system.&lt;/p&gt;
&lt;p&gt;Again, neat.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/10021.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2008/01/24/Interception-as-an-extensibility-mechanism.aspx</guid>
            <pubDate>Thu, 24 Jan 2008 08:31:41 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2008/01/24/Interception-as-an-extensibility-mechanism.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/10021.aspx</wfw:commentRss>
        </item>
        <item>
            <title>AOP: Be aware where your point cuts are</title>
            <link>http://ayende.com/Blog/archive/2007/12/19/AOP-Be-aware-where-your-point-cuts-are.aspx</link>
            <description>&lt;p&gt;So, this issue cause some head scratching today. We are using WIndsor's Automatic Transaction Management with NHibernate's flush-on-commit option, so if a transaction doesn't commit, nothing is written to the database.&lt;/p&gt; &lt;p&gt;Anyway, this is a story about refactoring, and what it showed us. We performed the following refactoring:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_3.png" atomicselection="true"&gt;&lt;img alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_thumb_3.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Some things that is important to understand, the LoginController is decorated with [Transactional], and there is a [Transaction] attribute on CreateUserLoggedInAuditRecord.&lt;/p&gt; &lt;p&gt;When it was on the controller, it just worked. When we moved it to its own class, it didn't work. To be rather more exact, it &lt;em&gt;worked&lt;/em&gt;, it just never committed the transaction. That was &lt;em&gt;weird. &lt;/em&gt;After some head scratching I found out that I forgot to put [Transactional] on the UsageRegistrationImpl. With a small smile of geeky  triumph, I run the code again. It didn't save.&lt;/p&gt; &lt;p&gt;That was really worrying, and I had no idea what was going on. Since this is rarely popular, I repeatedly run the code, hoping that something would turn up and that no one would pull the old quote about &lt;a href="http://www.quotationspage.com/quote/26032.html"&gt;insanity&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;After a few repetitions, I suddenly saw the light.&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_4.png" atomicselection="true"&gt;&lt;img height="301" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_thumb_4.png" width="293" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;It had to do where I placed the pointcut. A pointcut, in AOP terms, is where the AOP can interfere with the running code. Let us take a look at how it worked when we used the LoginController directly. Because we (well, the transaction facility) asked the container to create an interceptor for it, we got the following classes at runtime:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_7.png" atomicselection="true"&gt;&lt;img alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_thumb_7.png" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The login controller is the original class, the login controller proxy was generated at runtime, and any invocation of any of its methods would fire the transaction interceptor, so it would get a chance to create/rollback/commit a transaction if needed. Since those methods are virtual, this means that even if I am calling methods on the same class, they will be intercepted correctly.&lt;/p&gt; &lt;p&gt;Now, when I moved to the interface + implementing class, we have a different behavior. Now, we use the interface pointcuts in order to inject behavior, it looks like this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_10.png" atomicselection="true"&gt;&lt;img height="293" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/AOPBeawarewhereyourpointcutsare_11978/image_thumb_10.png" width="578" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Windsor will create a proxy interface implementation that would call the AOP interceptors and will forward to the UsageRegistrationImpl.&lt;/p&gt; &lt;p&gt;The problem was with the RegisterUserLoggedIn method. It was similar to this:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;&lt;span style="color: #0000ff"&gt;public virtual &lt;/span&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; RegisterUserLoggedIn(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; username)
{
	&lt;span style="color: #008000"&gt;// do other things&lt;/span&gt;
	CreateUserLoggedInAuditRecord(username);
}

[Transaction]
&lt;span style="color: #0000ff"&gt;public virtual&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CreateUserLoggedInAuditRecord(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; username)
{
	&lt;span style="color: #008000"&gt;//do database stuff&lt;/span&gt;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Given the story so far, you can obviously see the problem. When we call the CreateUserLoggedInAuditRecord() method, we call it from the UsageRegistrationImpl class, so we &lt;em&gt;never pass through any of the pointcuts.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;When we used the method from the controller directly, we made a virtual method call, which was intercepted, but since in this case, we were using the interface as our pointcut, this simply by passed the whole thing.&lt;/p&gt;
&lt;p&gt;That was an interesting lesson, and one that I'll need to remember for the future.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9964.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/19/AOP-Be-aware-where-your-point-cuts-are.aspx</guid>
            <pubDate>Wed, 19 Dec 2007 19:00:31 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/19/AOP-Be-aware-where-your-point-cuts-are.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9964.aspx</wfw:commentRss>
        </item>
        <item>
            <title>DSL &amp;amp; IoC - Combining powerful concepts</title>
            <link>http://ayende.com/Blog/archive/2007/12/13/DSL-amp-IoC--Combining-powerful-concepts.aspx</link>
            <description>&lt;p&gt;I was just writing this line of code:&lt;/p&gt; &lt;blockquote&gt;&lt;pre&gt;DslExecuter&amp;lt;SchedulingDslEngine&amp;gt;.Create(“validateWebSiteIsUp”)&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;When it occurred to me that this looks a look like an IoC resolve call. Which got me thinking about combining both concepts together, which gave me the shakes.
&lt;/p&gt;&lt;p&gt;The idea is that we can expose those dependencies like this:
&lt;/p&gt;&lt;blockquote&gt;&lt;pre&gt;customers &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; ICustomerRepository
fraudService &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; IFraudService
alerts &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; IAlertService

&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; customer &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; customers.FindAll(With.Orders):
	&lt;span style="color: #0000ff"&gt;for&lt;/span&gt; order &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; customer.Orders:
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; fraudService.IsFraud(order):
			alerts.ForFaurdOn(order)
			&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; customer.Important:
				alerts.NotifyCustomerAboutFraud(customer)&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Leaving aside the mountain slope code*, consider what is going on here. We are exposing dependencies by specifying the types that we need. We can then get them through the IoC and execute the code in hand.
&lt;/p&gt;&lt;p&gt;This means that the DSL has full access to everything in the application, and is subject to the same rules and behavior as all the other components. From my point of view... &lt;em&gt;sweeeeeet&lt;/em&gt;.
&lt;/p&gt;&lt;p&gt;* First example that popped to mind, yes, I know it is a bad one.&lt;/p&gt;&lt;img src="http://ayende.com/Blog/aggbug/9948.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Ayende Rahien</dc:creator>
            <guid>http://ayende.com/Blog/archive/2007/12/13/DSL-amp-IoC--Combining-powerful-concepts.aspx</guid>
            <pubDate>Thu, 13 Dec 2007 07:42:50 GMT</pubDate>
            <comments>http://ayende.com/Blog/archive/2007/12/13/DSL-amp-IoC--Combining-powerful-concepts.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://ayende.com/Blog/comments/commentRss/9948.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>