1

I'm thinking an enum might be the way to go, and depending on the action (create, delete) it replaces the characters? Then I'd have to store a variable for "--", "++", ">=" and ">" hrrmm. Any simple suggestions would be great, and I could use it as a future reference as to how I might tackle a problem like this!

public void ArrangeCategoriesOrderOnCreate(Category category)
{
    var tx = _session.BeginTransaction();
    var categories = _session.QueryOver<Category>()
        .Where(c => c.Parent.Id == category.Parent.Id && c.OrderInList >= category.OrderInList) // the >= category.OrderInList here
        .List<Category>();

    foreach(var item in categories)
    {
        item.OrderInList++; // The ++ here.
        _session.SaveOrUpdate(item);
    }
    tx.Commit();
}

public void ArrangeCategoriesOrderOnDelete(Category category)
{
    var tx = _session.BeginTransaction();
    var categories = _session.QueryOver<Category>()
        .Where(c => c.Parent.Id == category.Parent.Id && c.OrderInList > category.OrderInList) // the > category.OrderInList here
        .List<Category>();

    foreach(var item in categories)
    {
        item.OrderInList--; // the -- here.
        _session.SaveOrUpdate(item);
    }
    tx.Commit();
}

Edit 1

Could not determine member type from Invoke, Invoke(value(CumbriaMD.Infrastructure.DataServices.CategoryDataServices+<>c_DisplayClass8).orderComparer, c.OrderInList, value(CumbriaMD.Infrastructure.DataServices.CategoryDataServices+<>c_DisplayClass8).category.OrderInList), System.Linq.Expressions.InvocationExpression

Trace:

[Exception: Could not determine member type from Invoke, Invoke(value(CumbriaMD.Infrastructure.DataServices.CategoryDataServices+<>c_DisplayClass8).orderComparer, c.OrderInList, value(CumbriaMD.Infrastructure.DataServices.CategoryDataServices+<>c_DisplayClass8).category.OrderInList), System.Linq.Expressions.InvocationExpression] NHibernate.Impl.ExpressionProcessor.ProcessBooleanExpression(Expression expression) +1113 NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression) +114 NHibernate.Impl.ExpressionProcessor.ProcessAndExpression(BinaryExpression expression) +85 NHibernate.Impl.ExpressionProcessor.ProcessBinaryExpression(BinaryExpression expression) +124 NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression expression) +95 NHibernate.Impl.ExpressionProcessor.ProcessLambdaExpression(LambdaExpression expression) +52 NHibernate.Impl.ExpressionProcessor.ProcessExpression(Expression1 expression) +39 NHibernate.Criterion.QueryOver2.Add(Expression1 expression) +107 NHibernate.Criterion.QueryOver2.Where(Expression1 expression) +42 NHibernate.Criterion.QueryOver2.NHibernate.IQueryOver.Where(Expression`1 expression) +42

4

1 に答える 1

2

開始する簡単な方法の 1 つは、 for を使用して共通の機能をFunc分割し、ほとんどの重複を取り除くことです。

public void ArrangeCategoriesOrderOnCreate(Category category)
{
    ArrangeCategoriesOrderOnAction(category, (a,b) => a>=b, x => x + 1);
}

public void ArrangeCategoriesOrderOnDelete(Category category)
{
    ArrangeCategoriesOrderOnAction(category, (a,b) => a>b, x => x - 1);
}

public void ArrangeCategoriesOrderOnAction(Category category, 
                               Expression<Func<int, int, bool>> orderComparer,
                               Func<int, int> orderUpdateCalculator)
{
    var tx = _session.BeginTransaction();
    var categories = _session.QueryOver<Category>()
        .Where(c => c.Parent.Id == category.Parent.Id 
               && orderComparer(c.OrderInList, category.OrderInList))
        .List<Category>();

    foreach (var item in categories)
    {
        item.OrderInList = orderUpdateCalculator(item.OrderInList);
        _session.SaveOrUpdate(item);
    }
    tx.Commit();
}
于 2012-08-11T06:23:56.960 に答える