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