Question

I'll use the mystore sharp lite architecture example. You can download the solution here: https://github.com/codai/Sharp-Lite

So there are two entities in question. Products. And ProductCategories.

Products looks like this:

public class Product : Entity
{
    public Product() {
        Categories = new List<ProductCategory>();
    }

    [DomainSignature]
    [Required(ErrorMessage = "Name must be provided")]
    [StringLength(255, ErrorMessage = "Name must be 255 characters or fewer")]
    public virtual string Name { get; set; }

    /// <summary>
    /// Money is a component, not a separate entity; i.e., the Products table will have column 
    /// for the amount
    /// </summary>
    [DataType("Money")]
    public virtual Money Price { get; set; }

    /// <summary>
    /// many-to-many between Product and ProductCategory
    /// </summary>
    [Display(Name="Product Categories")]
    public virtual IList<ProductCategory> Categories { get; protected set; }
}

Notice it has the ProductCategories List in it. So it does have a list of ProductCategories that applies to it.

And here is ProductCategory:

public class ProductCategory : Entity
{
    public ProductCategory() {
        Children = new List<ProductCategory>();
        Products = new List<Product>();
    }

    [DomainSignature]
    [Required(ErrorMessage="Name must be provided")]
    [StringLength(255, ErrorMessage="Name must be 255 characters or fewer")]
    public virtual string Name { get; set; }

    /// <summary>
    /// many-to-one from child ProductCategory to parent ProductCategory
    /// </summary>
    [Display(Name="Parent Category")]
    public virtual ProductCategory Parent { get; set; }

    /// <summary>
    /// many-to-many between ProductCategory and Product
    /// </summary>
    public virtual IList<Product> Products { get; protected set; }

    /// <summary>
    /// one-to-many from parent ProductCategory to children ProductCategory
    /// </summary>
    public virtual IList<ProductCategory> Children { get; protected set; }
}

I understand queries enough that I have made simple ones with where statements. For example this is the query I used in another program to search for the first name of a customer:

 public static IQueryable<Customer> GetByFirstName(this IQueryable<Customer> customers, string name)
    {
        name = name.ToUpper();
        return
            customers.Where(c => c.BillingAddress.FirstName.ToUpper().Contains(name));
    }

But I don't quite understand joins yet. Can someone show me the light?

Was it helpful?

Solution

First of all, if you don't understand how to make a Join, you should probably consult some docs on it, but keep in mind, that this has very little (if nothing) to do with Sharp Lite.

The basic idea behind Sharp Lite's repsitories is to use the power of IQueryable in order to depend less on the underlying data access (NH or EF which are the most populars), so you should basically learn how joins work on NH (if you are using NH) and start working using that. Also, try to put up a better example with a more complex structure so you can really do some joins.

If you need a jumpstart on SharpLite, make sure to read the post on why Sharp lite exists and the other explaining how it's built. Also, I made one myself to get people started with the demo project.

Hope I can help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top