I want to create a DDD repository that returns IQueryable Entities that match the Linq to SQL underlying classes, minus any relations. I can easily return Entities minus the relations with a Linq select new {field, field, ... } projection. How do I code the Repository Entity class? How would I return an object from the repository having the repository class not the Linq to SQL class and still fill it with multiple entities from the Linq selection? How would I reference this returned class in my ViewModel?
I am very new to this, thus the obvious mistakes. Am I missing the boat and should only return complete Entities from the repository, not a projection? I would still need to strip out the Linq to SQL relations before I send them from the repository. Am I totally off base? I really want to keep the IQueryable data type.
For example, my Linq to SQL code in my repository:
public class MiniProduct
{
public MiniProduct( string ProductNo, string Name, string Description, double Price)
{ this.ProductNo = ProductNo;
this.Name = Name;
this.Description = Description;
this.Price = Price;
}
}
public IQueryable<MiniProduct> GetProductsByCategory( string productCategory)
{
return ( from p in db.Products
from c in db.Categories
from pc in db.ProductCategories
where c.CategoryName == productCategory &&
c.CatID == pc.CatID &&
pc.ProductID == p.ProductID
select new { p.ProductNo, p.Name, p.Description, p.Price } );
// how to return IQueryable<MiniProduct> instead of IQueryable<AnonymousType>???
}
And in the View (trying to strongly type ViewModel) what would be my model data type and how to reference from the view?
<% Page Inherits="System.Web.Mvc.ViewPage<MyStore.Models.MiniProduct>" %>
Edit:
Cottsak empowered the code and made it work, so he earns the checkbox. However, Mark Seemann pointed out that this technique will cause side effects. He was right in that projecting or sub-setting your POCO is bad. After making the code work, I ended up making a ton more one off entity objects, which causes unnecessary complications. Ultimately I changed the code to reflect Mark's suggestions.
To add to Cottsak's suggestions: My repository return value was IQueryable. The page directive model reference type was
Inherits="System.Web.Mvc.ViewPage<IQueryable<MyStore.Models.MiniProduct>>"
The Model fields were accessed by:
Model.SingleOrDefault().ProductNo
Model.SingleOrDefault().Name
...
And this lead to a foreach (MyStore.Models.MiniProduct myproduct in Model) {}
Thank you both for the answers.