Endiss
Endiss

Reputation: 699

QueryOver where generator Nhibernate

Hello i got some method that generating where statment programmatically how can i move where generation to other class method anyone can help ?

        public static List<MME.Objects.TypedLists.InvoiceList> GetList(List<MMPFramework.SearchParameter> parameter)
    {
        MME.Objects.Invoice Invoice = null;
        MME.Objects.Contractor Contractor = null;
        MME.Objects.Contract Contract = null;
        MME.Objects.TypedLists.InvoiceList invoiceList= null; 
        var t = MME.DAL.NhSessionHelper.GetCurrentSession().QueryOver<MME.Objects.Invoice>(() => Invoice);

        foreach (var searchParameter in parameter)
        {
            if(searchParameter.Expression == "Like")
            {
                t.Where(Restrictions.Like(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Eq")
            {
                t.Where(Restrictions.Eq(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Between")
            {
                t.Where(Restrictions.Between(searchParameter.PropertyName, searchParameter.ObjectValueLo,searchParameter.ObjectValueHi));
            }
            else if(searchParameter.Expression == "Gt")
            {
                t.Where(Restrictions.Gt(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else if (searchParameter.Expression == "Lt")
            {
                t.Where(Restrictions.Lt(searchParameter.PropertyName, searchParameter.ObjectValueLo));
            }
            else
            {
                //todo more
            }
            //t.Where(Restrictions.Eq(searchParameter.PropertyName, searchParameter.ObjectValue));

        }
        t.JoinQueryOver(() => Invoice.Contractor, () => Contractor, JoinType.LeftOuterJoin)

            .JoinQueryOver(() => Invoice.Contract, () => Contract, JoinType.LeftOuterJoin)
            .Select(Projections.Property(() => Invoice.Id).WithAlias(() => invoiceList.Id),
                    Projections.Property(() => Invoice.Number).WithAlias(() => invoiceList.InvoiceNumber),
                    Projections.Property(() => Contractor.Name).WithAlias(() => invoiceList.ContractorName),
                    Projections.Property(() => Contract.Number).WithAlias(() => invoiceList.ContractNumber)
            )
            .TransformUsing(Transformers.AliasToBean<MME.Objects.TypedLists.InvoiceList>());
        return t.List<MME.Objects.TypedLists.InvoiceList>().ToList();

    }

I've tried with this but it seems to not work.... Hope someone was doing something and can help me to handle with it.

    public class BaseList
{
    public object WhereGenerator(object ob)
    {
        QueryOver Ded = ob as QueryOver;
        return null;
    }
}

Upvotes: 0

Views: 306

Answers (1)

Firo
Firo

Reputation: 30813

foreach (var restriction in BaseList.Createrestrictions(parameter))
{
    t.Where(restriction);
}


public class BaseList
{
    public IEnumerable<AbstractCriterion> Createrestrictions(List<MMPFramework.SearchParameter> parameter)
    {
        return parameter.Select(ToCritieria);
    }

    private AbstractCriterion ToCritieria(SearchParameter searchParameter)
    {
        if(searchParameter.Expression == "Like")
        {
            return Restrictions.Like(searchParameter.PropertyName, searchParameter.ObjectValueLo);
        }
        else ...
    }
}

Upvotes: 1

Related Questions