为什么Automapper和NHibernate导致N + 1?

时间:2013-02-27 14:00:20

标签: c# nhibernate automapper

我用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)。它不会使用列表中每个类别中已填充的关键字。

这些是我的模型和ViewModel:

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

从Model到ViewModel:

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

response.Categories = categories.ConvertToCategoryView();

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

1 个答案:

答案 0 :(得分: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;
}

希望这会有所帮助