使用Sharp Lite Architecture在ASP.NET MVC 3中执行连接查询

时间:2012-02-09 19:46:51

标签: asp.net-mvc-3 entity-framework s#arp-architecture

我将使用mystore sharp lite架构示例。您可以在此处下载解决方案: https://github.com/codai/Sharp-Lite

因此有两个实体存在问题。产品。和ProductCategories。

产品如下所示:

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; }
}

请注意,其中包含ProductCategories列表。所以它确实有一个适用于它的ProductCategories列表。

这是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; }
}

我完全理解了查询,我用where语句做了简单的查询。例如,这是我在另一个程序中用于搜索客户名字的查询:

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

但我还不太了解加入。有人能告诉我光明吗?

1 个答案:

答案 0 :(得分:1)

首先,如果您不了解如何进行加入,您应该查阅一些文档,但请记住,这与Sharp Lite有很少(如果没有)。

Sharp Lite的代理商背后的基本思想是使用IQueryable的强大功能,以减少对基础数据访问(NH或EF最流行)的依赖,因此您基本上应该学习how joins work on NH(如果你正在使用NH)并开始使用它。另外,尝试用更复杂的结构建立一个更好的例子,这样你就可以真正做一些连接。

如果您需要在SharpLite上进行快速入门,请务必阅读the post on why Sharp lite existsthe other explaining how it's built。另外,我用演示项目制作了one myself to get people started

希望我能帮忙!