Windows Support Number

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 17 September 2009

Repository pattern - my preferred implementation...

Posted on 05:29 by Unknown
Okay it's nothing new and not even original but I wouldn't to get down my currently preferred implementation of the repository pattern. I suppose this was prompted by a blog by Jimmy Bogard and Oren's statement a couple of months ago the repository pattern may be near then end of it's life.

I still think in the .Net world they have great relevance as most .Net devs can't organise code for toffee and when you try and introduce layering into an application the use of an explicit repository layer is the first layer they seem to understand.

So here is my current repository flavour - strawberry with a twist of lemon...


 public sealed class Repository<T1, T2> : IRepository<T1, T2> where T1 : IEntity<T2>
{
private readonly ISession _session;
private readonly string _traceType;

public Repository(IProvideSessions sessionFactory)
{
_traceType = string.Format("Repository<{0}, {1}>: ", typeof(T1).Name, typeof(T2).Name);
_session = sessionFactory.GetSession();
}

public T1 Get(T2 id)
{
Trace.WriteLine(string.Format("{0} Get - '{1}'.", _traceType, id));
return _session.Get<T1>(id);
}

public void Save(T1 instance)
{
Trace.WriteLine(string.Format("{0} Save - '{1}'.", _traceType, instance.Id));
_session.Save(instance);
}

public void Delete(T1 instance)
{
Trace.WriteLine(string.Format("{0} Delete - '{1}'.", _traceType, instance.Id));
_session.Delete(instance);
}

public T1 FindOne(IFindable findable)
{
Trace.WriteLine(string.Format("{0} FindOne, - '{1}'.", _traceType, findable.GetType().Name));
var criteria = findable.BuildCriteria();
return criteria.UniqueResult<T1>();
}

public IList<T1> FindAll(IFindable findable)
{
Trace.WriteLine(string.Format("{0} FindAll, - '{1}'.", _traceType, findable.GetType().Name));
var criteria = findable.BuildCriteria();
return criteria.List<T1>();
}
}

One of the first things to notice is the use of two generic types T1 & T2 - I'm not great at naming generic parameters so they never got better names. Hopefully it's obvious but T1 is the domain entity and T2 represents the 'Id' column for the entity (All DDD entities have Id's).

The other important feature is the 'FindOne' & 'FindAll' methods they take an implementation of the interface IFindable which performs the magic. Now this is where the generic repository starts to have a 'leaky abstraction' and this happens to be exposing nHibernate's ICriteria interface vai the IFindable interface. The implementation of the IFindable is responible for the creation of the NH criteria and returns this when requested, this is then executed by the repository and volia the results are returned.

So my current repository pattern is designed to be used with NH, but if a client dictates I can't use NH then I will modify the IFindable interface accordingly or I will terminate the contract depending on how I'm feeling :)

 public interface IFindable    {        ICriteria BuildCriteria();    }


An example of this could the a Findable class that returns an NH criteria that will return bank customers with a balance greater than million - Millionaires!



public sealed class FindValuedCustomers : IFindable
{
private readonly ISession _session;

public FindValuedCustomers(IProvideSessions sessionFactory)
{
_session = sessionFactory.GetSession();
}

public ICriteria BuildCriteria()
{
return _session.CreateCriteria(typeof(Account)).Add(Property.ForName("CurrentBalance").Gt(1000000));
}
}


Now I can see how Oren goes from this to the idea of just using NH Session anywhere in the code you previously used a Repository but I do think there is still some requirement to provide a layer & abstraction for testing purposes.

One other thing to note, I do believe the Repository pattern has valid uses outside of DDD, as Eric has stated most of the common patterns in DDD existed before the book.



Awkward Coder
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in nHbiernate Repository Pattern Criteria | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Unit testing Rx methods Timeout & Retry with moq
    Earlier this week I was trying to unit test an asynchronous service (Foo) which used another asynchronous service (Bar) internally and ran i...
  • Be careful of the culture when using Bing Maps REST API
    When developing the Bing Maps Wrapper service for the WP7Contrib we weren't aware of the importance of the instance of the CultureInfo ...
  • WP7Contrib: Bing Maps REST Services Wrapper - Deep Dive
    Following on from Rich's post introducing the Bing Maps Service in the WP7Contrib I'm going to explain in more detail how we built ...
  • MVVM anti-pattern: View code behind with no implementation
    I've seen rather a lot of this anti-pattern recently, to be explicit about what I mean, lets define this in terms of a WPF user control....
  • Implementing a message box using a visual overlay in MVVM
    I've blogged about implementing a busy indicator before, this post is an extension of this pattern to implement a message box - this is...
  • MVVM anti-pattern: Injecting the IoC container into a View Model
    This is another anti-pattern I've seen a lot recently, the dynamic use of the IoC container inside a view model to resolve child view mo...
  • Coupling and cohesion
    I was reading ploeh's blog  this morning and it made me think about coupling and cohesion in general. These are import concepts in softw...
  • Using IoC nested lifetime scopes with View Models in MVVM
    A common pattern you see when developing web services is the use of the Unit of Work applied to the HTTP request - anything that happens dur...
  • WP7Contrib: URL shortening in a WP7 app
    I needed the ability to shorten a URL for a WP7 app the other day so I could share a URL via the ShareLinkTask, more info about this task ca...
  • MVVM anti-pattern: explicitly using data context in View code behind
    I believe explicitly using the data context in the code behind of the view (custom, user control etc) in any MVVM application is an anti-pat...

Categories

  • .Net
  • .Net 4.5
  • Abstractions
  • Advertising
  • Agile
  • Agile Courage
  • AOP
  • Async
  • automated testing
  • Azure
  • Azure IIS RESTful development
  • BDD
  • Bing Maps
  • Bounded Context
  • C#
  • C# 5.0
  • Caching
  • Chocolatey
  • CLoud
  • CodePlex
  • Coding
  • Coding Building CI Testing
  • Coding C#
  • coding C# IoC StructureMap
  • Coding Functional-Programming
  • Coding REST Knowledge
  • Coding Services
  • Coding TDD Refactoring Agile
  • Command
  • continuous testing
  • coupling
  • CultureInfo
  • DAL
  • databases
  • DDD
  • DDD Coaching
  • DDD Domain Events Auditing nHibernate
  • DDD Entities Value Objects
  • Debugging
  • Design Patterns
  • Design Patterns Databases Auditing
  • Developement
  • Development
  • Development Coding
  • Development Process
  • Development unit testing
  • Development VS 2011
  • Diagnostics
  • Disposable
  • Exceptions
  • FINDaPAD
  • FindaPad Property Rental Windows Phone 7 Mobile Devices
  • Fun Coding Duct-Tape
  • Hotfixes
  • integration testing
  • IoC
  • jasmine
  • javascript
  • Jobs Development
  • LINQ
  • marketplace
  • Mobile Devices
  • Mocking
  • MSDN Coding
  • MSpec
  • Multilingual
  • MVC
  • MVVM
  • nCrunch
  • nHbiernate Repository Pattern Criteria
  • nHibernate Auditing Design Fluent
  • nHibnerate Entities Events Listeners
  • node.js
  • nodes.js
  • Nokia
  • NoSQL RavenDB Azure Development
  • Observations
  • OO
  • ORM
  • Performance
  • Portable Class Library
  • Portable Library
  • PostSharp
  • Process
  • Rants
  • RavenDB IIS 7.5 Development
  • Reactive
  • Reactive Extension
  • Reactive Extensions
  • ReadOnlyCollections
  • Resharper
  • REST Distributed-Systems
  • REST HTTP
  • rest web
  • RESTful
  • Rx
  • Serialization
  • Silverlight
  • Silverlight Installation
  • Task
  • TDD
  • TDD IoC DI
  • TDD Mocking
  • TDD Team Observation
  • Telerik
  • testing
  • threading
  • TPL
  • UI
  • Undo-Redo
  • unit testing
  • ViewModels
  • VS 2012
  • wcf
  • web api
  • Web Services
  • web services mobile devices data
  • WebAPI
  • Windows
  • Windows 8
  • windows phone
  • Windows Phone 7
  • WP7
  • WP7 Bing Maps Development Network HTTP
  • WP7 Bing Maps Development UK Crime
  • WP7 Bing Maps Development UK Crime Clustering
  • WP7 Bing Maps Development UK Polygons Clustering Performance
  • WP7 cryptography bouncy castle
  • WP7 Cultures C#
  • WP7 feedback development app store
  • WP7 Javascript web browser
  • WP7 MSBuild
  • WP7 ORM Databases performance
  • WP7 Serialisation
  • WP7 SilverlightSerializer C#
  • WP7 sqlite performance development
  • WP7 WP7Contrib Bing Maps Development
  • WP7 WP7Contrib Bing Maps Polygon Development
  • WP7 WP7Contrib CodePlex
  • WP7 WP7Contrib CodePlex Bing Maps Development
  • WP7 WP7Contrib CodePlex ObservableCollection
  • WP7 WP7Contrib ILMerge .Net
  • WP7 WP7Contrib Phone Maps
  • WP7 WP7Contrib SilverlightSerializer C#
  • WP7Contrib
  • WP7Contrib Bing Maps WP7
  • WP7Contrib WP7 Geo-Location development C#
  • WP7Contrib WP7 HTTP Compression
  • WP7Contrib WP7 Url Development Rx
  • WP7Dev
  • WPF
  • WPF Cultures
  • WuApi
  • XAML

Blog Archive

  • ►  2013 (16)
    • ►  November (5)
    • ►  September (3)
    • ►  August (1)
    • ►  July (1)
    • ►  June (3)
    • ►  May (2)
    • ►  January (1)
  • ►  2012 (44)
    • ►  November (2)
    • ►  October (8)
    • ►  September (5)
    • ►  August (2)
    • ►  July (4)
    • ►  June (3)
    • ►  May (1)
    • ►  April (2)
    • ►  March (13)
    • ►  February (4)
  • ►  2011 (52)
    • ►  December (3)
    • ►  November (5)
    • ►  October (7)
    • ►  September (7)
    • ►  August (11)
    • ►  July (4)
    • ►  May (2)
    • ►  April (1)
    • ►  March (5)
    • ►  February (3)
    • ►  January (4)
  • ►  2010 (1)
    • ►  August (1)
  • ▼  2009 (32)
    • ►  December (3)
    • ►  November (7)
    • ►  October (6)
    • ▼  September (11)
      • Mocks, Fakes, Stubs - why bother?
      • Distributed Systems are Coupled - Period!
      • Devlicio.us boys run out of duct tape!
      • Test Harnesses are counter productive...
      • So you thinking you're doing TDD?
      • I know nothing moments...
      • The secret all developers should know...
      • How to test a static dependency used inside a clas...
      • Application auditing - an example why I don't work...
      • Repository pattern - my preferred implementation...
      • Auditing with nHibernate...
    • ►  April (1)
    • ►  March (4)
Powered by Blogger.

About Me

Unknown
View my complete profile