Alternatives to the repository pattern for encapsulating ORM logic?

I’ve just had to switch out an ORM and it was a relatively daunting task, because the query logic was leaking everywhere. If i’d ever had to develop a new application, my personal preference would be to encapsulate all query logic (using an ORM) to futureproof it for change. Repository pattern is quite troublesome to code and maintain so i was wondering if there are any other patterns to solve the problem ?

I can foresee posts about not adding extra complexity before it’s actually needed, being agile etc. but i’m only interested about the existing patterns solving a similar problem in a simpler way.

My first thought was to have a generic type repository, to which i add methods as needed to specific type repository classes via extension methods, but unit testing static methods is awfully painful. IE:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static class PersonExtensions
{
public static IEnumerable<Person> GetRetiredPeople(this IRepository<Person> personRep)
{
// logic
}
}
</code>
<code>public static class PersonExtensions { public static IEnumerable<Person> GetRetiredPeople(this IRepository<Person> personRep) { // logic } } </code>
public static class PersonExtensions
{
    public static IEnumerable<Person> GetRetiredPeople(this IRepository<Person> personRep)
    {
        // logic
    }
}

2

First of all: A generic repository should be considered to be a base class and not complete implementations. It should help you to get the common CRUD methods in place. But you still have to implement the query methods:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class UserRepository : EntityFrameworkRepository<User>, IUserRepository
{
public UserRepository(DbContext context){}
public ICollection<Person> FindRetired()
{
return context.Persons.Where(p => p.Status == "Retired").ToList();
}
}
</code>
<code>public class UserRepository : EntityFrameworkRepository<User>, IUserRepository { public UserRepository(DbContext context){} public ICollection<Person> FindRetired() { return context.Persons.Where(p => p.Status == "Retired").ToList(); } } </code>
public class UserRepository : EntityFrameworkRepository<User>, IUserRepository
{
    public UserRepository(DbContext context){}

    public ICollection<Person> FindRetired()
    {
        return context.Persons.Where(p => p.Status == "Retired").ToList();
    }
}

Second:

Do not return IQueryable. It’s a leaky abstraction. Try to implement that interface yourself or use it without knowledge about the underlying db provider. For instance, every db provider has it’s own API for eagerly load entities. That’s why it’s a leaky abstraction.

Alternative

As an alternative you can use queries (for instance as defined by the Command/Query separation pattern). CQS is described in wikipedia.

You basically create query classes which you invoke:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class GetMyMessages
{
public GetMyMessages(IDbConnection connection)
{
}
public Message[] Execute(DateTime minDate)
{
//query
}
}
</code>
<code>public class GetMyMessages { public GetMyMessages(IDbConnection connection) { } public Message[] Execute(DateTime minDate) { //query } } </code>
public class GetMyMessages
{
    public GetMyMessages(IDbConnection connection)
    {
    }


    public Message[] Execute(DateTime minDate)
    {
        //query
    }
}

the great thing with queries is that you can use different data access methods for different queries without cluttering the code. you can for instance use a web service in one, nhibernate in another and ADO.NET in a third.

If you are interested in a .NET implementation read my article: http://blog.gauffin.org/2012/10/griffin-decoupled-the-queries/

6

First think I say is that ORM is already big enough abstraction over your database. Some ORMs provide binding to all common relational databases (NHibernate has bindings to MSSQL, Oracle, MySQL, Postgress, etc..) So building new abstraction on top of it doesn’t seem profitable to me. Also, argummenting, that you need to “abstract away” this ORM is meaningless.

If you still want to build this abstraction, I would go against Repository pattern. It was great in age of pure SQL, but it is quite troublesome in face of modern ORM. Mainly because you end up re-implementing most of the ORM features, like CRUD operations and querying.

If I was to build such abstractions, I would use these rules/patterns

  • CQRS
  • Use as much as possible of existing ORM features
  • Encapsulate only complex logic and queries
  • Try to have “plumbing” of the ORM hidden inside an architecture

In concrete implementation, I would use CRUD operations of ORM directly, without any wrapping. I would also do simple queries directly in code. But complex queries would be encapsulated in their own objects. To “hide” the ORM, I would try to inject the data context transparently into an service/UI objects and have same done to query objects.

Last thing I would like to say, is that many people use ORM without knowing how to use it and how to make best “profit” out of it. People recommending repositories are usually of this kind.

As a recommended reading, I would say Ayende’s blog, especially this article.

2

The problem with the leaky implementation is that you need many different filter conditions.

You can narrow this api if you implement a repository method FindByExample and use it like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>// find all retired persons
Person filter = new Person {Status=PersonStatus.Retired};
IEnumerable<Person> found = personRepository.FindByExample(filter);
// find all persons who have a dog named "sam"
Person filter = new Person();
filter.AddPet(new Dog{Name="sam"});
IEnumerable<Person> found = personRepository.FindByExample(filter);
</code>
<code>// find all retired persons Person filter = new Person {Status=PersonStatus.Retired}; IEnumerable<Person> found = personRepository.FindByExample(filter); // find all persons who have a dog named "sam" Person filter = new Person(); filter.AddPet(new Dog{Name="sam"}); IEnumerable<Person> found = personRepository.FindByExample(filter); </code>
// find all retired persons
Person filter = new Person {Status=PersonStatus.Retired};
IEnumerable<Person> found = personRepository.FindByExample(filter);


// find all persons who have a dog named "sam"
Person filter = new Person();
filter.AddPet(new Dog{Name="sam"});
IEnumerable<Person> found = personRepository.FindByExample(filter);

Consider making your extensions operate on IQueryable instead of IRepository.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static class PersonExtensions
{
public static IQueryable<Person> AreRetired(this IQueryable<Person> people)
{
return people.Where(p => p.Status == "Retired");
}
}
</code>
<code>public static class PersonExtensions { public static IQueryable<Person> AreRetired(this IQueryable<Person> people) { return people.Where(p => p.Status == "Retired"); } } </code>
public static class PersonExtensions
{
    public static IQueryable<Person> AreRetired(this IQueryable<Person> people)
    {
        return people.Where(p => p.Status == "Retired");
    }
}

To unit test:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>List<Person> people = new List<Person>();
people.Add(new Person() { Name = "Bob", Status = "Retired" });
people.Add(new Person() { Name = "Sam", Status = "Working" });
var retiredPeople = people.AsQueryable().AreRetired();
// assert that Bob is in the list
// assert that Sam is not in the list
</code>
<code>List<Person> people = new List<Person>(); people.Add(new Person() { Name = "Bob", Status = "Retired" }); people.Add(new Person() { Name = "Sam", Status = "Working" }); var retiredPeople = people.AsQueryable().AreRetired(); // assert that Bob is in the list // assert that Sam is not in the list </code>
List<Person> people = new List<Person>();
people.Add(new Person() { Name = "Bob", Status = "Retired" });
people.Add(new Person() { Name = "Sam", Status = "Working" });

var retiredPeople = people.AsQueryable().AreRetired();
// assert that Bob is in the list
// assert that Sam is not in the list

No fancing mocking required for testing.
You can also combine these extension methods to create more complex queries as needed.

1

I wrote a pretty neat query object pattern for NHibernate here: https://github.com/shaynevanasperen/NHibernate.Sessions.Operations

It works by using an interface like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IDatabases
{
ISessionManager SessionManager { get; }
T Query<T>(IDatabaseQuery<T> query);
T Query<T>(ICachedDatabaseQuery<T> query);
void Command(IDatabaseCommand command);
T Command<T>(IDatabaseCommand<T> command);
}
</code>
<code>public interface IDatabases { ISessionManager SessionManager { get; } T Query<T>(IDatabaseQuery<T> query); T Query<T>(ICachedDatabaseQuery<T> query); void Command(IDatabaseCommand command); T Command<T>(IDatabaseCommand<T> command); } </code>
public interface IDatabases
{
    ISessionManager SessionManager { get; }

    T Query<T>(IDatabaseQuery<T> query);
    T Query<T>(ICachedDatabaseQuery<T> query);

    void Command(IDatabaseCommand command);
    T Command<T>(IDatabaseCommand<T> command);
}

Given a POCO entity class like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Database1Poco
{
public int Property1 { get; set; }
public string Property2 { get; set; }
}
</code>
<code>class Database1Poco { public int Property1 { get; set; } public string Property2 { get; set; } } </code>
class Database1Poco
{
    public int Property1 { get; set; }
    public string Property2 { get; set; }
}

You can build query objects like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Database1PocoByProperty1 : DatabaseQuery<Database1Poco>
{
public override Database1Poco Execute(ISessionManager sessionManager)
{
return sessionManager.Session.Query<Database1Poco>().SingleOrDefault(x => x.Property1 == Property1);
}
public int Property1 { get; set; }
}
</code>
<code>class Database1PocoByProperty1 : DatabaseQuery<Database1Poco> { public override Database1Poco Execute(ISessionManager sessionManager) { return sessionManager.Session.Query<Database1Poco>().SingleOrDefault(x => x.Property1 == Property1); } public int Property1 { get; set; } } </code>
class Database1PocoByProperty1 : DatabaseQuery<Database1Poco>
{
    public override Database1Poco Execute(ISessionManager sessionManager)
    {
        return sessionManager.Session.Query<Database1Poco>().SingleOrDefault(x => x.Property1 == Property1);
    }

    public int Property1 { get; set; }
}

And then use them like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var database1Poco = _databases.Query(new Database1PocoByProperty1 { Property1 = 1 });
</code>
<code>var database1Poco = _databases.Query(new Database1PocoByProperty1 { Property1 = 1 }); </code>
var database1Poco = _databases.Query(new Database1PocoByProperty1 { Property1 = 1 });

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật