Split construct method by models. Part1.
Using construct method is a common technique in AX, for example SalesFormLetter::construct(...)
Corresponding child class of SalesFormLetter class is constructed based on DocumentStatus parameter.
This is good.
However let’s try to implement one simple task.
Step 1.
We need to print message BackOrder is #SalesId #CustomerName for all sales orders.Implementation.
Create a new class
class TestBaseClass
{
SalesTable salesTable;
}
public SalesTable parmSalesTable(SalesTable _salesTable = salesTable)
return salesTable;
} public void showInfo()
{
info(strFmt("Backorder is %1 %2", this.parmSalesTable().SalesId, this.parmSalesTable().customerName()));
}
{
public static TestBaseClass construct() { return new TestBaseClass(); }and job for testing this class
Result isstatic void TestBaseClass1(Args _args)
{
SalesTable salesTable; // cursor
TestBaseClass baseClass; setPrefix("SalesInfo1");
while select salesTable
baseClass = TestBaseClass::construct(salesTable.SalesStatus);
baseClass.parmSalesTable(salesTable);
baseClass.showInfo();
}
info("done");
}
Step 2.
Consultant has changed the requirement to print Invoice if SalesStatus== Invoice. - ok - let's implement the change.Create a new class
class TestBaseClass01_Model01 extends TestBaseClass { }overwrite the method
public void showInfo() { info(strFmt("Invoiced is %1 %2", this.parmSalesTable().SalesId, this.parmSalesTable().customerName())); }
and change construct method of TestBaseClass
public static TestBaseClass construct(SalesStatus _salesStatus) { TestBaseClass testBaseClass; switch (_salesStatus) { case SalesStatus::Invoiced: testBaseClass = new TestBaseClass01_Model01(); break; default : testBaseClass = new TestBaseClass(); return testBaseClass; }
Update job and run it
OK - we have done this!
All the changes were done in one layer and one model (for example layer cus, model A01).
Step 3.
Our consultant wants to create another modification in cus layer, but in model A02. The goal of the modification is to print Delivered for sales order with SalesStatus==Delivered. Why do we need to implement it in different model (we ask our consultant)?Because we have many clients. Some clients want to get additional information, some - don't. Base logic is in one model and additional logic is in another model which allows us to have only one application and to develop without any problem in the future.
Ok, We have a new task!!!
Excellent. Let's do it!
*Note - if you create an object in wrong model - don't worry, you can always change current model of the object (method, event handler ...) - right click on the object and select - Move to model ...
Create a new model and select it as current. In new model create a new class
class TestBaseClass02_Model02 extends TestBaseClass { }
overload method
public void showInfo() { info(strFmt("Delivered is %1 %2", this.parmSalesTable().SalesId, this.parmSalesTable().customerName())); }
than slightly modify method TestBaseClass::construct()
public static TestBaseClass construct(SalesStatus _salesStatus)
{
TestBaseClass testBaseClass;
switch (_salesStatus)
{
case SalesStatus::Invoiced:
testBaseClass = new TestBaseClass01_Model01();
break;
case SalesStatus::Delivered:
testBaseClass = new TestBaseClass02_Model02();
break;
default :
testBaseClass = new TestBaseClass();
}
return testBaseClass;
}
and run our job without any modificationEverything is good and we can summarize our results.
1. We implemented the requirements - good.
2. We created correct models by AX standard - good.
3. We have one model A01 for one type of customers - good.
4. We have one model A02 - this model allows us to extend logic for other customers. - good.
Everything is almost good!
Let's see our solution split by models
TestBaseClass - in model A01
TestBaseClass01_Model01 in model A01
TestBaseClass02_Model02 in model A02
If we try to install model A01 to another application - we will get compile error, because system could not find class TestBaseClass02_Model02 - this is not good.
If we move TestBaseClass::construct to A02 model and try to install A01 to another application - we will lose construct method - this is not good.
How we can re-factoring our small solution to resolve this problem?
In AX 2012 developers have interesting mechanism - delegates. Let's try to use it to resolve the conflict.
Ok, We have a new Interesting task!
Step 4.
4.1 Change TestBaseClass::constructpublic static TestBaseClass construct() { return new TestBaseClass(); }
4.2. create a new method TestBaseClass. findDetermination() (model A01)
public boolean findDetermination(SalesTable _salesTable) { boolean ret; if (_salesTable.SalesStatus == SalesStatus::Backorder) { ret = true; this.parmSalesTable(_salesTable); } return ret; }
4.3. overwrite method \Classes\TestBaseClass01_Model01\findDetermination (model A01)
public boolean findDetermination(SalesTable _salesTable) { boolean ret; if (_salesTable.SalesStatus == SalesStatus::Invoiced) { ret = true; this.parmSalesTable(_salesTable); } return ret; }
4.4. overwrite method \Classes\TestBaseClass02_Model02\findDetermination (model A02)
public boolean findDetermination(SalesTable _salesTable) { boolean ret; if (_salesTable.SalesStatus == SalesStatus::Delivered) { ret = true; this.parmSalesTable(_salesTable); } return ret; }
4.5. Let's create a new class - this class helps us to resolve the problems (model 01)
// Class fabric. This class can generate right class updater class TestBaseClassFabricaConstructor { SalesTable salesTable; // salesTable for updating List potentialConstructors; // save all potential updaters }
public void new() { potentialConstructors = new List(Types::Class) }
public SalesTable parmSalesTable(SalesTable _salesTable = salesTable) { salesTable = _salesTable; return salesTable; }
// add updater to some list to save public void subscribe(TestBaseClass _subscriber) { potentialConstructors.addEnd(_subscriber); } public static TestBaseClassFabricaConstructor construct() { return new TestBaseClassFabricaConstructor(); }
and create a new blank but very important method
delegate void constructorCollect(TestBaseClassFabricaConstructor _fabricaCollector) { }
plus another method to call delegate
// call all subscribers and get subscribers list. Subscriber add itselt manualy to our collections public void collect(TestBaseClassFabricaConstructor _fabricaCollector = this) { this.constructorCollect(_fabricaCollector); }
It look's good, but not finished. We will return to this class a little bit later.
4.6. create a new method \Classes\TestBaseClass\subscribe (model A01)
public static void subscribe(TestBaseClassFabricaConstructor _fabricaConstructor) { TestBaseClass baseClass = TestBaseClass::construct(); _fabricaConstructor.subscribe(baseClass); }
4.7. Create a new method \Classes\TestBaseClass01_Model01\subscribe (model A01)
public static void subscribe(TestBaseClassFabricaConstructor _fabricaConstructor) { TestBaseClass01_Model01 baseClass = TestBaseClass01_Model01::construct(); _fabricaConstructor.subscribe(baseClass); }
4.8. Create anew method \Classes\TestBaseClass02_Model02\subscribe (model A02)
public static void subscribe(TestBaseClassFabricaConstructor _fabricaConstructor) {
TestBaseClass02_Model02 baseClass = TestBaseClass02_Model02::construct(); _fabricaConstructor.subscribe(baseClass); }
4.9. Return to class TestBaseClassFabricaConstructor.
method constructorCollect - right click "New Event Handler Subscription"
and do like shown bellow 3 times - for all three classes
Each event handler can be placed in own model.
TestBaseClass and TestBaseClass01_Model01 is placed in model A01, TestBaseClass02_Model02 is placed in A02 model.
4.10 - create the last method in TestBaseClassFabricaConstructor.softConstruct
// determine what is class want be updater for salesTable public TestBaseClass softConstruct(SalesTable _salesTable = this.parmSalesTable()) { ListEnumerator le = potentialConstructors.getEnumerator(); TestBaseClass ret; while (le.moveNext()) { ret = le.current(); if (ret.findDetermination(_salesTable)) // check if updater want update SalesTable - welcome { break; } } return ret; }
Very good. Now we need to run it!
Create a new job
// TODO - Example for everyone. How me can split method with multimodel declarations static void TestBaseClassFabricaConstructorJob(Args _args) { TestBaseClassFabricaConstructor fabricaConstructor; // constructor for our class updater SalesTable salesTable; // cursor TestBaseClass baseClass; // abstract updater setPrefix("SalesInfo"); while select salesTable { fabricaConstructor = TestBaseClassFabricaConstructor::construct(); // init fabricaConstructor.parmSalesTable(salesTable); // init fabricaConstructor.collect(); // call method collect. This method call all subscribers and subscribers add itself to update candidate list baseClass = fabricaConstructor.softConstruct(); // loop by all updaters and find the best updater if (baseClass) // if updater find - show info about it { baseClass.showInfo(); } } info("done"); // If we need to create new updater // 1. Create new class and extends it from base class (TestBaseBaseClass_Model01) // 2. Create static method subscribe // 3. Create subscription to \Classes\TestBaseClassFabricaConstructor\constructorCollect // 4. Create method findDetermination // 5. Create method showInfo // // }
and results
Well done!
Let's see what will happen if we decide to publish model A01 for clients - it will work - because we move model with out one event handler.Next time we will try to resolve our task with another approach.
Have a nice day!
Looks good!
ОтветитьУдалитьDid you take a look at the extension framework? I think you could achieve the same result with that using Attributes.
ОтветитьУдалитьWhen I wrote this article, I not look at "your" link, but have some plans to write Part3 with using sysAttribute class. Now - you shown great blog with detailed description how we can use another way to do same ... and my Part3 not need anymore. Big thanks for link.
Удалить