Skip to content

April 4, 2014

4

Repository Encapsulation

by adamstephensen

It’s important to consider how strictly you should encapsulate your data access logic within the repository layer

Repository Layers allow us to abstract our data access away from our application.

There are two common approaches.

 

Approach #1: Fully Encapsulated – The Strict Repository

In this model you pass in simple types or DTOs as parameters to repository methods and receive enumerated lists of results in return. (ie. IEnumerable / ICollection / IList)

 

Benefit: Your data access is 100% completely abstracted away from your core application.
This model only requires the Entity Framework to be added to your Data project.

Cons: It’s more work. Every data access query requires a new repository method and a change to the interface.

 

Approach #2: The Flexible Repository

In this model you encapsulate complex queries, or queries that will be re-used inside your repository, but simple queries can be performed by the calling client because the Get() method accepts a filter operator and returns an IQueryable that provides great flexibility.

 

Benefit: It’s less work. A developer can build user interface that performs simple CRUD operations on the entity without needing to add methods to the Repository.

 

Cons: This is a ‘leaky’ abstraction where your implementation is likely to leak into your other layers. The Entity Framework is required as a reference in any project where you are performing queries that require EF specific extensions (e.g. .Include(c=>c.Children) )

 

For each application you need to weight the cons and benefits of the two options.

For a long-lived enterprise application, I recommend fully encapsulated repositories.

For simple applications that are essentially providing forms over data functionality, the flexible repository enables developers to quickly add value to the application, while still leveraging most of the benefits of implementing a repository layer.

 

TODO: Add Code Samples

Advertisements
4 Comments Post a comment
  1. Hi! Quick question that’s totally off topic. Do you know how to make your site mobile friendly?
    My weblog looks weird when viewing from my apple iphone.
    I’m trying to find a theme or plugin that might be able to correct
    this issue. If you have any recommendations, please share.
    With thanks!

    Reply
  2. Sep 10 2014

    Hi there mates, how is all, and what you desire to say concerning this paragraph, in my view its truly remarkable in favor of me.

    Reply
  3. We stumbled over here by a different web page and thought I should check things
    out. I like what I see so now i am following you. Look forward
    to looking over your web page for a second time.

    Reply
  4. Jan 8 2015

    Hello I am so thrilled I found your blog page, I really found you by
    mistake, while I was looking on Askjeeve for something
    else, Nonetheless I am here now and would just like to say many thanks
    for a incredible post and a all round entertaining blog
    (I also love the theme/design), I don’t have time to look over it all at the minute but I have bookmarked it and also included your
    RSS feeds, so when I have time I will be back to read much more, Please do keep up the fantastic work.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments