tags:

views:

31

answers:

1

In my repository layer I am mapping Linq-to-Sql objects to my application domain objects. The code below shows how I am doing this for a complex object, product:

        public IQueryable<Product> Products {
        get 
        {
            var result = from p in _db.Products
                         let images = GetProductImages(p.ProductID)
                         select new Product {
                             Id = p.ProductID,
                             SKU = p.SKU,
                             Name = p.Name,
                             ShortDescription = p.ShortDescription,
                             Description = p.Description,
                             Price = p.Price,
                             Weight = p.Weight,
                             StockQuantity = p.StockQuantity,
                             IsAvailable = p.IsAvailable,
                             AllowBackOrder = p.AllowBackOrder,
                             PageMetaData = ConvertXmlToDictionary(p.PageMetaData),
                             Images = new LazyList<ProductImage>(images)
                         };
            return result;
        }
    }

This works great for any child objects (e.g. Images).

However, everything is not so smooth when saving my product. As I am not using the Linq objects, presumably I lose any object tracking.

Therefore in an ideal world I would execute the following in my service layer:

Image productImage = new Image(params...); product.Images.Add(productImage); _myservice.SaveProduct(product);

To implement this in my repository I would need to loop through each Image object in the Images property of my Product domain object, check to see if it exists and then add or update the image.

This seems a lot of work if I just want to add one image.

Of course I could do something like:

Image productImage = new Image(params...); _myservice.AddProductImage(product.Id, productImage);

which would be more efficient (since I can add the Image and then the product/image mapping record) but is not as nice as the first example.

What would you recommend? Since my application is still in the early stages of development would it be worthwhile to start looking at EF 4.0 since I would lose the need to have to map my domain objects in this way?

Thanks, Ben

+1  A: 

I was helping a colleague with a similar problem last week. Our solution was to remove the direct association between a product and it's image. Instead, we provided a seperate query (not a lazy list) to load the image. This is because in our model the joins necessary to retrieve the image was too much of a performance hit.

We now have something like:

_myservice.GetProduct(id);
_myservice.GetImagesForProduct(id);

As far as editing the product, you don't necessarily need to do all the editing at once. Why not have one edit for product information, and another edit for images?

_myservice.Update(productId, parameters);
_myservice.AddImageToProduct(productId, image);
_myservice.RemoveImageFromProduct(productId, imageId);
Ryan
@Ryan - Thanks for the suggestion. We are going to continue with lazy loading for retrieving the complex objects will use specific save methods for saving child objects.
Ben