This project is read-only.

Tips and Tricks

POM object properties:

The properties in a POM object are not standard .NET properties, in that they call the Preactor.ReadField...() method internally.
This means you need to be careful not to have multiple calls to the same property in one method.

i.e. This will have a performance hit: (because multiple calls to operation.RecordNumber are made. since this value does not change while we are in the method body we can save this value to a variable.)

public static void AddToSecondaryConstraint<T>(this T operation, string secondaryConstraintName, ConstraintUsage constraintUsage, double quantity, int stepSize) where T : PreactorRecord, IOperation
{ 
    var pom = operation.PreactorObjectModel; 

    var size = pom.Preactor.MatrixFieldSize(operation.FormatNumber, "Secondary Constraints", operation.RecordNumber); 
    var newIndex = size.X + 1; 

    pom.Preactor.SetAutoListSize(operation.FormatNumber, "Secondary Constraints", operation.RecordNumber, newIndex); 

    pom.Preactor.WriteListField(operation.FormatNumber, "Secondary Constraints", operation.RecordNumber, secondaryConstraintName, newIndex); 
    pom.Preactor.WriteListField(operation.FormatNumber, "Constraint Usage", operation.RecordNumber, constraintUsage.Value, newIndex); 
    pom.Preactor.WriteListField(operation.FormatNumber, "Constraint Step Size", operation.RecordNumber, stepSize, newIndex); 
    pom.Preactor.WriteListField(operation.FormatNumber, "Constraint Quantity", operation.RecordNumber, quantity, newIndex); 
}

This is Better: (Note the "ordersFormatNumber" and "recordNumber" variables at the beginning of the method.)

public static void AddToSecondaryConstraint<T>(this T operation, string secondaryConstraintName, ConstraintUsage constraintUsage, double quantity, int stepSize) where T : PreactorRecord, IOperation
{ 
    var pom = operation.PreactorObjectModel; 
    var ordersFormatNumber = operation.FormatNumber; 
    var recordNumber = operation.RecordNumber; 

    var size = pom.Preactor.MatrixFieldSize(ordersFormatNumber, "Secondary Constraints", recordNumber); 
    var newIndex = size.X + 1; 

    pom.Preactor.SetAutoListSize(ordersFormatNumber, "Secondary Constraints", recordNumber, newIndex); 

    pom.Preactor.WriteListField(ordersFormatNumber, "Secondary Constraints", recordNumber, secondaryConstraintName, newIndex); 
    pom.Preactor.WriteListField(ordersFormatNumber, "Constraint Usage", recordNumber, constraintUsage.Value, newIndex); 
    pom.Preactor.WriteListField(ordersFormatNumber, "Constraint Step Size", recordNumber, stepSize, newIndex); 
    pom.Preactor.WriteListField(ordersFormatNumber, "Constraint Quantity", recordNumber, quantity, newIndex); 
}


The same can be observed when using LINQ.

If you want to make multiple calls like this in different methods:

Pom.GetAllOperations().Where( op => op.OrderNumber == "ABC")

You will be better off creating a Dictionary that stores all the OrderNumbers as a key and then get the operations from the Dicationary, like this:

var OperationsGroupedByOrderNumber = Pom.GetAllOperations().GroupBy( op => op.OrderNumber).ToDictionary(x => x.Key, x => x.ToList()));

var abcOps = OperationsGroupedByOrderNumber["ABC"];

You can save this dictionary as a static property in your CustomActions class and reuse it as often as you like.

NOTE: In the first LINQ example a call to Preactor.ReadField...() would be made every time the op.OrderNumber property value needed to be determined, which has a significant speed reduction.

Last edited Sep 12, 2012 at 2:46 AM by bvsms, version 2

Comments

No comments yet.