Repository pattern in CSLA

2008 October 6
by Frank Mao

We use Repository Pattern inside our CSLA BO to extract data access code into a separate class – repository object,  because we use interface IRepository to loose couple BO and concrete Repository object and implement “design by contract”, so BO doesn’t care which technology repository is using, either using ADO.net or Linq to SQL, or NHinbernate or Entity Framework .

The benefits of using this model include:

  • Loose coupling between BL and DAL
  • Easy to switch to different Data Access technology
  • Central place to control and switch IOC container

Updated at Feb. 09, 2009:
Found this post about the difference between DAO and Repository pattern. Yes, my model is more similar to DAO, because I was following one Repo/DAO per table rule. In fact, I’m on the progress of switching to the new Csla.ObjectFactory which is a real repository class, or, at lease, can be a repository wrapper, as Ryan did in his example.

6 Responses leave one →
  1. 2008 October 9
    Kevin Fairclough permalink

    I like this approach, and we are thinking of using a similar technique. For me this is DAO not a repository (although I’m pretty new to this.) From what I have read the Repository pattern lives above the BO.

    Regardless of that, have you had any issues with memory, as you will have to copy the contents of DTO into BO. If you have a BusinessListBase with many records then the memory will consume twice the amount?

  2. 2008 October 9

    I always thought DAO and Repository are almost same. Maybe I should look back to the defination more detail. Anyway I think if using LinQ2Sql yield return properly, memeroy won’t be a big issue.

  3. 2008 October 10

    In case you are still not clear. Here is my IRepository definition:

    public interface IRepository
    {

    DTO FindById(int id);

    DTO Insert(DTO data);
    DTO Update(DTO newData, DTO oldData); // Our Stored Procedure is very old fashion. We don’t have choice.

    void Delete(DTO data);

    }

    public interface IDynamicQueryable
    {
    IEnumerable FindAllByCriteria(string criteria);
    IEnumerable FindAllByCriteria(Func criteria);
    }

    public interface INameValueListRepository
    {
    Dictionary GetAll();
    }

  4. 2008 October 22

    Frank,

    I posted my IRepository Code on my blog: http://www.techfocus2.com/2008/10/irepository-code/

    Also, comments are working now, thanks for pointing that out.

Trackbacks & Pingbacks

  1. TechFocus2.0 » CSLA 3.6: Object Factory intro
  2. Mock/Test DataAccess Funtions in CSLA « maonet technotes

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS