<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-385473814989421290</atom:id><lastBuildDate>Wed, 10 Jun 2009 13:35:32 +0000</lastBuildDate><title>Morts Like Us</title><description /><link>http://mortslikeus.blogspot.com/</link><managingEditor>noreply@blogger.com (Markus Zywitza)</managingEditor><generator>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MortsLikeUs" type="application/rss+xml" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-1645768237380222660</guid><pubDate>Fri, 05 Jun 2009 16:58:00 +0000</pubDate><atom:updated>2009-06-05T17:58:18.907+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">ActiveRecord</category><title>Understand ActiveRecord mapping: Collections of simple objects</title><description>&lt;p&gt;Recently I had a post in the Castle user’s group about mapping a collection of enums with ActiveRecord. Since I know it’s just as mapping other simple types, I only referred to the documentation of the Element and ElementType properties of the HasMany attribute. &lt;/p&gt;  &lt;p&gt;But, it turned out it is not really clear to many how to map a collection of simple values using HasMany. Therefore I decided to prepare an explanatory post, which is what you are reading now.&lt;/p&gt;  &lt;h4&gt;&lt;/h4&gt;  &lt;h4&gt;Mapping Basics&lt;/h4&gt;  &lt;p&gt;The problem: I want to hold a collection of enum values, say I have a status enum and want a history of that status persisted. Here is my model:&lt;/p&gt;  &lt;pre style="font-size: small"&gt;namespace EnumMapping&lt;br /&gt;{&lt;br /&gt;	[ActiveRecord]&lt;br /&gt;	public class Article&lt;br /&gt;	{&lt;br /&gt;		[PrimaryKey]&lt;br /&gt;		public virtual int Id { get; set; }&lt;br /&gt;&lt;br /&gt;		[Property]&lt;br /&gt;		public virtual string Author { get; set; }&lt;br /&gt;&lt;br /&gt;		[Property]&lt;br /&gt;		public virtual string Title { get; set; }&lt;br /&gt;&lt;br /&gt;		[Property]&lt;br /&gt;		public virtual Status CurrentStatus { get;set;}&lt;br /&gt;&lt;br /&gt;		[HasMany(...)]&lt;br /&gt;		public virtual IList&lt;status&gt; StatusHistory&lt;br /&gt;		{&lt;br /&gt;			get { return statusHistory; }&lt;br /&gt;			set { statusHistory = value; }&lt;br /&gt;		}&lt;br /&gt;		private IList&lt;status&gt; statusHistory = new List&lt;status&gt;();&lt;br /&gt;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public enum Status&lt;br /&gt;	{&lt;br /&gt;		None, Planned, InWriting, InEditing, Released &lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The ellipsis in the HasMany attribute is by design. I will work out in this post what you have to put here, so I let it out to make you understand the post, and not just read the code and copy and paste it.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To understand what information ActiveRecord requires to map this collection, we first will take a look into how these collections are saved in the database:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_zi65ycu3guI/SilOoTgIEBI/AAAAAAAAAB0/f9emoOlgCo0/image%5B3%5D.png?imgmax=800" width="465" height="166" /&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As expected, we have to tables. The Article table stores all the simple properties, but it cannot store collections. In a relational model, collections are always modeled by foreign key relations. That means we need a second table that stores both the values and the link to the article in question. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Note: You might have noticed that the StatusHistory table does not have a primary key. This is because of the bag semantics we currently have for this collection. More efficient semantics are shown later.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Looking at the StatusHistory table again, we see what we have to tell ActiveRecord to fetch the values for us and populate our List:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;What is the table’s name? &lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/_zi65ycu3guI/SilOpN5JxGI/AAAAAAAAAB8/Y0Xx1F0oKo0/image%5B7%5D.png?imgmax=800" width="221" height="111" /&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We take that name and put it into the respective property of the HasMany attribute:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: small"&gt;[HasMany(Table=&amp;quot;StatusHistory&amp;quot;,&lt;br /&gt;	...)]&lt;br /&gt;public virtual IList&lt;status&gt; StatusHistory {...}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;As you can see from the code, we are not through yet.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Which column refers to the article?&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_zi65ycu3guI/SilOpvggQUI/AAAAAAAAACA/ENu_ujOqr-s/image%5B11%5D.png?imgmax=800" width="221" height="111" /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: small"&gt;[HasMany(Table = &amp;quot;StatusHistory&amp;quot;,&lt;br /&gt;	ColumnKey = &amp;quot;article&amp;quot;,&lt;br /&gt;	...)]&lt;br /&gt;public virtual IList&lt;status&gt; StatusHistory {...}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Which column holds the value?&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_zi65ycu3guI/SilOp77rsLI/AAAAAAAAACE/m8hNcTu0ODw/image%5B15%5D.png?imgmax=800" width="221" height="111" /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: small"&gt;[HasMany(Table = &amp;quot;StatusHistory&amp;quot;,&lt;br /&gt;	ColumnKey = &amp;quot;article&amp;quot;,&lt;br /&gt;	Element = &amp;quot;status&amp;quot;,&lt;br /&gt;	...)]&lt;br /&gt;public virtual IList&lt;status&gt; StatusHistory {...}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;What? There is even more to specify even though we already have all columns included? &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Yes. ActiveRecord also needs to know the types of the columns. The foreign key’s column can be inferred from the types primary key, but we need to know what type the value actually has.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: small"&gt;[HasMany(Table = &amp;quot;StatusHistory&amp;quot;,&lt;br /&gt;	ColumnKey = &amp;quot;article&amp;quot;,&lt;br /&gt;	Element = &amp;quot;status&amp;quot;,&lt;br /&gt;	ElementType = typeof(Status))]&lt;br /&gt;public virtual IList&lt;status&gt; StatusHistory {...}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;That’s it (for now). We now can take that mapping and go for the database.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The Collection Types&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;I already mentioned the missing primary key on the collection’s table and that the collection uses bag semantics. So what are these semantics? There are a few available in ActiveRecord:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Bag: All elements are unordered and may appear multiple. Total chaos. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Set: The elements are unordered, but each one can be present only once. This is the typical relational collection. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;List: Elements are ordered and may be there multiple times. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Dictionary: Elements have a key that identifies them. &lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If the collection type is not specified, ActiveRecord assumes that you want bag semantics. After all, that’s what a collection is. Put things in there and get them out. No one cares for ordering or duplication.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Unfortunately, this is quite inefficient with databases:Items in a bag cannot be updated individually. Recall that there is no primary key in the collection table. Since the value can be in the collection more than once, an update or delete would effect all rows with that value. As a result, every time the collection is updated, it is completely deleted from the DB and inserted again.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To get around this, we could use a set. However, this requires us to use another Collection type, Set&amp;lt;Status&amp;gt; from Iesi.Collections. So instead of a set, I will use a list semantic. This is suitable, because a status history has an implicit ordering.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To use the list semantic, the schema must be changed first, because an index column is needed to store the list’s index.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/_zi65ycu3guI/SilOqYZ9UtI/AAAAAAAAACI/hHPHJrlo0hM/image%5B23%5D.png?imgmax=800" width="474" height="141" /&gt; &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;We now don’t have only an additional column, we also have a nice primary key consisting of the combination of the article’s id and the list’s index. So every value in the list can now be updated individually. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now we must tell ActiveRecord to use list semantics and how to find the index:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre style="font-size: small"&gt;[HasMany(Table = &amp;quot;StatusHistory&amp;quot;,&lt;br /&gt;	ColumnKey = &amp;quot;article&amp;quot;,&lt;br /&gt;	Element = &amp;quot;status&amp;quot;,&lt;br /&gt;	ElementType = typeof(Status),&lt;br /&gt;	RelationType = RelationType.List,&lt;br /&gt;	Index = &amp;quot;idx&amp;quot;)]&lt;br /&gt;public virtual IList&lt;status&gt; StatusHistory {...}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;That’s it.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-1645768237380222660?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2009/06/understand-activerecord-mapping.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-7765754632523154898</guid><pubDate>Fri, 08 May 2009 21:01:00 +0000</pubDate><atom:updated>2009-05-08T22:01:36.127+01:00</atom:updated><title>ActiveRecord Alpha Release</title><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;You will have already heard it: It is release season in the Castle’s domain.&lt;/p&gt;  &lt;p&gt;The newest release is ActiveRecord 2.0 Alpha 1, recently uploaded to &lt;a title="http://groups.google.com/group/castle-project-users/web/ActiveRecord2.0Alpha1.zip" href="http://groups.google.com/group/castle-project-users/web/ActiveRecord2.0Alpha1.zip"&gt;http://groups.google.com/group/castle-project-users/web/ActiveRecord2.0Alpha1.zip&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This release includes a lot of breaking changes with regard to the last release, ActiveRecord 1.0 RC3, which is the main reason for going through a new complete release cycle starting with alpha releases.&lt;/p&gt;  &lt;p&gt;This release includes a move from NHibernate 1.2 to NHibernate 2.1. Please make sure to read the &lt;a href="http://groups.google.com/group/castle-project-users/web/AR2.0Alpha1_Changes.txt" target="_blank"&gt;Changes.txt&lt;/a&gt; and start testing early. &lt;/p&gt;  &lt;p&gt;Bugs can be posted at &lt;a href="http://support.castleproject.org/projects/AR/issues/new" target="_blank"&gt;donjon&lt;/a&gt; and last-minute-features can be suggested at &lt;a href="http://castle.uservoice.com" target="_blank"&gt;UserVoice&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;There is no feature freeze yet: All suggestions on &lt;a href="http://castle.uservoice.com" target="_blank"&gt;UserVoice&lt;/a&gt; that get 15+ votes there until May 17th, will be considered for addition in Alpha 2. If no features take this hurdle, I will move forward to Beta 1.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-7765754632523154898?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2009/05/activerecord-alpha-release.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-3383341144707560436</guid><pubDate>Thu, 23 Apr 2009 09:06:00 +0000</pubDate><atom:updated>2009-04-23T10:06:12.432+01:00</atom:updated><title>Understanding ActiveRecord Sessions 2</title><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;This article series deals with the internal session keeping of Castle ActiveRecord. This installment introduces the concept of a scope and how scopes are used within ActiveRecord.&lt;/p&gt;  &lt;h3&gt;Introducing Scopes&lt;/h3&gt;  &lt;p&gt;Having a session per database call is suboptimal. There is no chance to use transactions, which is a showstopper for most serious uses. Another issue is lazy loading: Lazy loading prevents NHibernate from loading large collections until they are requested, avoiding to load unnecessary data. Lazy loading is however only possible when done within a single session.&lt;/p&gt;  &lt;p&gt;Castle ActiveRecord uses scopes to overcome this. A scope represents a single unit of work. It is not necessarily a single transaction, but may span multiple transactions. Prior to NHibernate 2.0, when transactions were not mandatory, scopes did not even have to support transactions.&lt;/p&gt;  &lt;p&gt;The scopes that are part of the ActiveRecord package do always share a single session that is used for all persistance related calls. However, it is possible to implement a scope that uses a new session, but the changed semantic should be clearly communicated in such a case.&lt;/p&gt;  &lt;h4&gt;Localizing Scopes&lt;/h4&gt;  &lt;p&gt;In order to access scopes without holding a reference to the object, they stored in thread static stacks. The central interface for this is IThreadScopeInfo, which is implemented by various classes to cover different situations such as web applications. WebThreadScopeInfo for example uses a HttpContext for storing the scopes while the default ThreadScopeInfo implementation uses a thread static field.&lt;/p&gt;  &lt;p&gt;In order to acquire the current IThreadScopeInfo implementation, ThreadScopeAccessor can be used. This class is a singleton providing the current ScopeInfo under&lt;/p&gt;  &lt;pre&gt;public IThreadScopeInfo ScopeInfo&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Additionally it acts a proxy to this scope, delegating calls to the scope info in the property above. This allows to access the current scope without keeping a field for it using&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;ThreadScopeAccessor.Instance.XXX();&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;where XXX is one of the methods defined in IThreadScopeInfo.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;With the IThreadScopeInfo available, the current scope can be requested by SessionFactoryHolder using the following methods:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;ISessionScope GetRegisteredScope()&lt;br /&gt;bool HasInitializedScope&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Scope Initialization&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When a scope is created, it has no sessions stored at first. That is intentional. While the ActiveRecordStarter configures the ISessionFactoryHolder at startup, it cannot configure an arbitrary number of ISessionScope implementations.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Therefore the initialization of scopes is implemented using a simple protocol between ISessionFactoryHolder and ISessionScope:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;Upon creation, the ISessionScope registers itself with the current IThreadScopeInfo.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;void IThreadScopeInfo.RegisterScope(ISessionScope scope)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;ISessionFactoryHolder fetches the scope the next time it has to deliver a session to the ActiveRecordBase methods.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;bool IThreadScopeInfo.HasInitializedScope&lt;br /&gt;ISessionScope IThreadScopeInfo.GetRegisteredScope()&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;The ISessionFactoryHolder asks the ISessionScope whether it already has a suitable session stored. Scopes do not hold only one session. Due to different database connections by root type, they need to hold a dictionary of sessions. The scope doesn't know the key to the sessions because the key is provided by the ISessionFactoryHolder everytime a session is required.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;bool ISessionScope.IsKeyKnown(object key)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;If there is a session registered with the scope, the session is requested and the protocol ends.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;ISession ISessionScope.GetSession(object key)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;If there is no suitable session available the ISessionFactoryHolder asks the ISessionScope whether it accepts an existing session or if it wants to create its own session.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;bool ISessionScope.WantsToCreateTheSession&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;Depending on the answer to 5, the ISessionFactoryHolder either opens a session itself or provides a suitable ISessionFactory to the ISessionScope so that the session can be created by the scope itself.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;ISession ISessionScope.OpenSession(ISessionFactory sessionFactory, IInterceptor interceptor)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;The session is registered with the ISessionScope by the holder. This is done regardless who in fact created the session.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;void ISessionScope.RegisterSession(object key, ISession session)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;&lt;br /&gt;    &lt;p&gt;The freshly registered session is fetched from the scope.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;    &lt;pre&gt;ISession ISessionScope.GetSession(object key)&lt;/pre&gt;&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The different Scope Types&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Scopes generelly have two attributes that describe there behaviour: by FlushAction and by SessionScopeType. The FlushAction controls whether changes are automatically flushed to the database. The SessionScopeType describes the behaviour of the scope in common, most important whether the scope supports transactions.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If the FlushAction is defined as FlushAction.Never, no writing to the database will occur unless ISessionScope.Flush() is called by the using code. This behaviour gives full control to the using code, but requires it to control flushing. This is important: If the changes are not flushed, queries will still find an older state in the database although the entity has already changed.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;FlushAction.Auto instructs the NHibernate session to flush its first-level-cache whenever needed. That means that if the cache contains an unflushed change to an entity, the session will write those changes back before it runs a query against the entity's type.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The SessionScopeType has four values:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;SessionScopeType.Undefined&lt;br /&gt;SessionScopeType.Simple&lt;br /&gt;SessionScopeType.Transactional&lt;br /&gt;SessionScopeType.Custom&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Undefined should never be used; it is more an error state than a valid scope type. Simple and Transactional define whether the scope supports transactional behaviour. Custom can be used for own implementations that fall in between. An example would be a SessionScope that uses transactions only for specific sessions.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The next part of the series will show how ActiveRecord usage differs when using a scope.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-3383341144707560436?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2009/04/understanding-activerecord-sessions-2.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-8902306299756615993</guid><pubDate>Tue, 21 Apr 2009 12:28:00 +0000</pubDate><atom:updated>2009-04-21T13:28:29.501+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ActiveRecord</category><category domain="http://www.blogger.com/atom/ns#">Sessions</category><title>Understanding ActiveRecord Sessions 1</title><description>&lt;p&gt;This article series deals with the internal session keeping of Castle ActiveRecord. It begins in explaining how sessions are managed within ActiveRecord and how the user can influence this behavior.&lt;/p&gt;  &lt;h3&gt;What happens when Save() is called?&lt;/h3&gt;  &lt;p&gt;The first part of our journey starts in &lt;em&gt;ActiveRecordBase&lt;/em&gt;. Whenever a method is called that requires a NHibernate session, it requests one from the following field:&lt;/p&gt;  &lt;pre&gt;protected internal static ISessionFactoryHolder holder;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Since that field is marked protected internal static, subclasses of &lt;em&gt;ActiveRecordBase&lt;/em&gt; can directly access it to acquire a session. This is done with the following methods:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;ISession CreateSession(Type type);&lt;br /&gt;void ReleaseSession(ISession session);&lt;br /&gt;void FailSession(ISession session);&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The first method requests a session from the &lt;em&gt;ISessionFactoryHolder&lt;/em&gt;, that can be used to issue calls to the session, such as &lt;em&gt;Save()&lt;/em&gt;, &lt;em&gt;Load()&lt;/em&gt; etc. After the operation was completed, &lt;em&gt;ReleaseSession&lt;/em&gt; must be called, preferably in a finally block.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If an exception is raised from NHibernate, the session cannot be used anymore. The &lt;em&gt;ISessionFactoryHolder&lt;/em&gt; must be notified through the &lt;em&gt;FailSession&lt;/em&gt;-method.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;To further understand the inner workings of ActiveRecord, we need to look at the default implementation of &lt;em&gt;ISessionFactoryHolder&lt;/em&gt;, which is simply called &lt;em&gt;SessionFactoryHolder&lt;/em&gt;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The &lt;em&gt;SessionFactoryHolder&lt;/em&gt; keeps a dictionary which maps the configured ActiveRecord root types to &lt;em&gt;ISessionFactory&lt;/em&gt; instances. Whenever &lt;em&gt;CreateSession&lt;/em&gt; is called, it fetches the &lt;em&gt;ISessionFactory&lt;/em&gt; for that type and creates an &lt;em&gt;ISession&lt;/em&gt;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When &lt;em&gt;ReleaseSession&lt;/em&gt; is called the session is flushed and disposed, closing any open connections. If you use the holder to acquire sessions directly, keep in mind that it is necessary to release them or you will get a resource leak. Database sessions are among the most critical resources with regard to leaks.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If an exception is raised, the &lt;em&gt;FailSession&lt;/em&gt; method will clear the session so that it doesn't throw again when the session is released.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;When ActiveRecord is used without any scopes, all of this happens on a single call of any of the data retrieval or persistence methods (Save(), FindAll() etc.):&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;  &lt;li&gt;ActiveRecordBase gets an ISession from the SessionFactoryHolder. ActiveRecordMediator simpy calls a static method on ActiveRecordBase, so there are no differences with respect to session handling. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;ActiveRecordBase performs the desired database operation. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;If there is no exception, ReleaseSession is called by ActiveRecordBase. In case of an exception, FailSession is called instead and the exception is rethrown. &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;SessionFactoryHolder closes the ISession instance. Upon the next call, a new session is created. &lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;But what if we need to have a single session span multiple commands? This will be handled in the following articles when we talk about scopes.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-8902306299756615993?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2009/04/understanding-activerecord-sessions-1.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-4341296983401638212</guid><pubDate>Tue, 27 Jan 2009 12:14:00 +0000</pubDate><atom:updated>2009-01-27T12:14:39.294Z</atom:updated><title>Active Record and DDD</title><description>&lt;p&gt;One of the most fatal mistakes one can conduct when beginning with Domain-Driven Design is doubling Active Record types as domain entities. This does not include Castle ActiveRecord, but all frameworks that map classes and tables one-to-one, and to some extend even more flexible solutions like NHibernate.&lt;/p&gt;  &lt;h4&gt;How does it start?&lt;/h4&gt;  &lt;p&gt;This trap is usually hit by developing a data-driven applications using ActiveRecord as an ORM. There is nothing bad with the approach per se, and I'm actually using it myself a lot. Let's see the following code taken from the Castle ActiveRecord GettingStarted section:&lt;/p&gt;  &lt;pre&gt;[ActiveRecord]&lt;br /&gt;public class Blog : ActiveRecordBase&amp;lt;Blog&amp;gt;&lt;br /&gt;{&lt;br /&gt;	private int id;&lt;br /&gt;	private String name;&lt;br /&gt;	private String author;&lt;br /&gt;	private IList&amp;lt;Post&amp;gt; posts = new List&amp;lt;Post&amp;gt;();&lt;br /&gt;&lt;br /&gt;	public Blog()&lt;br /&gt;	{&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	public Blog(String name)&lt;br /&gt;	{&lt;br /&gt;		this.name = name;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	[PrimaryKey]&lt;br /&gt;	public int Id&lt;br /&gt;	{&lt;br /&gt;		get { return id; }&lt;br /&gt;		set { id = value; }&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	[Property]&lt;br /&gt;	public String Name&lt;br /&gt;	{&lt;br /&gt;		get { return name; }&lt;br /&gt;		set { name = value; }&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	[Property]&lt;br /&gt;	public String Author&lt;br /&gt;	{&lt;br /&gt;		get { return author; }&lt;br /&gt;		set { author = value; }&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	[HasMany(&lt;br /&gt;		Table=&amp;quot;Posts&amp;quot;, ColumnKey=&amp;quot;blogid&amp;quot;, &lt;br /&gt;		Inverse=true, Cascade= ManyRelationCascadeEnum.AllDeleteOrphan)]&lt;br /&gt;	public IList&amp;lt;Post&amp;gt; Posts&lt;br /&gt;	{&lt;br /&gt;		get { return posts; }&lt;br /&gt;		set { posts = value; }&lt;br /&gt;	}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is straight forward data-driven code and there is nothing bad about it. Note that no business logic is embedded in the class. In the simple GettingStarted example, the logic is buried in the GUI, but in a real application you would perhaps use Transaction Scripts to encapsulate logic in objects.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Setting the trap&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The programmer eventually reads Eric Evans great book or hears from a mailing list about DDD. He might remember that Castle ActiveRecord does not require a base class and removes it, using ActiveRecordMediator for database access.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now that there are only POCOs, our unwary programmer starts adding business logic to the ActiveRecord types. By that, he tries to encapsulate complexity within the &amp;quot;domain model&amp;quot;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;But what has happened:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;  &lt;li&gt;Most important, he violates the Single Responsibility Principle (SRP); the class is now responsible for multiple aspects:&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;  &lt;ul&gt;&lt;br /&gt;    &lt;li&gt;Storing data&lt;/li&gt;&lt;br /&gt;&lt;br /&gt;    &lt;li&gt;Executing business logic&lt;/li&gt;&lt;br /&gt;  &lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;In the first few iterations, business logic that was previously packed in one method is now cluttered over multiple classes. DDD's supple design promises to mitigate that but a design usually only becomes supple by a lot of refactoring.&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The immediate result is big step backwards in maintainability. Over the long term, DDD will have a better maintainability, but you will need a lot of work to reach this state. By that time, the trap has already sprung...&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;The trap fires&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For a while, all will be well. The programmer get accustomed to the code and does some changes. The code gets more complex and a bit unwieldy. Finally, the programmer needs a &amp;quot;break-through&amp;quot;; a big refactoring takes place to make the code more supple.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now, violating the SRP fires back: It is not possible to refactor the design without writing complex migration scripts for the database. Integration suddenly becomes an issue. However, the redesign is utterly needed because of the business logic embedded in the design.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The typical outcome is that the redesign is put off until &amp;quot;there is more time&amp;quot;, or shorter: &amp;quot;never&amp;quot;. In the meanwhile, the code base is growing and getting more and more fragile.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;How to recover?&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The most important task in recovering from such a dilemma is deciding which approach will be used for the application. You can choose a data-driven approach or a domain-driven approach, but not both. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Using DDD&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;If the complexity of the domain mandates DDD, it is necessary to do it right. This means using the ActiveRecord types as a DAO/DTO layer and building a model upon it that contains the business logic. The model is then decoupled from the data structure. If the model is redesigned, the mapping code requires to adapt, not the structure of the data storage. If the storage structure changes, the mapping code changes and not the model.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;This is also the reason why it is possible to use NHibernate directly on a domain model: The NHibernate mapping files are mapping code, written in an XML-based DSL (and soon with a fluent API)&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;So this is the other way out of the trap when using DDD. If you use Castle ActiveRecord, take the hbm-files created when using the debug switch and use NHibernate directly instead.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Using data-driven design&lt;/h5&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;It is also possible to make a full turn. A domain of modest complexity that defines most of the business cases as sequential workflows and processes, will benefit from using a data-driven approach.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The processes defined in the business domain can be modeled using the Transaction Script pattern and the Active Record model is exactly that: a pattern for accessing an underlying database. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;On the &amp;quot;Anemic Model&amp;quot;&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Many of the solutions above use a model that is disregarded as anemic by many. But whether a model is anemic, depends on responsibilities rather than on LOC.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Thus an ActiveRecord-model is not anemic because it is responsible for accessing the data store. A DAO/DTO is part of the persistence layer and its responsibility is passing data around. By coincidence, it doesn't need any methods for this task, but it is not anemic.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;After all, violating the SRP is always worse than having an anemic model.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Disclaimer&lt;/h4&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The poor programmer who unwarily builds a trap who fired at himself was of course me.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-4341296983401638212?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2009/01/active-record-and-ddd.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-8592927270395957004</guid><pubDate>Wed, 19 Mar 2008 15:30:00 +0000</pubDate><atom:updated>2008-03-19T14:37:24.194Z</atom:updated><title>Emulating MixIns with C#</title><description>One of the shortcomings of C# is the missing support of MixIns, which are a popular substitute for multiple inheritance especially in Ruby.&lt;br /&gt;MixIns provide a way to put a common functionality in a separate classand use it in another class. That is still easy in C#, one can simply use inheritance. But, what will you do, when your classes are already inheriting from other classes? You won't want to break up your inheritance hierarchie for this, will you?&lt;br /&gt;&lt;br /&gt;Using a MixIn, you can put a common functionality in a separate class and mix it into multiple classes, regardless of their inheritance hierarchy. The methods and properties of a MixIn-class are merged into the using classes' interface and can also use the original classes members. That's the theory and what is possible in Ruby, but can it be achieved in C#?&lt;br /&gt;&lt;br /&gt;If you need an example for MixIns, you might want to read the following blog post &lt;a href="http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/"&gt;http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/a&gt;. However, you don't need any Ruby knowledge for the remainder of my post.&lt;br /&gt;&lt;br /&gt;Ok, what are the characteristics of a MixIn again:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Accessible via the using class&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Has access to the using class' instance members&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Must not use inheritance&lt;/li&gt;&lt;/ol&gt;&lt;p&gt;Taking into account that C# is a statically and strongly typed compiled language, this is not possible at first glance. But, if all possibilities of C# are used, they can be fulfilled at least partially.&lt;/p&gt;&lt;p&gt;As an example, I will develop a MixIn that adds reduction-functionality to collections. Reduction is a functional programming concept means that the collection will be reduced (boiled down) to a scalar value using a user-specified delegate. Simple reductions are joining a collection of strings or adding up a collection of integers.&lt;/p&gt;&lt;p&gt;The following code shows how this can be implemented using a conventional static method:&lt;/p&gt;&lt;div style="BACKGROUND: white;font-family:Courier New;font-size:8pt;color:black;"   &gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    2&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    3&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; Example&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    4&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    5&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;delegate&lt;/span&gt; TItem &lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;TItem&amp;gt;(&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    6&lt;/span&gt;         TItem firstItem, &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    7&lt;/span&gt;         TItem secondItem);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    8&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    9&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;Reduction&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   10&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   11&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;static&lt;/span&gt; TItem Reduce&amp;lt;TItem&amp;gt;(&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   12&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;TItem&amp;gt; collection, &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   13&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;TItem&amp;gt; reduceFunction)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   14&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   15&lt;/span&gt;             TItem result = &lt;span style="color:blue;"&gt;default&lt;/span&gt;(TItem);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   16&lt;/span&gt;             &lt;span style="color:blue;"&gt;bool&lt;/span&gt; first = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   17&lt;/span&gt;             &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (TItem item &lt;span style="color:blue;"&gt;in&lt;/span&gt; collection)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   18&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   19&lt;/span&gt;                 &lt;span style="color:blue;"&gt;if&lt;/span&gt; (first)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   20&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   21&lt;/span&gt;                     result = item;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   22&lt;/span&gt;                     first = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   23&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   24&lt;/span&gt;                 &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   25&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   26&lt;/span&gt;                     result = reduceFunction(result, item);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   27&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   28&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   29&lt;/span&gt;             &lt;span style="color:blue;"&gt;return&lt;/span&gt; result;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   30&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   31&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   32&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;Now the question is, how can this functionality be implemented as a mixin. We will need at least to interfaces for this:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;An interface that allows the mixin access to the using class.&lt;/li&gt;&lt;li&gt;An external interface that allows a client to use the functionality provided by the mixins.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The code below shows how these are declared:&lt;/p&gt;&lt;br /&gt;&lt;div style="BACKGROUND: white;font-family:Courier New;font-size:8pt;color:black;"   &gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; Mixin&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    2&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    3&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;delegate&lt;/span&gt; TItem &lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;TItem&amp;gt;(&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    4&lt;/span&gt;         TItem firstItem, &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    5&lt;/span&gt;         TItem secondItem);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    6&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    7&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IReduceClient&lt;/span&gt;&amp;lt;TItem&amp;gt; : &lt;span style="color:#2b91af;"&gt;IEnumerable&lt;/span&gt;&amp;lt;TItem&amp;gt;{}&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    8&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    9&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IReduceMixin&lt;/span&gt;&amp;lt;TItem&amp;gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   10&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   11&lt;/span&gt;         TItem Reduce(&lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;TItem&amp;gt; reduceFunction);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   12&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   13&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   14&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;interface&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IWithReduce&lt;/span&gt;&amp;lt;TItem&amp;gt; : &lt;span style="color:#2b91af;"&gt;IReduceClient&lt;/span&gt;&amp;lt;TItem&amp;gt;, &lt;span style="color:#2b91af;"&gt;IReduceMixin&lt;/span&gt;&amp;lt;TItem&amp;gt; {}&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   15&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   16&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReduceMixin&lt;/span&gt;&amp;lt;TItem&amp;gt; : &lt;span style="color:#2b91af;"&gt;IReduceMixin&lt;/span&gt;&amp;lt;TItem&amp;gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   17&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   18&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; ReduceMixin(&lt;span style="color:#2b91af;"&gt;IReduceClient&lt;/span&gt;&amp;lt;TItem&amp;gt; client)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   19&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   20&lt;/span&gt;             &lt;span style="color:blue;"&gt;this&lt;/span&gt;.client = client;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   21&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   22&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   23&lt;/span&gt;         &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;IReduceClient&lt;/span&gt;&amp;lt;TItem&amp;gt; client;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   24&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   25&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; TItem Reduce(&lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;TItem&amp;gt; reduceFunction)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   26&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   27&lt;/span&gt;             TItem result = &lt;span style="color:blue;"&gt;default&lt;/span&gt;(TItem);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   28&lt;/span&gt;             &lt;span style="color:blue;"&gt;bool&lt;/span&gt; first = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   29&lt;/span&gt;             &lt;span style="color:blue;"&gt;foreach&lt;/span&gt; (TItem item &lt;span style="color:blue;"&gt;in&lt;/span&gt; client)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   30&lt;/span&gt;             {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   31&lt;/span&gt;                 &lt;span style="color:blue;"&gt;if&lt;/span&gt; (first)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   32&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   33&lt;/span&gt;                     result = item;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   34&lt;/span&gt;                     first = &lt;span style="color:blue;"&gt;false&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   35&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   36&lt;/span&gt;                 &lt;span style="color:blue;"&gt;else&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   37&lt;/span&gt;                 {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   38&lt;/span&gt;                     result = reduceFunction(result, item);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   39&lt;/span&gt;                 }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   40&lt;/span&gt;             }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   41&lt;/span&gt;             &lt;span style="color:blue;"&gt;return&lt;/span&gt; result;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   42&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   43&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   44&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Line 7 declares the interface that the mixin uses to access the using class (IReduceClient). This can be arbitrary complex, but in this case, we only need an enumeration, so I just inherited from IEnumerable. Line 9 declares the interface that is publicly used to access the mixin's functionality (IReduceMixin). IWithReduce on line 14 just brackets the other two interfaces, so that an using class can specify IWithReduce, which shows the intention better than using the two different interfaces.&lt;/p&gt;&lt;p&gt;The mixin class is shown on lines 16ff. It simply holds a reference to the client, a.k.a. the using class and the Reduce implementation shown above.&lt;/p&gt;&lt;p&gt;Now the problem is how can the mixin added to a client. The code below would be ideal, but won't work with C# 2.0:&lt;/p&gt;&lt;br /&gt;&lt;div style="BACKGROUND: white;font-family:Courier New;font-size:8pt;color:black;"   &gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; Client&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    2&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    3&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color:#2b91af;"&gt;IWithReduce&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    4&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    5&lt;/span&gt;         &lt;span style="color:green;"&gt;// Won't work that way!&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    6&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    7&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Before I show a (naive) implementation, here is the test code for the whole thing, that shows how it is used:&lt;/p&gt;&lt;br /&gt;&lt;div style="BACKGROUND: white;font-family:Courier New;font-size:8pt;color:black;"   &gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; Test&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    2&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    3&lt;/span&gt;     [&lt;span style="color:#2b91af;"&gt;TestFixture&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    4&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReduceTest&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    5&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    6&lt;/span&gt;         [Test]&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    7&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;void&lt;/span&gt; TestMixin()&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    8&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    9&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; rl = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   10&lt;/span&gt;             rl.AddRange(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;string&lt;/span&gt;[] {&lt;span style="color:#a31515;"&gt;"a"&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;"b"&lt;/span&gt;, &lt;span style="color:#a31515;"&gt;"c"&lt;/span&gt;});&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   11&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   12&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;IReduceMixin&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt; reducable = rl;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   13&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color:#a31515;"&gt;"a,b,c"&lt;/span&gt;, reducable.Reduce(&lt;span style="color:blue;"&gt;delegate&lt;/span&gt;(&lt;span style="color:blue;"&gt;string&lt;/span&gt; s1, &lt;span style="color:blue;"&gt;string&lt;/span&gt; s2) { &lt;span style="color:blue;"&gt;return&lt;/span&gt; s1 + &lt;span style="color:#a31515;"&gt;","&lt;/span&gt; + s2; }));&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   14&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   15&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;gt; rli = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   16&lt;/span&gt;             rli.AddRange(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:blue;"&gt;int&lt;/span&gt;[] {1, 2, 3, 4});&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   17&lt;/span&gt;             &lt;span style="color:#2b91af;"&gt;Assert&lt;/span&gt;.AreEqual(10, rli.Reduce(&lt;span style="color:blue;"&gt;delegate&lt;/span&gt;(&lt;span style="color:blue;"&gt;int&lt;/span&gt; i, &lt;span style="color:blue;"&gt;int&lt;/span&gt; j) { &lt;span style="color:blue;"&gt;return&lt;/span&gt; i + j; }));&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   18&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   19&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   20&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;R# Jedi&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;One simple, naive and somewhat cheating method to make the ReducableList work, is using ReSharper. Just add a IRecudeMixin&lt;t&gt; field and choose Alt+Ins/Delegate members. As a finishing touch you need to initialize the field. &lt;/p&gt;&lt;br /&gt;&lt;p&gt;The resulting code is shown below:&lt;/p&gt;&lt;br /&gt;&lt;div style="BACKGROUND: white;font-family:Courier New;font-size:8pt;color:black;"   &gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    1&lt;/span&gt; &lt;span style="color:blue;"&gt;namespace&lt;/span&gt; Client&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    2&lt;/span&gt; {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    3&lt;/span&gt;     &lt;span style="color:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReducableList&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;T&amp;gt;, &lt;span style="color:#2b91af;"&gt;IWithReduce&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    4&lt;/span&gt;     {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    5&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; ReducableList()&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    6&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    7&lt;/span&gt;             reducer = &lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReduceMixin&lt;/span&gt;&amp;lt;T&amp;gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    8&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;    9&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   10&lt;/span&gt;         &lt;span style="color:green;"&gt;// Other constructors omitted for brevity&lt;/span&gt;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   11&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   12&lt;/span&gt;         &lt;span style="color:blue;"&gt;private&lt;/span&gt; &lt;span style="color:blue;"&gt;readonly&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;ReduceMixin&lt;/span&gt;&amp;lt;T&amp;gt; reducer;&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   13&lt;/span&gt; &lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   14&lt;/span&gt;         &lt;span style="color:blue;"&gt;public&lt;/span&gt; T Reduce(&lt;span style="color:#2b91af;"&gt;ReduceDelegate&lt;/span&gt;&amp;lt;T&amp;gt; reduceFunction)&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   15&lt;/span&gt;         {&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   16&lt;/span&gt;             &lt;span style="color:blue;"&gt;return&lt;/span&gt; reducer.Reduce(reduceFunction);&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   17&lt;/span&gt;         }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   18&lt;/span&gt;     }&lt;/pre&gt;&lt;pre style="MARGIN: 0px"&gt;&lt;span style="color:#2b91af;"&gt;   19&lt;/span&gt; }&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;Well, I know that this is not a viable solution for maintainable software, but it is at least a quick fix and an introduction for the next article, which will show how to use DynamicProxy2 for creating a ReducableList without ReSharper Jedi.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-8592927270395957004?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2008/01/emulating-mixins-with-c.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-1363369817816871008</guid><pubDate>Thu, 28 Feb 2008 07:51:00 +0000</pubDate><atom:updated>2008-02-28T14:49:54.207Z</atom:updated><title>The Component Burden Concept</title><description>&lt;span style="font-weight: bold;"&gt;Important Note:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Mort that I am, I have mistaken ReleasePolicy for Component Burden and blogged about the former...&lt;/span&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;As soon as I know what I am writing about, I will update that post.&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;There is a discussion on the Castle mailing list recently about the Component Burden problem. This is one of the oldest Castle &lt;a href="http://support.castleproject.org/browse/IOC-2" target="_blank"&gt;issues&lt;/a&gt; (and the oldest that is not resolved by now). Now, the issue got a little bit of momentum by Ayende's &lt;a href="http://groups.google.com/group/castle-project-devel/browse_thread/thread/e7c7d1904aac620c/61bb4d7635ba840f?hl=en#61bb4d7635ba840f" target="_blank"&gt;post&lt;/a&gt; and Hammett's &lt;a href="http://hammett.castleproject.org/?p=252" target="_blank"&gt;blog entry&lt;/a&gt; about it.&lt;/p&gt;  &lt;p&gt;In short, the problem is this: If there are non-singleton components created by the IoC-Container that implement &lt;span style="font-family:Courier New;"&gt;IDisposable&lt;/span&gt;, someone sometime needs to call &lt;span style="font-family:Courier New;"&gt;Dispose()&lt;/span&gt;. However, both someone and sometime pose a problem:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Someone could be the using code or the container. It has been proposed that a component should dispose it's dependencies itself, but if you think about it, this is problematic because the component cannot know it's dependencies' lifestyles. Just imagine a service configured to run as a singleton being disposed after the very first request it served, because an using component doesn't need it anymore. Additionally there is a simple principle about resource allocation: The code that allocates a resource is responsible for deallocation too. Period. Back when C/C++ was used, every developer adhered to that principle because there was no GC...&lt;/li&gt;    &lt;li&gt;Sometime is dependent on the components lifestyle. A singleton must not be disposed before the container shuts down itself while a transient component must be disposed as soon as possible to free allocated resources.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Now it is clear that the container is fully responsible for both construction and destruction of dependency objects. But how will it know, when the component is not used anymore?&lt;/p&gt;  &lt;p&gt;Well, at least one component must be resolved from the container. This component must be released after you are finished using it: &lt;/p&gt;  &lt;p&gt;&lt;span style="font-family:Courier New;"&gt;MyComponent c = container.Resolve&amp;lt;MyComponent&amp;gt;();    &lt;br /&gt;// Some work     &lt;br /&gt;container.Release(c);&lt;/span&gt;&lt;/p&gt;  &lt;p&gt;Do you need to do this even if you're component is not disposable? Yes, because it can have direct or indirect dependencies that are disposable. If you don't release it, the container cannot know that it is now safe to dispose these dependencies, when they are otherwise unused.&lt;/p&gt;  &lt;p&gt;Now there is one thing missing: The container must keep track of the dependencies. Assume that your component uses a custom per-web-request-logger that uses a &lt;span style="font-family:Courier New;"&gt;FileStream&lt;/span&gt;.&lt;/p&gt;  &lt;p&gt;Every time you resolve a component that uses the logger in one web request, the dependency will be fulfilled with the same logger. If you release a component, the container must remember that it used the logger, then check whether the logger is still used by other instances and if not, dispose the logger.&lt;/p&gt;  &lt;p&gt;This bookkeeping of dependencies and disposal is called the component burden in the &lt;a href="http://castleproject.org/" target="_blank"&gt;Castle project&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Although I don't know Microkernel's source code for this, I have an idea how to implement such a concept. But this is a topic for another post.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-1363369817816871008?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2008/02/component-burden-concept.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-576637242109682175</guid><pubDate>Fri, 09 Nov 2007 13:03:00 +0000</pubDate><atom:updated>2007-11-09T13:32:14.079Z</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">ActiveRecord</category><category domain="http://www.blogger.com/atom/ns#">Generics</category><category domain="http://www.blogger.com/atom/ns#">BugByDesign</category><title>WTF: Overloading and Generics</title><description>Recently I discovered some behaviour of .NET that I couldn't explain. I still wonder whether this is an error or "&lt;a href="http://ayende.com/Blog/archive/2007/09/18/Microsoft-Connect-Redefining-bugs-as-features-as-a-standard-operation.aspx"&gt;ByDesign&lt;/a&gt;". I found it when I tried to call ActiveRecordMediator's Exists method, which threw out an unexpected exception. Minutes later, I stared unbelievingly to the screen: A complete different method overload was called.&lt;br /&gt;&lt;br /&gt;I sat down and created a short example that doesn't use ActiveRecord, so it can be understood from any .NET-developer:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;interface&lt;/strong&gt;&lt;/span&gt; IString&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; Content&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; get&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/span&gt; DString&lt;span style="color:#000000;"&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/span&gt;IString&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;readonly&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; content&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; Content&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    get &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; content&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;DString&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; content&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#cc00cc;"&gt;this&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;content &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; content&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/span&gt; Class1&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;Foo&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;params&lt;/strong&gt;&lt;/span&gt; IString&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt; bars&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#006699;"&gt;&lt;strong&gt;foreach&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;IString s &lt;span style="color:#006699;"&gt;&lt;strong&gt;in&lt;/strong&gt;&lt;/span&gt; bars&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;      Console&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;WriteLine&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;s&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;Content&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;/span&gt; Foo&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/span&gt;T&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;T bar&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#006699;"&gt;&lt;strong&gt;throw&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;NotImplementedException&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;TestFixture&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/span&gt; Tester&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;Test&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;CallBar&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    Class1&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Foo&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;DString&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color:#ff00cc;"&gt;baz&lt;/span&gt;&lt;span style="color:#ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;  &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;So what would you expect when running the unit test?&lt;br /&gt;&lt;br /&gt;That's what I got:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;TestCase 'ClassLibrary2.Tester.CallBar' failed: System.NotImplementedException : The method or operation is not implemented.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;What happens here? If you add exactly one optional parameter, the compiler somehow thinks, you meant Foo&amp;lt;istring&amp;gt;() instead of Foo(). This happens only if the type parameter is an interface. I tried with string and that worked like expected, calling Foo().&lt;br /&gt;&lt;br /&gt;So, is this a bug or a documented behaviour. Do you know ressources that document it? I didn't find any...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-576637242109682175?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2007/11/wtf-overloading-and-generics.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-385473814989421290.post-3713984271470453318</guid><pubDate>Tue, 06 Nov 2007 11:26:00 +0000</pubDate><atom:updated>2007-11-06T13:16:31.946Z</atom:updated><category domain="http://www.blogger.com/atom/ns#">BookOfIdeas</category><category domain="http://www.blogger.com/atom/ns#">ActiveRecord</category><title>From The Book of Ideas: Searchable ActiveRecord-Entities</title><description>One of the feature requests that I hear most often from my users that they want a search function for the grids. I then show them a search form where they can specify field and values exactly. Most often, I then hear:&lt;br /&gt;"No, I meant a search field. I enter some text and the software shows all relevant entries."&lt;br /&gt;I usually answer by asking whether it should also save the climate and overcome poverty or if simple mindreading is sufficient.&lt;br /&gt;&lt;br /&gt;Ok, but last night I had an idea how searching with a single textfield interface can be accomplished without redeveloping that nasty thing for each view. I use the &lt;a href="http://www.castleproject.org/"&gt;Castle&lt;/a&gt; stack for .Net for my applications, which means that my entities are created with Castle ActiveRecord and displayed in MonoRail web applications.&lt;br /&gt;&lt;br /&gt;The idea works like this: There is only one who knows which properties must be included into the search and this the developer that created the entities. Therefore we need some mechanism to specify the searchable properties. My idea was adding a Searchable attribute to the ActiveRecord classes' properties that need to be included in such searches:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color:#000000;"&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;ActiveRecord&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;class&lt;/strong&gt;&lt;/span&gt; Entity &lt;span style="color:#000000;"&gt;&lt;strong&gt;:&lt;/strong&gt;&lt;/span&gt; ActiveRecordBase&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/span&gt;Entity&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt; id&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; name&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; entityDesc&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;private&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; internalName&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;PrimaryKey&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt; Id&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            get &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; id&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            set &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; id &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; value&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;Searchable&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;Property&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; Name&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            get &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; name&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            set &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; name &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; value&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Searchable&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;FriendlyName &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color:#ff00cc;"&gt;Description&lt;/span&gt;&lt;span style="color:#ff00cc;"&gt;"&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;Property&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; EntityDesc&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            get &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; entityDesc&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            set &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; entityDesc &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; value&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;Property&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; InternalName&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            get &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; internalName&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            set &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt; internalName &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; value&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt; &lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The Searchable takes an optional parameter, FriendlyName that allows to specify a localized or other, more user-friendly name. The attribute can be applied to both properties and relations. If applied to a relation attribute (BelongsTo, HasMany, HasAndBelongsToMany etc.) the properties tagged in that type will be added to the search as well.&lt;br /&gt;&lt;br /&gt;Then there is a SearchMediator, which will create a NHibernate Criteria Query that will check for every search term whether it can be parsed by the datatype of a tagged property and adds it to the query if this is the case.&lt;br /&gt;&lt;br /&gt;The code below sketches how the query could be created:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;span style="color:#000000;"&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;public&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;static&lt;/strong&gt;&lt;/span&gt; T&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#9966ff;"&gt;Search&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; text&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/span&gt; words &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; text&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Split&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    DetachedCriteria criteria &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; DetachedCriteria&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;For&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/span&gt;T&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#006699;"&gt;&lt;strong&gt;foreach&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#0099ff;"&gt;&lt;strong&gt;string&lt;/strong&gt;&lt;/span&gt; word &lt;span style="color:#006699;"&gt;&lt;strong&gt;in&lt;/strong&gt;&lt;/span&gt; words&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        Disjunction group &lt;span style="color:#000000;"&gt;&lt;strong&gt;=&lt;/strong&gt;&lt;/span&gt; Expression&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Disjunction&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#006699;"&gt;&lt;strong&gt;foreach&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;PropertyInfo info &lt;span style="color:#006699;"&gt;&lt;strong&gt;in&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#006699;"&gt;&lt;strong&gt;typeof&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;T&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;GetProperties&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;            &lt;span style="color:#006699;"&gt;&lt;strong&gt;if&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;info&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;GetCustomAttributes&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#006699;"&gt;&lt;strong&gt;typeof&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;SearchableAttribute&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;,&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#cc00cc;"&gt;true&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;Length &lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;0&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;                group&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Add&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;Expression&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Like&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;info&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;Name&lt;span style="color:#000000;"&gt;&lt;strong&gt;,&lt;/strong&gt;&lt;/span&gt; word&lt;span style="color:#000000;"&gt;&lt;strong&gt;,&lt;/strong&gt;&lt;/span&gt; MatchMode&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;Anywhere&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;        criteria&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;Add&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;group&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color:#006699;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt; ActiveRecordMediator&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;lt;&lt;/strong&gt;&lt;/span&gt;T&lt;span style="color:#000000;"&gt;&lt;strong&gt;&amp;gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#9966ff;"&gt;FindAll&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/span&gt;criteria&lt;span style="color:#000000;"&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;This can be extended by many means. Examples include:&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Allowing propertyName:searchTerm to narrow search to a specific property by its name or friendly name.&lt;/li&gt;&lt;li&gt;Allowing other operators than the colon like price&gt;10.00 or name!Foo&lt;/li&gt;&lt;li&gt;Add globbing to specify whether exact matches or all matches should be found&lt;/li&gt;&lt;li&gt;Specify the MatchMode behavior for string properties as part of SearchableAttribute&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Using that mechanism, I can add searching to almost any entity similar to validation. What do you think, would such a Search component benefit the Castle Framework?&lt;/p&gt;That is only some thoughts. I didn't start implementing anything yet. However, anyone interested in helping is welcome, of course.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/385473814989421290-3713984271470453318?l=mortslikeus.blogspot.com'/&gt;&lt;/div&gt;</description><link>http://mortslikeus.blogspot.com/2007/11/from-book-of-ideas-searchable.html</link><author>noreply@blogger.com (Markus Zywitza)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item></channel></rss>
