1

我用 NHibernate 创建了这个查询:

public IList<Category> GetCategoriesByUsername(string username)
{
    Category cAlias = null;
    User uAlias = null;
    Keyword kAlias = null;

    var categories = SessionFactory.GetCurrentSession().QueryOver<Category>(() => cAlias)
    .JoinAlias(() => cAlias.User, () => uAlias)
    .Where(() => uAlias.Username == username)
    .Future<Category>();

    var keywords = SessionFactory.GetCurrentSession().QueryOver<Keyword>(() => kAlias)
    .JoinAlias(c => c.Category, () => cAlias)
    .Where(() => cAlias.Id == kAlias.Category.Id)
    .Future<Keyword>();

    IList<Category> list = (List<Category>)categories.ToList();

    return list;
}

这很好用,并给了我一个类别列表,其中每个类别都有自己的关键字。在我的服务层中,我尝试将其转换为带有 Automapper 的 ViewModel,它可以正常工作但不如预期。对于类别列表中的每个关键字,它都会创建一个新查询 (N+1)。它不使用列表中每个类别中已填充的关键字。

这些是我的模型和视图模型:

public class Category
{
    public virtual Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Keyword> Keywords { get; set; }
    public virtual User User { get; set; }
}

public class CategoryView
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IList<KeywordSummaryView> Keywords { get; set; }
}

public class Keyword
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual Category Category { get; set; }
}

public class KeywordSummaryView
{
    public int Id { get; set; }
    public string Name { get; set; }
}

我的映射:

public class AutoMapperBootStrapper
{
    public static void ConfigureAutoMapper()
    {
        Mapper.CreateMap<Category, CategoryView>();
        Mapper.CreateMap<Keyword, KeywordSummaryView>();
    }
}

public static class CategoryMap
{
    public static IList<CategoryView> ConvertToCategoryView(this IList<Category> category)
    {
        return Mapper.Map<IList<Category>, IList<CategoryView>>(category);
    }
}

从模型到视图模型:

IList<Category> categories = _categoryRepository.GetCategoriesByUsername(request.Username);

response.Categories = categories.ConvertToCategoryView();

它不使用列表中每个类别中已填充的关键字,而是为每个关键字 (N+1) 创建一个新查询。有什么我做错了吗?

4

1 回答 1

1

我想这两个都应该防止 N+1 选择

public IList<Category> GetCategoriesByUsername(string username)
{
    User uAlias = null;

    var categories = SessionFactory.GetCurrentSession().QueryOver<Category>(() => cAlias)
    .Fetch(x => x.Keywords ).Eager
    .JoinAlias(() => cAlias.User, () => uAlias)
    .Where(() => uAlias.Username == username);
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Category>();

    return categories ;
}


public IList<Category> GetCategoriesByUsername(string username)
{
    User uAlias = null;
    Keyword kAlias = null;

    var categories = SessionFactory.GetCurrentSession().QueryOver<Category>(() => cAlias)
    .JoinAlias(() => cAlias.User, () => uAlias)
    .JoinAlias(x => x.Keywords , () => kAlias, JoinType.LeftOuterJoin)
    .Where(() => uAlias.Username == username);
    .TransformUsing(Transformers.DistinctRootEntity)
    .List<Category>();

    return categories;
}

希望这会有所帮助

于 2013-02-27T14:31:10.737 回答