Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
14 lines (12 sloc) 8.37 KB
---
layout: post
title: NHibernate repository that Oren won’t like
date: '2009-08-28T12:26:00.001+01:00'
tags: [nhibernate]
modified_time: '2009-08-28T12:26:17.354+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-7423455277386837982
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2009/08/nhibernate-repository-that-oren-wont.html
---
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; font-size: 8pt; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IRepository&lt;T&gt; : INHibernateQueryable&lt;T&gt;<br />{<br /> ITransaction BeginTransaction();<br /> <span style="color: #0000ff">void</span> Delete(T item);<br /> T Get(<span style="color: #0000ff">object</span> id);<br /> <span style="color: #0000ff">void</span> Save(T target);<br /> <span style="color: #0000ff">void</span> Update(T item);<br />}<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> NHibernateRepository&lt;T&gt; : IRepository&lt;T&gt;, IDisposable<br />{<br /> <span style="color: #0000ff">readonly</span> ISessionManager _sessionManager;<br /><br /> INHibernateQueryable&lt;T&gt; _queryable;<br /> ISession _session;<br /><br /> <span style="color: #0000ff">public</span> NHibernateRepository(ISessionManager sessionManager)<br /> {<br /> _sessionManager = sessionManager;<br /> }<br /><br /> INHibernateQueryable&lt;T&gt; AsQueryable<br /> {<br /> get<br /> {<br /> <span style="color: #0000ff">if</span> (_queryable == <span style="color: #0000ff">null</span>)<br /> _queryable = Session.Linq&lt;T&gt;();<br /> <span style="color: #0000ff">return</span> _queryable;<br /> }<br /> }<br /><br /> <span style="color: #0000ff">public</span> Type ElementType<br /> {<br /> get { <span style="color: #0000ff">return</span> AsQueryable.ElementType; }<br /> }<br /><br /> <span style="color: #0000ff">public</span> Expression Expression<br /> {<br /> get { <span style="color: #0000ff">return</span> AsQueryable.Expression; }<br /> }<br /><br /> <span style="color: #0000ff">public</span> IQueryProvider Provider<br /> {<br /> get { <span style="color: #0000ff">return</span> AsQueryable.Provider; }<br /> }<br /><br /> <span style="color: #0000ff">public</span> QueryOptions QueryOptions<br /> {<br /> get { <span style="color: #0000ff">return</span> AsQueryable.QueryOptions; }<br /> }<br /><br /> <span style="color: #0000ff">public</span> ISession Session<br /> {<br /> get<br /> {<br /> <span style="color: #0000ff">if</span> (_session == <span style="color: #0000ff">null</span>)<br /> _session = _sessionManager.OpenSession();<br /> <span style="color: #0000ff">return</span> _session;<br /> }<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()<br /> {<br /> <span style="color: #0000ff">if</span> (_session != <span style="color: #0000ff">null</span>)<br /> _session.Dispose();<br /> }<br /><br /> IEnumerator IEnumerable.GetEnumerator()<br /> {<br /> <span style="color: #0000ff">return</span> GetEnumerator();<br /> }<br /><br /> <span style="color: #0000ff">public</span> IEnumerator&lt;T&gt; GetEnumerator()<br /> {<br /> <span style="color: #0000ff">return</span> AsQueryable.GetEnumerator();<br /> }<br /><br /> <span style="color: #0000ff">public</span> IQueryable&lt;T&gt; Expand(<span style="color: #0000ff">string</span> path)<br /> {<br /> <span style="color: #0000ff">return</span> AsQueryable.Expand(path);<br /> }<br /><br /> <span style="color: #0000ff">public</span> ITransaction BeginTransaction()<br /> {<br /> <span style="color: #0000ff">return</span> Session.BeginTransaction();<br /> }<br /><br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Delete(T item)<br /> {<br /> Session.Delete(item);<br /> }<br /><br /> <span style="color: #0000ff">public</span> T Get(<span style="color: #0000ff">object</span> id)<br /> {<br /> <span style="color: #0000ff">return</span> Session.Get&lt;T&gt;(id);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Save(T target)<br /> {<br /> Session.SaveOrUpdate(target);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Update(T item)<br /> {<br /> Session.Update(item);<br /> }<br />}<br /><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> CustomerQueries<br />{<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IQueryable&lt;Customer&gt; ByName(<br /> <span style="color: #0000ff">this</span> IQueryable&lt;Customer&gt; repo,<br /> <span style="color: #0000ff">string</span> name)<br /> {<br /> <span style="color: #0000ff">return</span> repo.Where(x =&gt; x.Name == name);<br /> }<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> IQueryable&lt;Customer&gt; ByCity(<br /> <span style="color: #0000ff">this</span> IQueryable&lt;Customer&gt; repo,<br /> <span style="color: #0000ff">string</span> city)<br /> {<br /> <span style="color: #0000ff">return</span> repo.Where(x =&gt; x.City == city);<br /> }<br /> <br />}<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> MyPage<br />{<br /> IRepository&lt;Customer&gt; _repository;<br /><br /> <span style="color: #0000ff">public</span> MyPage(IRepository&lt;Customer&gt; repository)<br /> {<br /> _repository = repository;<br /> }<br /><br /> <span style="color: #0000ff">public</span> Customer UpdateCustomer()<br /> {<br /> <span style="color: #0000ff">return</span> _repository<br /> .ByCity(<span style="color: #006080">&quot;Aarhus&quot;</span>)<br /> .ByName(<span style="color: #006080">&quot;Sebastien&quot;</span>)<br /> .FirstOrDefault();<br /> }<br />}<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> when_filtering_by_city<br />{<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> the_wong_city_is_not_selected()<br /> {<br /> var myTestData = <span style="color: #0000ff">new</span> List&lt;Customer&gt;<br /> {<br /> <span style="color: #0000ff">new</span> Customer { City = <span style="color: #006080">&quot;Aarhus&quot;</span> },<br /> <span style="color: #0000ff">new</span> Customer { City = <span style="color: #006080">&quot;London&quot;</span> }<br /> };<br /> var funPlaceToBe = myTestData<br /> .AsQueryable()<br /> .ByCity(<span style="color: #006080">&quot;Aarhus&quot;</span>)<br /> .FirstOrDefault();<br /><br /> Debug.Assert(funPlaceToBe.City == <span style="color: #006080">&quot;London&quot;</span>);<br /> }<br />}<br /><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Customer<br />{<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> City { get; set; }<br />}</pre>
<br /></div>
You can’t perform that action at this time.