mardi 21 juin 2016

Is Factory pattern have dependent value C#?

Today I create a factory for operator based Expression. but when I create my factory it have some dependency on comparison value.

Here is my code

public LambdaExpression GetPredicate(FieldType fieldType, GigFilterOption value)
    {
        switch (fieldType)
        {
            case FieldType.PriceNumeric :
                return new OperatorFactory( Convert.ToDecimal(value.LowerLimit), Convert.ToDecimal(value.UpperLimit)).GetOperator((Operator)value.Operator).Apply<YayNinja.DAL.Task>("Price");
                break;
            case FieldType.TimePeriod :
                return new OperatorFactory(Convert.ToDecimal(value.LowerLimit), Convert.ToDecimal(value.UpperLimit)).GetOperator((Operator)value.Operator).Apply<YayNinja.DAL.Task>("Duration");
                break;
            case FieldType.CheckBox:
            case FieldType.Radio:
                return new OperatorFactory(value.LowerLimit, value.UpperLimit).GetOperator((Operator)value.Operator).Apply<TaskField>("Value");
                break;
            default :
                throw new NotImplementedException();
        };
    }

Here is My Operator factory.

public class OperatorFactory : IOperatoFactory { private object lowerLimit; private object upperLimit;

    public OperatorFactory(object _lowerLimit, object _upperLimit) //**here you see dependency of value that i need for comparsion.**
    {
        lowerLimit = _lowerLimit;
        upperLimit = _upperLimit;
    }
    public IOperatorPredicate GetOperator(Operator @operator)
    {
        IOperatorPredicate operatorPredicate = null;
        switch (@operator)
        {
            case Operator.Equal:
                operatorPredicate = new EqualToPredicate(lowerLimit);
                break;
            case Operator.GreaterThan:
                operatorPredicate = new GreaterThanPredicate(lowerLimit);
                break;
            case Operator.GreaterThanOrEqual:
                operatorPredicate = new GreaterThanEqualPerdicate(lowerLimit);
                break;
            case Operator.LessThan:
                operatorPredicate = new LessThanPredicate(lowerLimit);
                break;
            case Operator.LessThanOrEqual:
                operatorPredicate = new LessThanEqualPredicate(lowerLimit);
                break;
            case Operator.Between:
                operatorPredicate = new BetweenPredicate(lowerLimit,upperLimit);
                break;
            default: 
                throw new NotImplementedException();
        }
        return operatorPredicate;
    }
}

I m sure that factory pattern is creator pattern it only create object without any dependency.

Please guide me on this topic

Aucun commentaire:

Enregistrer un commentaire