0

I currently have a linq to sql project that was used for one project and not much work was put into it, only the generated context and entities were used. Since then the project has started to become referenced in other projects and is growing rather quickly. My concern at first was duplicate code, for example:

using (DataContext dc = new DataContext())
{
    User myUser = dc.Users.Where(u => u.UserId == 1).SingleOrDefault();
}

This could be used in my project, but also used in someone else's project. I don't want smelly projects...

Lately we have started implementing the repository design so we even have a single repository that can handle multiple entities, so now queries go to the repository rather than the data context directly.

We have implemented a generic repository with methods like All() and FindAll() etc... but my question is when we come to doing more complex things like creating a record that involves creating numerous different entities, does this go into the repository layer or do I need to start looking at another design pattern?

4
  • 1
    please don't cross-post: stackoverflow.com/questions/26807675/…
    – gnat
    Nov 7, 2014 at 18:58
  • @gnat, I was advised to post here. Are you suggesting I delete the SO question?
    – KnottytOmo
    Nov 7, 2014 at 18:59
  • 1
    @KnottytOmo: For future reference, it is possible for admins to migrate questions from one site to another. If you feel that a question should be migrated, flag the question and explain that you think it should be migrated. Nov 7, 2014 at 19:06
  • Thank you for your advice, @FrustratedWithFormsDesigner. I will keep that in mind for future reference.
    – KnottytOmo
    Nov 7, 2014 at 19:06

1 Answer 1

1

A Save() method for a complex object would go into your Repository pattern interface. No, you don't necessarily need to use a different pattern.

A Repository should ideally abstract away all underlying storage (i.e. DB, memory collection, XML file etc) implementation detail from the object model that it works with. So if you are creating a complex object which has relationships between different entities, model this as a set of classes and write a method on your Repository to accept that type and do the dirty work of creating the classes in your physical storage device (whatever that may be).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.