p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET Design Patterns (http://p2p.wrox.com/forumdisplay.php?f=598)
-   -   Changes needed for Linq2Sql Repository? (http://p2p.wrox.com/showthread.php?t=81887)

ksouthworth December 11th, 2010 12:55 PM

Changes needed for Linq2Sql Repository?
Hi Scott,
Really enjoyed the book, great stuff.

I was wondering what changes would be needed to the code to use a Linq2Sql repository (or some other persistence technology like SubSonic, EntityFramework, etc). It seems that NHibernate does a very good job of letting you code "model first" using regular POCO objects, and the separation of concerns in the Agatha's code is great. However, Linq2Sql takes more of a "database-first" approach and generates classes/objects from your database schema. Is it still possible to use the Agatha's Storefront Model project/classes with a persistence mechanism other than NHibernate? If so, I'd be very interested to see what code changes would be needed.

I'm wondering if I should just bite the bullet and learn NHibernate, but I've been trying out Mindscape LightSpeed (http://www.mindscapehq.com/products/...d/default.aspx) and like it so far, and was curious how the Agatha's code might be different if it was not using NHibernate. Would the strict separation of layers not be possible with something other than NHib?

Thanks again for a great book.

elbandit December 14th, 2010 10:16 AM

Hi ksouthworth,

Thanks the buying the book and taking time to post this question.

You can use EntityFramework and Linq2SQL in a code first manner, check out the code from chapter 7, here the entities are POCO and there is a Entity Framework and a NHibernate repository.

Here are some good videos on code first Entity Framework development:

Julie Lerman Domain Driven Entity Framework

Julie Lerman Entity Framework Persistance Ignorance

However you can still use database first and just use something like Linq2SQL as a strongly typed data access layer, a bit like this..


  public class CustomerRepository : ICustomerRepository
      public Customer FindBy(int id)
          // Actual entity built code first
          Customer customerEntity = null;
          // Object created by Linq2SQL
          CustomerData customerDataTable = DataContextFactory
                          .FirstOrDefault<CustomerData>(c => c.Id == id);
          // If there is a matching object
          if (customerDataTable != null)         
              // Create a Customer Entity from it
              customerEntity = ConvertIntoACustomerEntity(customerDataTable);
          return customerEntity;
      public Customer ConvertIntoACustomerEntity(CustomerData customerDataTable)
          return new Customer()
                          Id = customerDataTable.Id,
                          Name = new Name(customerDataTable.FirstName, customerDataTable.LastName)

But this way you would have to create extra classes for lazy loading etc.

Unfortunately I haven't used SubSonic or Mindscape LightSpeed, but if it follows the Active Record pattern you should be able to use the same method as with Linq2SQL.

I am a couple of weeks away from adding an Entity Framework repository layer to the Agatha solution, plus replacing the QueryObjects with Linq Expression trees, as well as moving to a Command Query Responsibility Segregation type architecture so hopefully this will help you out.

Let me know if I have missed something or not answered ok.


All times are GMT -4. The time now is 06:39 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.