<?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" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6999142696093813888</atom:id><lastBuildDate>Mon, 09 Nov 2009 15:37:46 +0000</lastBuildDate><title>Germán Schuager</title><description>powered by myself</description><link>http://blog.schuager.com/</link><managingEditor>gschuager@gmail.com (Germán Schuager)</managingEditor><generator>Blogger</generator><openSearch:totalResults>17</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/gschuager" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-8691903303894023303</guid><pubDate>Fri, 19 Jun 2009 14:21:00 +0000</pubDate><atom:updated>2009-06-19T11:24:42.261-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>Case-sensitive queries in NHibernate using SQL Server</title><description>&lt;p&gt;Depending on the collation setting of your database (you probably already know this) the queries that you issue against it are treated as case-insensitive (CI collations) or as case-sensitive (CS collations).&lt;/p&gt; &lt;p&gt;If your database use a CI collation and you need to do some case-sensitive querying, the target SQL statement would be something like this:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;SELECT&lt;/span&gt; u.Name&lt;br /&gt;&lt;span class="kwrd"&gt;FROM&lt;/span&gt; Users u&lt;br /&gt;&lt;span class="kwrd"&gt;WHERE&lt;/span&gt; u.Name like &lt;span class="str"&gt;'SomeName'&lt;/span&gt; &lt;span class="kwrd"&gt;COLLATE&lt;/span&gt; Modern_Spanish_CS_AS&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Now, if you are using NHibernate you can do some things to make it help you issuing this kind of query.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The first one is to just use the Criteria API specifying the desired SQL expression:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;var user = session.CreateCriteria(&lt;span class="kwrd"&gt;typeof&lt;/span&gt; (User))&lt;br /&gt;    .Add(Expression.Sql(&lt;span class="str"&gt;"Username like ? collate Modern_Spanish_CS_AS"&lt;/span&gt;, username, NHibernateUtil.String))&lt;br /&gt;    .UniqueResult&amp;lt;User&amp;gt;();&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;This approach has the drawback that you tie your code to SQL Server specifically, and that will give you some headaches if you ever try to target another RDBMS.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The other (more elegant) option is to subclass the dialect that you are using (in this case MsSql2005Dialect) and register in it a custom function to perform case-sensitive comparisons.&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;br /&gt;&lt;pre class="csharpcode"&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CustomMsSqlDialect : MsSql2005Dialect&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; CustomMsSqlDialect()&lt;br /&gt;    {&lt;br /&gt;        RegisterFunction(&lt;span class="str"&gt;"sensitivelike"&lt;/span&gt;,&lt;br /&gt;            &lt;span class="kwrd"&gt;new&lt;/span&gt; SQLFunctionTemplate(NHibernateUtil.String,&lt;br /&gt;                &lt;span class="str"&gt;"?1 like ?2 collate Modern_Spanish_CS_AS"&lt;/span&gt;));&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Then you can use this new &lt;strong&gt;sensitivelike&lt;/strong&gt; function inside any HQL statement and NHibernate will generate the correct SQL for you.&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;var user = session.CreateQuery(&lt;span class="str"&gt;"from User u where sensitivelike(u.Username, :username)"&lt;/span&gt;)&lt;br /&gt;    .SetParameter(&lt;span class="str"&gt;"username"&lt;/span&gt;, username)&lt;br /&gt;    .UniqueResult&amp;lt;User&amp;gt;();&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;This way allows you to support a different RDBMS just by registering the corresponding function implementation in a new derived dialect and without modifying your code.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Thanks to &lt;a href="http://darioquintana.com.ar/blogging/"&gt;Dario&lt;/a&gt; for the tip.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-8691903303894023303?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/1spkHT8ELb6qSCA5Kf_Xmzvlt_E/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1spkHT8ELb6qSCA5Kf_Xmzvlt_E/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/1spkHT8ELb6qSCA5Kf_Xmzvlt_E/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/1spkHT8ELb6qSCA5Kf_Xmzvlt_E/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/b2JTvLCrmjc/case-sensitive-queries-in-nhibernate.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://blog.schuager.com/2009/06/case-sensitive-queries-in-nhibernate.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-3051486283773419407</guid><pubDate>Wed, 29 Apr 2009 14:58:00 +0000</pubDate><atom:updated>2009-10-13T21:18:03.999-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">deployment</category><category domain="http://www.blogger.com/atom/ns#">tools</category><title>Introducing NLaunch</title><description>&lt;p&gt;In the last month I've been working in several desktop applications and faced the situation of having to remotely update my clients machines repeatedly. It is not fun. The process that I was using for accomplish this task was the following:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;build the application locally (I'm not using CI)  &lt;/li&gt;&lt;li&gt;upload the result somewhere (ftp, Mediafire, etc.)  &lt;/li&gt;&lt;li&gt;notify the client that I will be logging in to do some work  &lt;/li&gt;&lt;li&gt;log in remotely using Remote Desktop or &lt;a href="http://logmein.com/"&gt;LogMeIn&lt;/a&gt; (great tool BTW)  &lt;/li&gt;&lt;li&gt;backup the current running version (just in case)  &lt;/li&gt;&lt;li&gt;download and install the new version&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;I can tell you that it takes a lot of time to do these simple steps (and it is boring as hell) so I’ve decided to do something about it… I automated it.&lt;/p&gt; &lt;p&gt;The first thing that I’ve done is to evaluate the existing tools for this job. Here are some of them:&lt;/p&gt; &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/wh45kb66.aspx"&gt;Clickonce&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I had spent almost 3 non-consecutive days trying to integrate Clickonce deployment into my nant build scripts and I had failed; besides this, along the way I’ve discovered some limitations of Clickonce that made me look in other direction:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Target directory can't be specified, it will always deploy to the Clickonce cache  &lt;/li&gt;&lt;li&gt;Not easy to deploy updates without running through the whole workflow (signing, uploading, etc). This means that you can't just recompile and xcopy the files to the target system (this is useful when you are at the client site).  &lt;/li&gt;&lt;li&gt;Complex. Application manifest, deployment manifest, signing, mage (some things can be only done through mageui), etc, etc.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms978545.aspx"&gt;Updater Application Block&lt;/a&gt;&lt;/p&gt; &lt;p&gt;It has lots of functionalities, extensibility points and configuration options but it seems too big for my needs. I was looking for something simpler that just works with almost no configuration.&lt;/p&gt; &lt;p&gt;&lt;a href="http://windowsclient.net/articles/appupdater.aspx"&gt;.NET Application Updater Component&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Not looked at this enough. It is bundled as a component that must be dragged into a form of your application (didn’t liked that)&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;h3&gt;NLaunch&lt;/h3&gt; &lt;h3&gt;&lt;/h3&gt; &lt;p&gt;Anyway… those were some of the existing options, but I was looking for something a lot simpler, something that I hadn’t found, so I decided to write it myself.&lt;/p&gt; &lt;p&gt;My two main requirements were:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Dead simple: it must consist of a single executable file and maybe some configuration.  &lt;/li&gt;&lt;li&gt;Unobtrusive: it must not require to add anything to the target application, it should work on its own.&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The simplicity requirement have taken me to make a lot of assumptions and to apply some conventions (a good thing as long as they are documented) that may not apply in everyone's scenario, for this reason, I’ve created several interfaces first and then developed its implementations to fulfill my requirements; this would allow anyone to easily replace some of my implementations with its owns in order to adjust NLaunch behavior. Right now it is not easily extensible without requiring source code modification, but I plan to fix that in the future.&lt;/p&gt; &lt;p&gt;Now with NLaunch my deployment process is reduced to this:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Build the application locally and upload the results to an FTP server (all with just 2 clicks)&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;I can’t say that this is actually fun, but at least is it neither boring nor tiresome… because I don’t have to DO anything! :)&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;Well, enough writing for now, you can check out the project’s home page for a little more specific information and you can also download the source code (it is really simple) or a ready-to-work binary.&lt;/p&gt; &lt;p&gt;&lt;a href="http://code.google.com/p/nlaunch/"&gt;http://code.google.com/p/nlaunch/&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-3051486283773419407?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VrxpoRMzlqq2v-hpQsFZAPDUBFo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VrxpoRMzlqq2v-hpQsFZAPDUBFo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VrxpoRMzlqq2v-hpQsFZAPDUBFo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VrxpoRMzlqq2v-hpQsFZAPDUBFo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/oV7-za9D8j0/introducing-nlaunch.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2009/04/introducing-nlaunch.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-8122489340688203142</guid><pubDate>Mon, 23 Mar 2009 18:12:00 +0000</pubDate><atom:updated>2009-09-04T17:24:34.323-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">castle</category><category domain="http://www.blogger.com/atom/ns#">ioc</category><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>Rich-client NHibernate session management</title><description>&lt;p&gt;I've been struggling with this subject for quite some time now, and I've just seem to found the &lt;a href="http://fgheysels.blogspot.com/2008/07/nhibernate-session-management.html"&gt;'sweet spot'&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Before we start, I need to describe some facts about the target application:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;rich-client: this approach will work the same for a Winform application as for a WPF application.  &lt;/li&gt;&lt;li&gt;multi-screen UI: it could be multi-form or tabbed or MDI... the important thing here is that you "will" have several screens opened at the same time.  &lt;/li&gt;&lt;li&gt;application uses an Inversion of Control container for wiring up dependencies among other useful things (I’m using Castle Windsor/Microkernel)&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Ok.&lt;/p&gt; &lt;p&gt;Before we continue, I recommend that you read the work of &lt;a href="http://stalamoni.blogspot.com/2007/12/nhibernate-and-winforms-article-1st.html"&gt;Sebastian Talamoni&lt;/a&gt;, that although not finished yet, covers a lot of ground.&lt;/p&gt; &lt;p&gt;Basically there are 4 options for NH session management on a rich-client environment:&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Session per application (per thread)  &lt;ul&gt; &lt;li&gt;the simplest approach  &lt;/li&gt;&lt;li&gt;only 1 session opened for the whole application lifecycle  &lt;/li&gt;&lt;li&gt;lots of problems: cache size, stale data due to other users, unrecoverable exception, etc.  &lt;/li&gt;&lt;li&gt;&lt;a href="http://fabiomaulo.blogspot.com/"&gt;Fabio Maulo&lt;/a&gt; calls this pattern TIME BOMB&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Session per screen  &lt;ul&gt; &lt;li&gt;1 session opened for each form/tab/view/whatever  &lt;/li&gt;&lt;li&gt;implies multiple concurrent sessions  &lt;/li&gt;&lt;li&gt;since a single screen could be left opened for quite some time, this approach is almost equally susceptible to stale data as the previous one&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Every data access - Fine-grained sessions  &lt;ul&gt; &lt;li&gt;every repository/DAO opens and closes a new session each time that a DB access is requested.  &lt;/li&gt;&lt;li&gt;loss of some useful NH features like lazy loading and 1st level cache… not worth it.&lt;/li&gt;&lt;/ul&gt; &lt;/li&gt;&lt;li&gt;Session per use-case  &lt;ul&gt; &lt;li&gt;also implies multiple concurrent sessions  &lt;/li&gt;&lt;li&gt;this seems to be the right choice, however it is the most difficult to implement&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ol&gt; &lt;h3&gt;Per-thread session management&lt;/h3&gt; &lt;p&gt;Options 1 and 3 are easily implemented using some kind of thread-static session manager. Examples of this are &lt;a href="http://ayende.com/wiki/Rhino%20Commons.ashx"&gt;Rhino.Commons&lt;/a&gt; and &lt;a href="http://castleproject.org/container/facilities/trunk/nhibernate/index.html"&gt;Castle NHibernate Integration Facility&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Each repository/DAO gets an instance of the a global "session manager" and uses it to get hold of the right session:&lt;/p&gt; &lt;ul&gt; &lt;li&gt;if 1 session per application is used, this "global" session is return by each call to SessionManager.OpenSession()  &lt;/li&gt;&lt;li&gt;if fine-grained sessions are used there are two possibilities:  &lt;ul&gt; &lt;li&gt;we are in the scope of another session... the outer session is returned  &lt;/li&gt;&lt;li&gt;we are not in the scope of another session... a new one is created&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;The easy thing about this is that each repository/DAO can access to the "session manager" through some static accessor, or even better, they can receive a singleton (as in singleton lifestyle) instance as a parameter of its constructor, which enable us to use an Inversion of Control container to wire up these dependencies and ease the unit testing of these components (no statics = easier testing).&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Presenter {&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; IBlogRepository blogRepository;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; Presenter(IView view, IBlogRepository blogRepository)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;this&lt;/span&gt;.blogRepository = blogRepository;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   ...&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BlogRepository : IBlogRepository&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; ISessionManager sessionManager;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; BlogRepository(ISessionManager sessionManager)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;this&lt;/span&gt;.sessionManager = sessionManager;&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; Blog Get(&lt;span class="kwrd"&gt;int&lt;/span&gt; id)&lt;br /&gt;   {&lt;br /&gt;       &lt;span class="kwrd"&gt;using&lt;/span&gt; (ISession session = sessionManager.OpenSession()) {&lt;br /&gt;           &lt;span class="kwrd"&gt;return&lt;/span&gt; session.Get&amp;lt;Blog&amp;gt;(id);&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;This pattern is suitable for a web application, where the context is usually given by the current request, but in a rich-client application it would imply that two opened screens (different contexts) would share the same session manager and therefore the same session when they intend to do some data access at the same time.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Contextual session management&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;Options 2 and 4 require another approach since they imply several simultaneous sessions through the lifecycle of the application. We can no longer have a static or singleton session storage from where to get the right session for each thread... we need some kind of "contextual session management" where each screen/use-case is treated as a different context and provided with sessions accordingly.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;This don't seem so hard, right? We can just do something like this for option 4:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Presenter {&lt;br /&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; ISessionManager sessionManager;&lt;br /&gt;   &lt;span class="kwrd"&gt;private&lt;/span&gt; IBlogRepository blogRepository;&lt;br /&gt;&lt;br /&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; Presenter(IView view)&lt;br /&gt;   {&lt;br /&gt;       sessionManager = SessionManagerFactory.CreateNewContext();&lt;br /&gt;       blogRepository = &lt;span class="kwrd"&gt;new&lt;/span&gt; BlogRepository(sessionManager);&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;The implementation of BlogRepository in this case is the same as before.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;At first sight this seems like a nice way to define contexts, but that is until you realize that you are making very hard to unit test this class (hardcoded dependencies) and you are neglecting the help of the IoC container to wire up things.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Imagine that now, instead of BlogRepository we need something like AggregatorService that depends on BlogRepository, UserRepository and CommentService, and CommentService depends on UserRepository and CommentRepository. In this case, you'd need to create the session manager and then to instantiate every one of this components passing the right arguments to them.... this seems too much work for me.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Managing the dependencies yourself is not fun, and your IoC container enjoys doing it for you anyway.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Maybe something can be done using some kind of service location and passing the current context down the chain, like this:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;container.Resolve&amp;lt;IBlogRepository&amp;gt;(With.Dependency&amp;lt;ISessionManager&amp;gt;(sessionManager)); &lt;span class="rem"&gt;// imaginary syntax&lt;/span&gt;&lt;br /&gt;container.Resolve&amp;lt;IAggregatorService&amp;gt;(With.Dependency&amp;lt;ISessionManager&amp;gt;(sessionManager)); &lt;span class="rem"&gt;// imaginary syntax&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;but there is an easier way, let me introduce you to the…&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;&lt;/h3&gt;&lt;br /&gt;&lt;h3&gt;Contextual Session Manager&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;This is an extension to the NHibernate Integration Facility of the Castle project that allows me to control the scope of my session and do not impose a global per-thread scope.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The solution is integrated by the following components:&lt;/p&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;ContextualNHibernateFacility: inherits from NHibernateFacility and all it does is to replace some of the standard components registered by the original facility specifying the right Lifestyle for the new ones.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;ContextualPerThreadStore: inherits from AbstractDictStackSessionStore and provides a contextual session storage where there will be a different session for each instance of this class for each thread (tricky). The original CallContextSessionStore uses the call-context to store sessions, that means that two different instances of CallContextSessionStore will return the same session for the same call-context... this behavior is the one that I didn't like.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;ContextualSessionManager: just inherits from DefaultSessionManager to specify a custom lifestyle (see below) to be use by Microkernel to manage this component.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;ResolutionContextLifestyleManager: inherits from AbstractLifestylemanager. This is the “thing” that delimits our contexts. For more information go &lt;a href="http://blog.schuager.com/2008/11/custom-windsor-lifestyle.html"&gt;here&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;p&gt;This set of components allow me to use the container to resolve presenters, ViewModels, forms, or anything else that I’d like to use to delimit a context, like this:&lt;/p&gt;&lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;var presenter = container.Resolve&amp;lt;IBlogPresenter&amp;gt;();&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;then, every required dependency (including the NH session manager) is resolved within the same context, all of this without even making any component aware of the existence of such grouping.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;It is all handled by the container, which is exactly what I was looking for.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;&lt;/h3&gt;&lt;br /&gt;&lt;h3&gt;Sample Project&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;I’ve put together a small spike to illustrate these concepts. Maybe a more complex application would be more suitable to present some things, but for now this is all I’ve done.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;The sample solution uses NHibernate, Windsor, NH facility, ATM (automatic transaction management) and a simple WPF UI using Caliburn’s &lt;a href="http://caliburn.codeplex.com/Wiki/View.aspx?title=Action%20Basics&amp;amp;referringTitle=Table%20Of%20Contents"&gt;Action&lt;/a&gt; support.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;If you’re using SQL Server Express you just need to create an empty DB called “test”, otherwise just modify the settings in App.config to suit your needs. The tables and initial data are created by the application at startup.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can download the sample &lt;a href="http://schuager.com/downloads/CRMSample.zip"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;h3&gt;&lt;/h3&gt;&lt;br /&gt;&lt;h3&gt;Improvements&lt;/h3&gt;&lt;br /&gt;&lt;p&gt;One obvious thing that is left out here is the capability to resolve more than one root component for a given context (two repositories in two different presenters that get the same ISessionManager) but that is something that could be added with no problem.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;There are probably a lot of scenarios where the approach described here does not apply… I’ve just implemented a solution to my problem that works well for me and I think that maybe someone else can benefit from this.&lt;/p&gt;&lt;br /&gt;&lt;p&gt; &lt;/p&gt;&lt;br /&gt;&lt;p&gt;I would like to know what do you think about this approach.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-8122489340688203142?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Sgj_IdsFbEFI4Dasa4UgKr2_1T4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Sgj_IdsFbEFI4Dasa4UgKr2_1T4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Sgj_IdsFbEFI4Dasa4UgKr2_1T4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Sgj_IdsFbEFI4Dasa4UgKr2_1T4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/ivwSCxr8yDM/rich-client-nhibernate-session.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">10</thr:total><feedburner:origLink>http://blog.schuager.com/2009/03/rich-client-nhibernate-session.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4270262199159504444</guid><pubDate>Mon, 09 Mar 2009 16:12:00 +0000</pubDate><atom:updated>2009-03-09T14:12:48.109-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">bugs</category><category domain="http://www.blogger.com/atom/ns#">reporting</category><title>Reporting Services + LocalReport hang</title><description>&lt;p&gt;Just for future reference...  &lt;p&gt;Winforms application + Reporting Services + LocalReport + bad luck = Application hangs without throwing any exception :(  &lt;p&gt;I've just spend a lot of time trying to figure out what the hell was causing this issue.&lt;br&gt;It happened when I did this: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;localReport.LoadReportDefinition(stream)&lt;br /&gt;localReport.GetDataSourceNames()  // here it hangs 2 of 3 times &lt;/pre&gt;&lt;br /&gt;&lt;p&gt;I've managed to get the data source names from the stream reading some XML but then it hung at another point where the report was referenced again... agrhh! &lt;br /&gt;&lt;p&gt;Researching a bit more and using Reflector a lot I've found out that the issue might have something to do with the report definition being compiled in another application domain the first time that it is required. &lt;br /&gt;&lt;p&gt;All in all, I've solved this issue by changing the threading model of my application from STA (single-threaded apartment) to MTA (multithreaded apartment) by changing this: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;[STAThread]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()&lt;br /&gt;{&lt;br /&gt;   ...&lt;br /&gt;} &lt;/pre&gt;&lt;br /&gt;&lt;p&gt;to this: &lt;!-- code formatted by http://manoli.net/csharpformat/ --&gt;&lt;pre class="csharpcode"&gt;[MTAThread]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()&lt;br /&gt;{&lt;br /&gt;   ...&lt;br /&gt;} &lt;/pre&gt;&lt;br /&gt;&lt;p&gt;Perhaps this can save somebody some time.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4270262199159504444?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/NI33Tz1tADCabqlbk30WTSIomzI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NI33Tz1tADCabqlbk30WTSIomzI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/NI33Tz1tADCabqlbk30WTSIomzI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/NI33Tz1tADCabqlbk30WTSIomzI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/NRyi43ZoiN8/reporting-services-localreport-hang.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2009/03/reporting-services-localreport-hang.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4203045489572345730</guid><pubDate>Thu, 12 Feb 2009 16:56:00 +0000</pubDate><atom:updated>2009-02-13T11:26:42.632-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>Contextual data using NHibernate filters</title><description>I'm in the middle of the development process of an application using NH for data access, and I'm faced with a requirement that could be stated as follows:&lt;br /&gt;&lt;blockquote&gt;The application needs to provide support for different Contexts of execution, and certain entities must be context-aware, which means that at a given time, the application only sees instances of these entities that correspond to the current context of execution.&lt;/blockquote&gt;Now, just remember that I have several entities defined that are used throught the entire application layer stack, so I wanted to solve this issue modifying as little as possible.&lt;br /&gt;&lt;br /&gt;I'm very proud with the solution that I came up with, and also very amazed by the power of NHibernate.&lt;br /&gt;&lt;br /&gt;To simplify a little lets assume that I have a static class that defines the current context of execution:&lt;pre&gt;&lt;blockquote&gt;&lt;pre&gt;public enum ContexType&lt;br /&gt;{&lt;br /&gt;  ContextA,&lt;br /&gt;  ContextB,&lt;br /&gt;}&lt;/pre&gt;public static class Context&lt;br /&gt;{&lt;br /&gt;  public static ContextType Current { get; set; }&lt;br /&gt;}&lt;/blockquote&gt;&lt;/pre&gt;Then, I create an interface that will be implemented by all the entities that need to be contextualized:&lt;pre&gt;&lt;blockquote&gt;public interface IContextAware&lt;br /&gt;{&lt;br /&gt;  ContextType Context { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/blockquote&gt;&lt;/pre&gt;Given a Cat class that needs to be contextualized, then I add the property to the class and to the mapping:&lt;pre&gt;&lt;blockquote&gt;public class Cat : Entity, IContextAware&lt;br /&gt;{&lt;br /&gt;  ...&lt;br /&gt;  ContextType Context { get; set; }&lt;br /&gt;  ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&amp;lt;class name="Cat"&amp;gt;&lt;br /&gt;  ...&lt;br /&gt;  &amp;lt;property name="Context"&amp;gt;&lt;br /&gt;  ...&lt;br /&gt;&amp;lt;/class&amp;gt;&lt;/blockquote&gt;&lt;/pre&gt;The idea now, is to use the dynamic filtering capabilities of NHibernate to only retrieve the Cats instances corresponding with the current context every time that a query against Cat is issued.&lt;br /&gt;Typically this means that I need to add a filter definition to the mappings and the specify the condition for that filter in every class mapping that need to be aware of this behavior.&lt;br /&gt;But there is an easier way to do this automatically:&lt;pre&gt;&lt;blockquote&gt;var filterParametersType = new Dictionary&lt;string,&gt;&amp;lt;string, IType&amp;gt;(1);&lt;br /&gt;filterParametersType.Add("current", NHibernateUtil.Enum(typeof(ContextType)));&lt;br /&gt;cfg.AddFilterDefinition(new FilterDefinition("contextFilter", ":current = Context", filterParametersType));&lt;br /&gt;&lt;br /&gt;foreach (var mapping in cfg.ClassMappings)&lt;br /&gt;{&lt;br /&gt;  if (typeof(IContextAware).IsAssignableFrom(mapping.MappedClass))&lt;br /&gt;  {&lt;br /&gt;     mapping.AddFilter("contextFilter", ":current = Context");&lt;br /&gt;  }&lt;br /&gt;}&lt;/string,&gt;&lt;string,&gt;&lt;/string,&gt;&lt;/blockquote&gt;&lt;/pre&gt;Just do this (cfg is the NH Configuration object) before building the session factory and it creates the correct filter definition and adds the condition to every entity mapped that implements IContextAware.&lt;br /&gt;&lt;br /&gt;At this point we just have our filter defined; now we need to enable it in order to actually filter something. It would be very handy if we can enable filtering at session factory scope, but since the session factory is immutable we need to enable it for each session that we will be using.&lt;br /&gt;&lt;br /&gt;Wait.... maybe something else can do this work for us...&lt;br /&gt;&lt;br /&gt;The following interceptor actually takes care of 2 things:&lt;br /&gt;1. enables the context filter as soon as it is attached to the session, and&lt;br /&gt;2. assigns the correct value to the Context property of entities implementing IContextAware when they are persisted.&lt;br /&gt;&lt;pre&gt;&lt;blockquote&gt;public class ContextInterceptor : EmptyInterceptor&lt;br /&gt;{&lt;br /&gt;  public override void SetSession(ISession session)&lt;br /&gt;  {&lt;br /&gt;     session.EnableFilter("contextFilter").SetParameter("current", Context.Current);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)&lt;br /&gt;  {&lt;br /&gt;     var contextAware = entity as IContextAware;&lt;br /&gt;     if (contextAware != null)&lt;br /&gt;     {&lt;br /&gt;        int index = Array.Find(propertyNames, 0, x =&gt; x.Equals("Context"));&lt;br /&gt;        state[index] = contextAware.Context = Context.Current;&lt;br /&gt;        return true;&lt;br /&gt;     }&lt;br /&gt;     return false;&lt;br /&gt;  }&lt;br /&gt;}&lt;/blockquote&gt;&lt;/pre&gt;Every session in the application needs to be created specifying this interceptor, but this should be an easy change (that depends on your architecture) if you are doing things right.&lt;br /&gt;&lt;br /&gt;And thats all, the rest of the application is untouched and the requirement is fulfilled in a very elegant way.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4203045489572345730?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9B9leV_6wo734WCwKJE3XbARr98/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9B9leV_6wo734WCwKJE3XbARr98/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9B9leV_6wo734WCwKJE3XbARr98/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9B9leV_6wo734WCwKJE3XbARr98/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/lEnGCkvYf1A/contextual-data-using-nhibernate.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://blog.schuager.com/2009/02/contextual-data-using-nhibernate.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-6055836783351168319</guid><pubDate>Wed, 07 Jan 2009 11:11:00 +0000</pubDate><atom:updated>2009-10-09T12:07:54.155-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tools</category><title>Line count in Visual Studio</title><description>This is a nice trick that should be spreaded.&lt;br /&gt;&lt;br /&gt;Select &lt;span style="font-weight: bold;"&gt;Edit&lt;/span&gt; -&gt; &lt;span style="font-weight: bold;"&gt;Find &amp;amp; Replace&lt;/span&gt; -&gt; &lt;span style="font-weight: bold;"&gt;Find in files...&lt;/span&gt; or just press CTRL+SHIFT+F&lt;br /&gt;&lt;br /&gt;Check &lt;span style="font-weight: bold;"&gt;Use&lt;/span&gt; and select &lt;span style="font-weight: bold;"&gt;Regular expressions&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Type the following as the text to find:&lt;div&gt;&lt;br /&gt;&lt;div&gt;C#&lt;br /&gt;&lt;blockquote&gt;&lt;span style="font-family:courier new;"&gt;^~(:Wh@//.+)~(:Wh@\{:Wh@)~(:Wh@\}:Wh@)~(:Wh@/#).+&lt;/span&gt;&lt;/blockquote&gt;VB.NET (thanks Steve for the info!)&lt;blockquote&gt;&lt;span style="font-family:courier new;"&gt;^~(:Wh@'.+)~(:Wh@/#).+&lt;/span&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;br /&gt;Select where you want to do the search/count: file, project or solution.&lt;br /&gt;&lt;br /&gt;If you select Current project or Entire solution, you also need to specify the file types that will be included in the search.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_IqjkFvf2cS4/SWSWkn8PsPI/AAAAAAAAAE8/dOhwUUK86eA/s1600-h/findinfiles.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 309px; height: 400px;" src="http://4.bp.blogspot.com/_IqjkFvf2cS4/SWSWkn8PsPI/AAAAAAAAAE8/dOhwUUK86eA/s400/findinfiles.png" alt="" id="BLOGGER_PHOTO_ID_5288517418380931314" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;Proceed with the "search" and at the bottom of the Find results window you will see the total line count.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_IqjkFvf2cS4/SWSWkwXc2VI/AAAAAAAAAFE/SLzlsoEHGFU/s1600-h/linecount.png"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 400px; height: 50px;" src="http://2.bp.blogspot.com/_IqjkFvf2cS4/SWSWkwXc2VI/AAAAAAAAAFE/SLzlsoEHGFU/s400/linecount.png" alt="" id="BLOGGER_PHOTO_ID_5288517420642523474" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;The regular expression that is used match every line that are not a comment (//), a compiler directive (starts with #), a single opening or closing brace, or blank lines.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;Note:&lt;/span&gt;&lt;br /&gt;&lt;span style="font-style: italic;"&gt;This post is based on &lt;a href="http://redgloo.sse.reading.ac.uk/dotnet/weblog/103.html"&gt;this one&lt;/a&gt; from Philip Stears (the regex in the original post does not work correctly).&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-6055836783351168319?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/rQcWiAlc-MBdmYfxLrerMZaQh-c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rQcWiAlc-MBdmYfxLrerMZaQh-c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/rQcWiAlc-MBdmYfxLrerMZaQh-c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/rQcWiAlc-MBdmYfxLrerMZaQh-c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/C9U2-whCvjA/line-count-in-visual-studio.html</link><author>gschuager@gmail.com (Germán Schuager)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/_IqjkFvf2cS4/SWSWkn8PsPI/AAAAAAAAAE8/dOhwUUK86eA/s72-c/findinfiles.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">9</thr:total><feedburner:origLink>http://blog.schuager.com/2009/01/line-count-in-visual-studio.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4372146891112085602</guid><pubDate>Tue, 23 Dec 2008 21:11:00 +0000</pubDate><atom:updated>2008-12-23T19:13:14.595-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>Polymorphic query using interfaces</title><description>&lt;p&gt;Given an object model defined by the following classes/interfaces:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Animal&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IHasFourLegs&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;public interface &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Dog &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;Animal&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IHasFourLegs&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Bird &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;Animal&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Unicorn &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;Animal&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;IHasFourLegs&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;p&gt;Suppose that you want to persist this class hierarchy to a database and be able to easily retrieve all the entities that implement one of both interfaces... then NHibernate is your friend (as always)&lt;/p&gt;I've chosen to map this using the &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#inheritance-tablepersubclass"&gt;table-per-subclass strategy&lt;/a&gt;, but probably the other &lt;a href="http://www.nhforge.org/doc/nh/en/index.html#inheritance-strategies"&gt;inheritance mapping strategies&lt;/a&gt; would work as well:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;hibernate-mapping &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:nhibernate-mapping-2.2&lt;/span&gt;"&lt;br /&gt;                 &lt;span style="color:red;"&gt;assembly&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Assembly&lt;/span&gt;"&lt;br /&gt;                 &lt;span style="color:red;"&gt;namespace&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Namespace&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;class &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Animal&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;  &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Dog&lt;/span&gt;" &lt;span style="color:red;"&gt;extends&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Animal&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;key&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;  &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Bird&lt;/span&gt;" &lt;span style="color:red;"&gt;extends&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Animal&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;key&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;  &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Unicorn&lt;/span&gt;" &lt;span style="color:red;"&gt;extends&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Animal&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;key&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;  &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;...&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;joined-subclass&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;hibernate-mapping&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;Now that we have our classes and their mappings we are going to query our domain to get all the entities that implement ICanFly.&lt;br /&gt;&lt;p&gt;Basically you have 2 options right now: Criteria API and HQL.&lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;h3&gt;&lt;/h3&gt;&lt;br /&gt;&lt;h3&gt;Using Criteria API&lt;/h3&gt;This is very simple, all you need to do is this:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;flyers = session.CreateCriteria(&lt;span style="color:blue;"&gt;typeof &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;/span&gt;)).List&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt; &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Using HQL&lt;/h3&gt;This one gets a little trickier (at least it got for me) since you need to explicitly import the interface type inside the mapping to get NH to know about it; this is simply done by adding the following line to your mapping:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;import &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Namespace.ICanFly&lt;/span&gt;" &lt;span style="color:red;"&gt;rename&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;ICanFly&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;After this little addition we can issue a successful HQL query:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;flyers = session.CreateQuery(&lt;span style="color: rgb(163, 21, 21);"&gt;"from ICanFly"&lt;/span&gt;).List&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ICanFly&lt;/span&gt;&amp;gt;();&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;&lt;p&gt; &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Remarks&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;The &lt;span style="font-family:Courier New;"&gt;import&lt;/span&gt; mapping needed when using HQL seems to be used by NH to map the string used in the query (&lt;span style="font-family:Courier New;"&gt;ICanFly&lt;/span&gt;) to an actual type... if you'd have put &lt;span style="font-family:Courier New;"&gt;rename="Flyers"&lt;/span&gt; in the import element then the HQL would need to be &lt;span style="font-family:Courier New;"&gt;"from Flyers"&lt;/span&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;At first, I thought that specifying the full name of the type directly in the HQL string would have been enough, but it is not.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;Take special care when trying to retrieve an ordered result set because since NH should issue several SELECT statements to get all the entities from the different tables (instead of using an UNION) the order of the results will probably be wrong. &lt;/li&gt;&lt;/ul&gt;&lt;p&gt; &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;h2&gt;&lt;/h2&gt;This polymorphic behavior of NHibernate is just awesome because it allows you to be creative with your domain and frees you of the constraints imposed by the underlying relational model.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4372146891112085602?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/JbYV9Laxf7xun5iWqNQdRdiPa0c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JbYV9Laxf7xun5iWqNQdRdiPa0c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/JbYV9Laxf7xun5iWqNQdRdiPa0c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/JbYV9Laxf7xun5iWqNQdRdiPa0c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/Z7HqhWHS698/polymorphic-query-using-interfaces.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://blog.schuager.com/2008/12/polymorphic-query-using-interfaces.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-3506108272264221548</guid><pubDate>Mon, 22 Dec 2008 11:48:00 +0000</pubDate><atom:updated>2008-12-27T11:55:22.336-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>NHibernate Read-only property access</title><description>&lt;p&gt;UPDATE: If you use the trunk you can now just do this (since rev. 3965):&lt;/p&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;blockquote&gt;&lt;/blockquote&gt;&lt;p&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Total&lt;/span&gt;" &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Decimal&lt;/span&gt;" &lt;span style="color:red;"&gt;access&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;readonly&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Keep reading if you are using an older version of NHibernate.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;hr /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;Recently I've asked in the &lt;a href="http://groups.google.com/group/nhusers"&gt;NHibernate mailing list&lt;/a&gt; about how to map a property that is calculated in the domain but needs to be persisted in order to query data based on this property's value.&lt;/p&gt;  &lt;p&gt;The thread is &lt;a href="http://groups.google.com/group/nhusers/browse_thread/thread/7251b55b123b1e1b?tvc=2&amp;amp;q=+Calculated+property+mapping+"&gt;here&lt;/a&gt; and the sample I've used is the following:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public decimal &lt;/span&gt;Total&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;   get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;calculateTotal(); }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;private decimal &lt;/span&gt;calculateTotal()&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;   return &lt;/span&gt;children.Sum(x =&amp;gt; x.Price);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;Basically, you have 3 options to persist this property.&lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;br /&gt;&lt;h3&gt;1. Add an empty set method&lt;br /&gt;&lt;/h3&gt;&lt;p&gt;This is the simplest but the ugliest of the three, you don't need to do anything else and it works just fine:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public decimal &lt;/span&gt;Total&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;   get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;calculateTotal(); }&lt;br /&gt;&lt;span style="color:blue;"&gt;   set &lt;/span&gt;{ }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;h3&gt;2. Use a nullable field to hold the value&lt;/h3&gt;&lt;p&gt;Fabio suggested this approach:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;private decimal &lt;/span&gt;total?;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public decimal &lt;/span&gt;Total&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;   get&lt;br /&gt;&lt;/span&gt;   {&lt;br /&gt;&lt;span style="color:blue;"&gt;      if&lt;/span&gt;(!total.HasValue)&lt;br /&gt;    total = calculateTotal();&lt;br /&gt;&lt;span style="color:blue;"&gt;      return &lt;/span&gt;total.Value;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;Item AddItem()&lt;br /&gt;{&lt;br /&gt;invalidateTotal();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;private void &lt;/span&gt;invalidateTotal()&lt;br /&gt;{&lt;br /&gt;total = &lt;span style="color:blue;"&gt;null&lt;/span&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;In this case, the Total property is only calculated the first time that is required and its value cached, also you need to add calls to invalidateTotal() in every operation that would alter the result of the calculation.&lt;/p&gt;Here you should use access="field.camelcase" when mapping the property.&lt;br /&gt;&lt;p&gt;As long as you don't call invalidateTotal() again, the calculated value is persisted and restored from the DB without processing the sum again.&lt;/p&gt;&lt;p&gt;This method is the most efficient, but requires you to put invalidateTotal() calls (and to don't forget about that) in some places... if you have a few calculated properties that each one depends on a few others it can get quite messy.&lt;br /&gt;&lt;/p&gt;&lt;h3&gt;3. Read-only property accessor&lt;/h3&gt;&lt;p&gt;Because of this issue, I've dove into NHibernate source code and tried to create a new property accessor that would allow me to map the domain that I really want without any hassle.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;I was surprised at how easy I've found what I wanted to do inside that jungle.&lt;/p&gt;&lt;p&gt;I've created a JIRA &lt;a href="http://jira.nhibernate.org/browse/NH-1621"&gt;issue&lt;/a&gt; with the patch &lt;strike&gt;but so far it is not applied to the trunk.&lt;/strike&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;In the mean time you can use the readonly property accessor from &lt;a href="http://schuager.com/downloads/ReadonlyPropertyAccessor.zip"&gt;here&lt;/a&gt; mapping it using the custom accessor functionality of NHibernate like this:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;property &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Total&lt;/span&gt;" &lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Decimal&lt;/span&gt;" &lt;span style="color:red;"&gt;access&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;Namespace.ReadonlyAccessor, Assembly&lt;/span&gt;"&lt;span style="color:blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/blockquote&gt;(remember to replace Namespace and Assembly with the real namespace and assembly name where you put ReadonlyPropertyAccessor.cs)&lt;br /&gt;&lt;br /&gt;This ReadonlyPropertyAccessor is working against NHibernate 2.0.0.GA and it needs a minor spell correction to make it work against the trunk... you will notice if you try.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So, here we are... those were the 3 options... choose whichever you like most.&lt;br /&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-3506108272264221548?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/X2vSPTgU0BE0IWqp_P5s5R6bSdo/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/X2vSPTgU0BE0IWqp_P5s5R6bSdo/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/X2vSPTgU0BE0IWqp_P5s5R6bSdo/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/X2vSPTgU0BE0IWqp_P5s5R6bSdo/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/CWcWH6bIMu0/nhibernate-read-only-property-access.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2008/12/nhibernate-read-only-property-access.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-959049463586875732</guid><pubDate>Wed, 26 Nov 2008 13:36:00 +0000</pubDate><atom:updated>2008-11-26T11:38:43.706-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">netcf</category><category domain="http://www.blogger.com/atom/ns#">ioc</category><title>CompactContainer new features</title><description>&lt;p&gt;Last week I've found this &lt;a href="http://www.agilification.com/post/Dependency-Injection-for-the-Compact-Framework.aspx"&gt;post&lt;/a&gt;, in which &lt;a href="http://www.agilification.com/"&gt;Jeff Doolittle&lt;/a&gt; made an analysis of some alternatives about using dependency injection with the .NET Compact Framework.&lt;/p&gt;  &lt;p&gt;As I've faced the same decision some time ago and ended rolling out my own solution, I pointed him to &lt;a href="http://code.google.com/p/compactcontainer/"&gt;CompactContainer&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Apparently he likes the project but needs some features not implemented at the time. He sent me some patches and in the end we've added the following features to CompactContainer.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h4&gt;Attribute to mark the injectable constructor&lt;/h4&gt;  &lt;p&gt;By default, the container uses the constructor with most satisfiable (I thinks that's not a word...) dependencies to instantiate each component. This is the default behavior of Windsor/Microkernel.&lt;/p&gt;  &lt;p&gt;Now, we have created an extension point where the default IHandler (responsible of creation and initialization of each component) can be specified and we've created an AttributedHandler that allows to select which constructor to use by means of an attribute.&lt;/p&gt;  &lt;p&gt;The attribute type could be specified by the user when creating the AttributedHandler. &lt;/p&gt;  &lt;p&gt;Take a look at the following example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family:Courier New;"&gt;var container = new CompactContainer();      &lt;br /&gt;container.DefaultHandler = new AttributedHandler();       &lt;br /&gt;container.AddComponent(typeof(IDependencyA), typeof(DependencyA));       &lt;br /&gt;container.AddComponent(typeof(IDependencyB), typeof(DependencyB));       &lt;br /&gt;container.AddComponent(typeof(MyComponent));       &lt;br /&gt;container.Resolve&amp;lt;MyComponent&amp;gt;();       &lt;br /&gt;... &lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family:Courier New;"&gt;public class MyComponent      &lt;br /&gt;{       &lt;br /&gt;     [Inject]       &lt;br /&gt;     public MyComponent(IDependencyA a) { ... } &lt;/span&gt;&lt;/p&gt;    &lt;p&gt;&lt;span style="font-family:Courier New;"&gt;     public MyComponent(IDependencyA a, IDependencyB b) { ... }      &lt;br /&gt;}&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The first constructor will be used to create the MyComponent service even when all the dependencies for the second constructor are available.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;h4&gt;Autoregistration of components&lt;/h4&gt;  &lt;p&gt;Jeff also added a feature where the container automatically registers concrete types when asked for them and they are not already registered.  &lt;br /&gt;This has required some improvements in the code base:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Specification of the default lifestyle (this can be improved a lot introducing something like ILifestyleManager to decouple the lifestyle's logic) &lt;/li&gt;    &lt;li&gt;Generation of a default component key (based on the type) and overloads to the registration methods that don't require the component key and use the default one instead. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Example:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-family:Courier New;"&gt;container = new CompactContainer();      &lt;br /&gt;container.AddComponent(typeof(IDependencyA), typeof(DependencyA));       &lt;br /&gt;var comp = container.Resolve&amp;lt;MyComponent&amp;gt;();&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;comp will be a singleton of type MyComponent and it is resolved ok even when the container knows nothing about it before the call to Resolve&amp;lt;T&amp;gt;.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;To sum up &lt;a href="http://www.agilification.com/post/Dependency-Injection-for-the-Compact-Framework-Part-2.aspx"&gt;here&lt;/a&gt; you can read Jeff's thoughts about CompactContainer. Thanks Jeff!&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;Open source rulez!&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-959049463586875732?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jAsLAXAb9hWkeTmArLU6ftMirvk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jAsLAXAb9hWkeTmArLU6ftMirvk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jAsLAXAb9hWkeTmArLU6ftMirvk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jAsLAXAb9hWkeTmArLU6ftMirvk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/mNObM0Xhcek/compactcontainer-new-features.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total><feedburner:origLink>http://blog.schuager.com/2008/11/compactcontainer-new-features.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4755437052586356441</guid><pubDate>Thu, 06 Nov 2008 04:04:00 +0000</pubDate><atom:updated>2008-11-23T14:10:54.327-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">castle</category><category domain="http://www.blogger.com/atom/ns#">ioc</category><title>Custom Windsor lifestyle: ResolutionContextLifestyleManager</title><description>&lt;p&gt;For reasons that will become clear in a future post (or later in this one), I needed to implement a custom lifestyle for Microkernel/Windsor. The required behavior was to propagate an instance of a component through the whole resolution chain but no further.&lt;/p&gt;  &lt;p&gt;Let me explain it a little better.&lt;/p&gt;  &lt;p&gt;The default lifestyle used by the container when none is specified is Singleton. This means that the first time that a service is resolved, the instance created is cached, and every other time after that, this same instance is returned.&lt;/p&gt;  &lt;p&gt;You can also specify a Transient lifestyle, which indicates that every time that a service is being resolved a new instance is created.&lt;/p&gt;  &lt;p&gt;What I was looking for here was some way to specify context boundaries in order that when the container is asked to provide a dependency for a given service within these boundaries, it will provides always the same instance, but when you are outside that context (or inside a different one) it will resolve a different (new) instance.&lt;br /&gt;This behavior is achieved by the ResolutionContextLifestyleManager, that although it is not perfect at all (it is not thread-safe for example), fulfills my current requirements very well.&lt;/p&gt;  &lt;p&gt;The implementation is based in remembering the type of the handler that initiates the resolution chain along with the instance just created, then every time that an instance is asked for within the same resolution chain, the cached instance is returned. It also hooks to the Kernel.ComponentCreated event in order to determine when the cached instances must be released (the resolution starter component has been created). It has grow a little messy to enable support for generic services.&lt;/p&gt;  &lt;p&gt;I've put together the custom lifestyle with some tests in a solution that you can download from &lt;a href="http://www.mediafire.com/?jh3yoimcume"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The motivation behind this idea was to enable the usage of simultaneous forms in a rich-client environment where each one maintains its own context (NHibernate session manager in my current scenario).&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4755437052586356441?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/9vcm6rcntBpSU6Z9Gabw8hSKUuk/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9vcm6rcntBpSU6Z9Gabw8hSKUuk/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/9vcm6rcntBpSU6Z9Gabw8hSKUuk/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/9vcm6rcntBpSU6Z9Gabw8hSKUuk/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/JUVygWnvvHQ/custom-windsor-lifestyle.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2008/11/custom-windsor-lifestyle.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-7405498766024515678</guid><pubDate>Wed, 03 Sep 2008 23:08:00 +0000</pubDate><atom:updated>2008-11-14T01:02:06.785-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">jquery</category><title>Mutually exclusive checkboxes with jQuery</title><description>&lt;p&gt;I was needing a way for the user to select none or just one item in a set... here is how I've managed to achieve this functionality using jQuery:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;html&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript" &lt;/span&gt;&lt;span style="color:red;"&gt;src&lt;/span&gt;&lt;span style="color:blue;"&gt;="http://code.jquery.com/jquery-latest.min.js"&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="text/javascript" &lt;/span&gt;&lt;span style="color:red;"&gt;language&lt;/span&gt;&lt;span style="color:blue;"&gt;="javascript"&amp;gt;&lt;br /&gt;          &lt;/span&gt;$(document).ready(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;br /&gt;              $(&lt;span style="color: rgb(163, 21, 21);"&gt;'.mutuallyexclusive'&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function &lt;/span&gt;() {&lt;br /&gt;                  checkedState = $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).attr(&lt;span style="color: rgb(163, 21, 21);"&gt;'checked'&lt;/span&gt;);&lt;br /&gt;                  $(&lt;span style="color: rgb(163, 21, 21);"&gt;'.mutuallyexclusive:checked'&lt;/span&gt;).each(&lt;span style="color:blue;"&gt;function &lt;/span&gt;() {&lt;br /&gt;                      $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).attr(&lt;span style="color: rgb(163, 21, 21);"&gt;'checked'&lt;/span&gt;, &lt;span style="color:blue;"&gt;false&lt;/span&gt;);&lt;br /&gt;                  });&lt;br /&gt;                  $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).attr(&lt;span style="color: rgb(163, 21, 21);"&gt;'checked'&lt;/span&gt;, checkedState);&lt;br /&gt;              });&lt;br /&gt;          });              &lt;br /&gt;      &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;          &lt;/span&gt;Red: &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkRed" &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkRed" &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="checkbox" &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;="red" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="mutuallyexclusive"&amp;gt;&lt;br /&gt;          &lt;/span&gt;Blue: &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkBlue" &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkBlue" &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="checkbox" &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;="blue" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="mutuallyexclusive"&amp;gt;&lt;br /&gt;          &lt;/span&gt;Green: &lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkGreen" &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;="chkGreen" &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;="checkbox" &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;="green" &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;="mutuallyexclusive"&amp;gt;&lt;br /&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;  &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;html&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;A running sample is &lt;a href="http://gschuager.com.ar/blog/mutuallyexclusive.htm"&gt;here&lt;/a&gt;. (Dead link)&lt;br /&gt;&lt;/p&gt;&lt;p&gt;(Based on &lt;a href="http://forums.asp.net/p/1303486/2549303.aspx"&gt;this&lt;/a&gt; post. Thanks Tony.)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-7405498766024515678?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/w_2-66ajmk6w1Kx8KFD-A48pQhE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/w_2-66ajmk6w1Kx8KFD-A48pQhE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/w_2-66ajmk6w1Kx8KFD-A48pQhE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/w_2-66ajmk6w1Kx8KFD-A48pQhE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/bdI_VGtp0q0/mutually-exclusive-checkboxes-with.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total><feedburner:origLink>http://blog.schuager.com/2008/09/mutually-exclusive-checkboxes-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-7491984612456421274</guid><pubDate>Tue, 02 Sep 2008 23:25:00 +0000</pubDate><atom:updated>2008-09-03T13:03:51.243-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">jquery</category><category domain="http://www.blogger.com/atom/ns#">ASP.NET MVC</category><title>jQuery Autocomplete + JSON + ASP.NET MVC</title><description>&lt;p&gt;It just took me a while to get this running, so maybe this could save you some time.&lt;/p&gt;  &lt;p&gt;The requirement was to use the &lt;a href="http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/"&gt;jQuery Autocomplete plugin&lt;/a&gt; to aid in the selection of some data coming from a database in an application using ASP.NET MVC Preview 4.&lt;/p&gt;  &lt;p&gt;If you would like to see this plugin in action, refer to its &lt;a href="http://jquery.bassistance.de/autocomplete/demo/"&gt;demo page&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions"&gt;documentation&lt;/a&gt; of the plugin states that when a remote source is used, the result must be returned formatted with one value on each line; but since I want to return a list of complex objects I would like to use JSON... googling around I've arrived to &lt;a href="http://www.it-eye.nl/weblog/2008/08/23/using-jquery-autocomplete-with-grails-and-json/"&gt;this&lt;/a&gt; page and learnt about the &lt;strong&gt;parse&lt;/strong&gt; and &lt;strong&gt;dataType&lt;/strong&gt; options that are not documented... using this options you can make the autocomplete plugin to receive remote data in JSON format.&lt;/p&gt;  &lt;p&gt;So... this is the JS code that configures the plugin:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="code"&gt;$(document).ready( &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;br /&gt;  $(&lt;span style="color: rgb(163, 21, 21);"&gt;'#signalName'&lt;/span&gt;).autocomplete(&lt;span style="color: rgb(163, 21, 21);"&gt;'&amp;lt;%=Url.Action("Lookup", "Signal") %&amp;gt;'&lt;/span&gt;, {&lt;br /&gt;      dataType: &lt;span style="color: rgb(163, 21, 21);"&gt;'json'&lt;/span&gt;,&lt;br /&gt;      parse: &lt;span style="color:blue;"&gt;function&lt;/span&gt;(data) {&lt;br /&gt;          &lt;span style="color:blue;"&gt;var &lt;/span&gt;rows = &lt;span style="color:blue;"&gt;new &lt;/span&gt;Array();&lt;br /&gt;          &lt;span style="color:blue;"&gt;for&lt;/span&gt;(&lt;span style="color:blue;"&gt;var &lt;/span&gt;i=0; i&amp;lt;data.length; i++){&lt;br /&gt;              rows[i] = { data:data[i], value:data[i].SignalName, result:data[i].SignalName };&lt;br /&gt;          }&lt;br /&gt;          &lt;span style="color:blue;"&gt;return &lt;/span&gt;rows;&lt;br /&gt;      },&lt;br /&gt;      formatItem: &lt;span style="color:blue;"&gt;function&lt;/span&gt;(row, i, n) {&lt;br /&gt;          &lt;span style="color:blue;"&gt;return &lt;/span&gt;row.SignalName + &lt;span style="color: rgb(163, 21, 21);"&gt;' - ' &lt;/span&gt;+ row.Description;&lt;br /&gt;      },&lt;br /&gt;      width: 300,&lt;br /&gt;      mustMatch: &lt;span style="color:blue;"&gt;true&lt;/span&gt;,&lt;br /&gt;  });&lt;br /&gt;});&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;Here, I specify that the rows in the drop down will have the "SignalName - Description" format, and that the value that will be put in the textbox is the SignalName property of the selected item... this is done with the &lt;strong&gt;value&lt;/strong&gt; property of the &lt;strong&gt;rows&lt;/strong&gt; array.&lt;/p&gt;&lt;p&gt;The ASP.NET MVC controller/action that provides the data will be something like this:&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SignalController &lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;Controller &lt;/span&gt;{&lt;br /&gt;  ...&lt;br /&gt;  &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ActionResult &lt;/span&gt;Lookup(&lt;span style="color:blue;"&gt;string &lt;/span&gt;q, &lt;span style="color:blue;"&gt;int &lt;/span&gt;limit)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color:blue;"&gt;var &lt;/span&gt;list = signalRepository.SearchByPage(q, 1, limit);&lt;br /&gt;      &lt;span style="color:blue;"&gt;var &lt;/span&gt;data = &lt;span style="color:blue;"&gt;from &lt;/span&gt;s &lt;span style="color:blue;"&gt;in &lt;/span&gt;list &lt;span style="color:blue;"&gt;select new &lt;/span&gt;{s.SignalName, s.Description};&lt;br /&gt;      &lt;span style="color:blue;"&gt;return &lt;/span&gt;Json(data);&lt;br /&gt;  }&lt;br /&gt;  ...&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;To get this running, don't forget to include the required javascript files and the css to style the autocomplete drop down. Refer to the documentation for more information.&lt;/p&gt;&lt;p&gt;That's all for now.&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-7491984612456421274?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VboaFLCaBJ9G4A3scDIE6vczEnU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VboaFLCaBJ9G4A3scDIE6vczEnU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VboaFLCaBJ9G4A3scDIE6vczEnU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VboaFLCaBJ9G4A3scDIE6vczEnU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/hsDXcQpp23s/jquery-autocomplete-json-apsnet-mvc.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">18</thr:total><feedburner:origLink>http://blog.schuager.com/2008/09/jquery-autocomplete-json-apsnet-mvc.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4340519225513884214</guid><pubDate>Thu, 28 Aug 2008 17:20:00 +0000</pubDate><atom:updated>2008-08-28T14:29:51.289-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">castle</category><category domain="http://www.blogger.com/atom/ns#">ASP.NET MVC</category><title>MvcContrib parameter binder's: CastleBind vs Deserialize</title><description>I'm using &lt;a href="http://www.blogger.com/www.mvccontrib.org"&gt;MvcContrib&lt;/a&gt; in my first ASP.NET MVC based project...&lt;br /&gt;I need to get an object built from the user entry in a web form, and to simplify this task I'm trying the ''parameter binding'' feature implemented in MvcContrib.&lt;br /&gt;&lt;br /&gt;I've tested both Deserialize and CastleBind attributes and the first big difference I've found between them is that the Deserialize attribute tries to fill every property in the newed object whether the property value is contained in the Request or not. Since I'm using my business objects directly in my controllers (shame for me), this behaviour fires some properties setter that weren't intended to run.&lt;br /&gt;&lt;br /&gt;The CastleBind attribute, which uses the Castle project Binder component, works as expected.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4340519225513884214?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/j4QNxdIxdlnKDsdXdOU58ZQSMf0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/j4QNxdIxdlnKDsdXdOU58ZQSMf0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/j4QNxdIxdlnKDsdXdOU58ZQSMf0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/j4QNxdIxdlnKDsdXdOU58ZQSMf0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/wG4Z5hXiU-8/mvccontrib-parameter-binders-castlebind.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2008/08/mvccontrib-parameter-binders-castlebind.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-6330577446580325848</guid><pubDate>Mon, 09 Jun 2008 22:28:00 +0000</pubDate><atom:updated>2008-06-09T21:12:25.458-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">castle</category><category domain="http://www.blogger.com/atom/ns#">ioc</category><category domain="http://www.blogger.com/atom/ns#">prism</category><title>Prism.WindsorPrismAdapter</title><description>&lt;a href="http://gschuager.com.ar/downloads/Prism.WindsorContainerAdapter.zip"&gt;Here&lt;/a&gt; is a port of the Prism.UnityContainerAdapter assembly that uses Windsor as the IoC container.&lt;br /&gt;It works with the &lt;a href="http://www.codeplex.com/CompositeWPF/Release/ProjectReleases.aspx?ReleaseId=14018"&gt;lastest&lt;/a&gt; drop of Prism source.&lt;br /&gt;&lt;br /&gt;You will have to resolve the references to make it compile.&lt;br /&gt;It uses Windsor trunk (build 812)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-6330577446580325848?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Hsfz_q3wP8NeDdc0GTx9eAW23V4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Hsfz_q3wP8NeDdc0GTx9eAW23V4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Hsfz_q3wP8NeDdc0GTx9eAW23V4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Hsfz_q3wP8NeDdc0GTx9eAW23V4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/8dGLhQvyxm4/prismwindsorprismadapter.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2008/06/prismwindsorprismadapter.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-2383409449126628276</guid><pubDate>Mon, 12 May 2008 18:52:00 +0000</pubDate><atom:updated>2008-05-12T16:58:53.373-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">tools</category><category domain="http://www.blogger.com/atom/ns#">nant</category><category domain="http://www.blogger.com/atom/ns#">xml</category><title>Appending an attribute to a XML node</title><description>&lt;p&gt;I just needed to append an attribute to a tag in a XML file, and it took me a  while to figure it out how to do it from the command line.&lt;br /&gt;&lt;br /&gt;The "easiest" way I've found is  using a tool called &lt;a href="http://xmlstar.sourceforge.net/"&gt;XMLStartlet&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt; &lt;p&gt;Some background... I am automating a Clickonce deployment through a nant  build file and I need to specify in the application manifest that one of the  files is a "&lt;a href="http://msdn.microsoft.com/es-es/library/6fehc36e%28VS.80%29.aspx"&gt;Data File&lt;/a&gt;". I'm using &lt;a href="http://msdn.microsoft.com/en-us/library/acz3y3te.aspx"&gt;mage&lt;/a&gt; to  generate the manifests.&lt;/p&gt; &lt;p&gt; &lt;/p&gt; &lt;p&gt;The app.exe.manifest XML file (application manifest) looks something like  this:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;xml &lt;/span&gt;&lt;span style="color:red;"&gt;version&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;1.0&lt;/span&gt;" &lt;span style="color:red;"&gt;encoding&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;utf-8&lt;/span&gt;"&lt;span style="color:blue;"&gt;?&amp;gt;&lt;br /&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;asmv1:assembly &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns:asmv3&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:asm.v3&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns:dsig&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;http://www.w3.org/2000/09/xmldsig#&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:asm.v2&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns:asmv1&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:asm.v1&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns:asmv2&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:asm.v2&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns:xsi&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;" &lt;span style="color:red;"&gt;xmlns:co.v1&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:clickonce.v1&lt;/span&gt;" &lt;span style="color:red;"&gt;xsi:schemaLocation&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd&lt;/span&gt;" &lt;span style="color:red;"&gt;manifestVersion&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;1.0&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;file &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;windsor.config.xml&lt;/span&gt;" &lt;span style="color:red;"&gt;size&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;819&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;hash&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:Transforms&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;     &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:Transform &lt;/span&gt;&lt;span style="color:red;"&gt;Algorithm&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;urn:schemas-microsoft-com:HashTransforms.Identity&lt;/span&gt;" &lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;   &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:Transforms&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:DigestMethod &lt;/span&gt;&lt;span style="color:red;"&gt;Algorithm&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;http://www.w3.org/2000/09/xmldsig#sha1&lt;/span&gt;" &lt;span style="color:blue;"&gt;/&amp;gt;&lt;br /&gt;   &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:DigestValue&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;EO69DP0Ewj2rOeFxdwSaqN6NY1s=&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;dsig:DigestValue&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;hash&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;file&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;asmv1:assembly&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;My requirement  was to append the writeableType attribute with value "applicationData" to the  shown "file" node. &lt;p&gt;To accomplish this I've used this command:&lt;/p&gt; &lt;p style="font-family: courier new; font-weight: bold;"&gt;xml ed -N a="urn:schemas-microsoft-com:asm.v1" -N  b="urn:schemas-microsoft-com:asm.v2" -a  "/a:assembly/b:file[@name='windsor.config.xml']/." -t attr -n writeableType -v  applicationData app.exe.manifest&lt;/p&gt;  &lt;p&gt;You get the resulting XML in the standard output (which need to be  redirected), and the result contains:&lt;br /&gt;&lt;/p&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;file &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;windsor.config.xml&lt;/span&gt;" &lt;span style="color:red;"&gt;size&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;819&lt;/span&gt;" &lt;span style="color:red;"&gt;writeableType&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;"&lt;span style="color:blue;"&gt;applicationData&lt;/span&gt;"&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;br /&gt;The XML namespaces make it somewhat tricky to get the correct arguments for  xml.exe, but when namespaces are not specified it turns out to be pretty easy  and straightforward.&lt;br /&gt;&lt;br /&gt;Remember also that if you want to include this command line in a nant script you will need to replace the double quotes with "&amp;amp; quot;" (without the space)&lt;br /&gt;&lt;p&gt;I think that XMLStartlet will be useful for me in the future, so this one goes  to my toolbox from now.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-2383409449126628276?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/phGbavLR9g7ge5U8S0edNio4H3Q/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/phGbavLR9g7ge5U8S0edNio4H3Q/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/phGbavLR9g7ge5U8S0edNio4H3Q/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/phGbavLR9g7ge5U8S0edNio4H3Q/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/mw81pDIOgEk/appending-attribute-to-xml-node.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total><feedburner:origLink>http://blog.schuager.com/2008/05/appending-attribute-to-xml-node.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-8117232750635785850</guid><pubDate>Fri, 02 May 2008 02:57:00 +0000</pubDate><atom:updated>2008-11-14T00:58:05.662-02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">castle</category><category domain="http://www.blogger.com/atom/ns#">nhibernate</category><title>NHibernate.Search with Castle.Facilities.NHibernateIntegration</title><description>&lt;p&gt;A few days ago I was looking to add full text search capability to an application that uses NHibernate, so I took a look at &lt;a href="http://www.ayende.com/Blog/archive/2007/04/02/NHibernate-Search.aspx"&gt;NHibernate.Search&lt;/a&gt;.&lt;br /&gt;The application also uses Windsor and the &lt;a href="http://castleproject.org/container/facilities/trunk/nhibernate/index.html"&gt;NH integration facility&lt;/a&gt;. This facility provides my repositories with an instance of ISessionManager (or ISessionFactory) which in turn is used to get the ISession.&lt;br /&gt;Next, I will explain roughly what you need to do to integrate NH.Search with an application that uses the NHibertante integration facility.&lt;br /&gt;At the end of the post you will find a link to download the full source code.&lt;br /&gt;&lt;br /&gt;The first thing we need to do is to initialize the NH.Search engine. This is done in your application startup as follows:  &lt;/p&gt;&lt;blockquote&gt;   &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;container = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WindsorContainer&lt;/span&gt;(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;XmlInterpreter&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"windsor.config.xml"&lt;/span&gt;));&lt;br /&gt;container.AddComponentLifeStyle(&lt;span style="color: rgb(163, 21, 21);"&gt;"nhibernate.session.interceptor"&lt;/span&gt;, &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IInterceptor&lt;/span&gt;),&lt;br /&gt;                           &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;SearchInterceptor&lt;/span&gt;), &lt;span style="color: rgb(43, 145, 175);"&gt;LifestyleType&lt;/span&gt;.Transient);&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;cfg = container.Resolve&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Configuration&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;sessionFactory = container.Resolve&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionFactory&lt;/span&gt;&amp;gt;();&lt;br /&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SearchFactory&lt;/span&gt;.Initialize(cfg, sessionFactory);&lt;/pre&gt;&lt;/blockquote&gt;The registration of the "nhibernate.session.interceptor" component is required for ISessionManager to return us ISession's already configured to use with NH.Search.&lt;br /&gt;Once we have everything in place we can define our repository class as follows:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;PostRepository&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionManager &lt;/span&gt;sessionManager;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;PostRepository(&lt;span style="color: rgb(43, 145, 175);"&gt;ISessionManager &lt;/span&gt;sessionManager)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;this&lt;/span&gt;.sessionManager = sessionManager;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;&amp;gt; GetAll()&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ISession &lt;/span&gt;session = sessionManager.OpenSession()) {&lt;br /&gt;       &lt;span style="color: rgb(43, 145, 175);"&gt;ICriteria &lt;/span&gt;crit = session.CreateCriteria(&lt;span style="color:blue;"&gt;typeof &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;));&lt;br /&gt;       &lt;span style="color:blue;"&gt;return &lt;/span&gt;crit.List&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;&amp;gt;();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public virtual void &lt;/span&gt;Save(&lt;span style="color: rgb(43, 145, 175);"&gt;Post &lt;/span&gt;post)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ISession &lt;/span&gt;session = sessionManager.OpenSession()) {&lt;br /&gt;       session.Save(post);&lt;br /&gt;       session.Flush();&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;And the Post class...&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Indexed&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;br /&gt;&lt;/span&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;private int &lt;/span&gt;id;&lt;br /&gt;&lt;span style="color:blue;"&gt;private string &lt;/span&gt;title;&lt;br /&gt;&lt;span style="color:blue;"&gt;private string &lt;/span&gt;content;&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;DocumentId&lt;/span&gt;]&lt;br /&gt;&lt;span style="color:blue;"&gt;public virtual int &lt;/span&gt;ID&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;id; }&lt;br /&gt;   &lt;span style="color:blue;"&gt;set &lt;/span&gt;{ id = &lt;span style="color:blue;"&gt;value&lt;/span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Field&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Index&lt;/span&gt;.Tokenized, Store = &lt;span style="color: rgb(43, 145, 175);"&gt;Store&lt;/span&gt;.Yes)]&lt;br /&gt;&lt;span style="color:blue;"&gt;public virtual string &lt;/span&gt;Title&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;title; }&lt;br /&gt;   &lt;span style="color:blue;"&gt;set &lt;/span&gt;{ title = &lt;span style="color:blue;"&gt;value&lt;/span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;[&lt;span style="color: rgb(43, 145, 175);"&gt;Field&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Index&lt;/span&gt;.Tokenized, Store = &lt;span style="color: rgb(43, 145, 175);"&gt;Store&lt;/span&gt;.Yes)]&lt;br /&gt;&lt;span style="color:blue;"&gt;public virtual string &lt;/span&gt;Content&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return &lt;/span&gt;content; }&lt;br /&gt;   &lt;span style="color:blue;"&gt;set &lt;/span&gt;{ content = &lt;span style="color:blue;"&gt;value&lt;/span&gt;; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;Post()&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;Post(&lt;span style="color:blue;"&gt;string &lt;/span&gt;title, &lt;span style="color:blue;"&gt;string &lt;/span&gt;content)&lt;br /&gt;{&lt;br /&gt;   &lt;span style="color:blue;"&gt;this&lt;/span&gt;.title = title;&lt;br /&gt;   &lt;span style="color:blue;"&gt;this&lt;/span&gt;.content = content;&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;Ok, now we have our repository and our entity. Now suppose that we want to add a Find method to PostRepository that returns all the Posts that contains a given text on its Title or Content properties.&lt;br /&gt;&lt;br /&gt;This method could be implemented as follows:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;&amp;gt; Find(&lt;span style="color:blue;"&gt;string &lt;/span&gt;text)&lt;br /&gt;{&lt;br /&gt;&lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ISession &lt;/span&gt;session = sessionManager.OpenSession()) {&lt;br /&gt;   &lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IFullTextSession &lt;/span&gt;fullTextSession = &lt;span style="color: rgb(43, 145, 175);"&gt;Search&lt;/span&gt;.CreateFullTextSession(((&lt;span style="color: rgb(43, 145, 175);"&gt;SessionDelegate&lt;/span&gt;)session).InnerSession)) {&lt;br /&gt;       &lt;span style="color:blue;"&gt;var &lt;/span&gt;queryString = &lt;span style="color:blue;"&gt;string&lt;/span&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"Title: {0} OR Content: {0}"&lt;/span&gt;, text);&lt;br /&gt;       &lt;span style="color:blue;"&gt;try &lt;/span&gt;{&lt;br /&gt;           &lt;span style="color:blue;"&gt;return &lt;/span&gt;fullTextSession.CreateFullTextQuery&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;&amp;gt;(queryString)&lt;br /&gt;               .List&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Post&lt;/span&gt;&amp;gt;();&lt;br /&gt;       }&lt;br /&gt;       &lt;span style="color:blue;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ParseException&lt;/span&gt;) {&lt;br /&gt;           &lt;span style="color:blue;"&gt;return null&lt;/span&gt;;&lt;br /&gt;       }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;/blockquote&gt;The important thing you have to look at here is that ISessionManager.OpenSession() doesn't return a ISessionImplementor which is required by Search.CreateFullTextSession... instead it returns a wrapper of type SessionDelegate (used to control the closing of the session when it is inside a transaction scope), so in order to be able to create the full text session we need to use the ISessionImplementor that the SessionDelegate wraps around ;)&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;h5&gt;Using ISessionFactory instead of ISessionManager&lt;/h5&gt;Another way of doing this is using ISessionFactory directly instead of ISessionManager. In this case, the component name of the IInterceptor that you have to register at startup is "nhibernate.sessionfactory.interceptor" and since the ISession return by ISessionFactory.OpenSession() is in fact an ISessionImplementor, the creation of the full text search session is reduced to:&lt;br /&gt;&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ISession &lt;/span&gt;session = sessionFactory.OpenSession()) {&lt;br /&gt;&lt;span style="color:blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IFullTextSession &lt;/span&gt;fullTextSession = &lt;span style="color: rgb(43, 145, 175);"&gt;Search&lt;/span&gt;.CreateFullTextSession(session)) {&lt;/pre&gt;&lt;/blockquote&gt;&lt;h5&gt;Sample Solution&lt;/h5&gt;I have put together a sample solution that implements this concepts. It uses a SQLite database to save you from setting up the DB in a RDBMS, so it should run "out of the zip".&lt;br /&gt;Although a winform application is not the optimal case to illustrate NH.Search usage (it is oriented to be used on server side components or in web applications) it serves the purpose of this post.&lt;br /&gt;You can download it from &lt;a href="http://www.mediafire.com/?mrwnoz2nnzh"&gt;here&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-8117232750635785850?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/IhpWrvGQwQP671hk9QWLZRPvdmg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IhpWrvGQwQP671hk9QWLZRPvdmg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/IhpWrvGQwQP671hk9QWLZRPvdmg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/IhpWrvGQwQP671hk9QWLZRPvdmg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/6paWw2EXqKs/nhibernatesearch-with.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">10</thr:total><feedburner:origLink>http://blog.schuager.com/2008/04/nhibernatesearch-with.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6999142696093813888.post-4488182251238611482</guid><pubDate>Wed, 02 Apr 2008 00:40:00 +0000</pubDate><atom:updated>2008-04-17T00:30:58.989-03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">netcf</category><category domain="http://www.blogger.com/atom/ns#">ioc</category><title>Inversion of Control container on .NET Compact Framework</title><description>Once upon a time I started to write an ambitiuos embedded application... I had very little knowledge about .NET, Design Patterns, TDD, DDD and all those cool things at that time.&lt;br /&gt;My application grew over time and suddenly it became an unmaintainable monster. It was composed of a big web of interrelated components each of one did his work the way they wanted. It was full of singletons and static classes...&lt;br /&gt;&lt;br /&gt;Since that time I've studied and learned a lot of usefull things like the ones that I mentioned above, and at some point I decided that I need to do something to prevent the monster from eating me.&lt;br /&gt;&lt;br /&gt;This is where "&lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2008/03/13/observations.aspx"&gt;The Great Refactoring&lt;/a&gt;" came in.... TDD... MVP... IoC!&lt;br /&gt;&lt;br /&gt;Among other things I was in the need of a IoC/DI tool that works under .NET Compact Framework. The only one that I've found is &lt;a href="http://www.codeplex.com/uIoC"&gt;uIoC&lt;/a&gt; but it lacks some features that I consider "must haves" for such a tool (constructor injection for example).&lt;br /&gt;So, I decided to write my own Inversion of Control Container... so CompactContainer borns.&lt;br /&gt;&lt;br /&gt;The API is roughly based on Castle MicroKernel and the main features are:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Constructor injection&lt;/li&gt;&lt;li&gt;Handlers for custom creation of different types of objects: I've used this to implement a PersistantaceHandler that attemps to create the asked component by deserializing it from disk, and if it can't then instanciate a new one in the usual way. This feature could be use to implement an IStartable interface with the same effect as the Startable Facility of MicroKernel. (not in the repository yet)&lt;/li&gt;&lt;li&gt;Lifestyles: Singleton and Transient (the only ones I've needed so far)&lt;/li&gt;&lt;/ul&gt;The source code can be found at &lt;a href="http://code.google.com/p/compactcontainer/"&gt;http://code.google.com/p/compactcontainer/&lt;/a&gt;&lt;br /&gt;Here you can find 2 solutions: one targeted to Pocket PC platform (can be retargeted as needed) and other composed of 2 project, CompactContainer and CompactContainer.Tests that is used to run the tests under the full .NET Framework (uses MBUnit).&lt;br /&gt;&lt;br /&gt;My 2 cents...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6999142696093813888-4488182251238611482?l=blog.schuager.com'/&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/pctnGjXTnNaRDaY8HxYhHltQvFs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pctnGjXTnNaRDaY8HxYhHltQvFs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/pctnGjXTnNaRDaY8HxYhHltQvFs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/pctnGjXTnNaRDaY8HxYhHltQvFs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://feedproxy.google.com/~r/gschuager/~3/eoFZj0lx7hs/inversion-of-control-container-on-net.html</link><author>gschuager@gmail.com (Germán Schuager)</author><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total><feedburner:origLink>http://blog.schuager.com/2008/04/inversion-of-control-container-on-net.html</feedburner:origLink></item></channel></rss>
