开发者

Saving complex domain objects mapped using linq to sql

开发者 https://www.devze.com 2022-12-23 16:22 出处:网络
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:

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


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号