maonet technotes

IOC(SM):MOC(RM):TDD(NU):SCC(TFS):ORM(L2S):JSL(Jq):CIS(CC)

Repository pattern in CSLA

Posted by Frank Mao on October 6, 2008

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 to “Repository pattern in CSLA”

  1. Kevin Fairclough said

    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. Frank Mao said

    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. [...] object through the use of the ObjectFactory and StructureMap. This is a setup very similar to what Frank is using in his project. However we are not using DTO objects in between the Repository and the [...]

  4. Frank Mao said

    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();
    }

  5. Ryan said

    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.

  6. [...] Posts Repository pattern in CSLAWhere should I distinct and sort data result from NHibernate?Moving WCF Service to IISNHibernate and [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>