Showing posts with label extension. Show all posts
Showing posts with label extension. Show all posts

Tuesday, 15 March 2016

Dynamics AX7 Development 4–Overlay or extend

In this post I will look at the decision of overlaying or extending. In AX7 you can extend metadata by adding a field to a table or adding a control to a form, and extend business logic by defining event handlers. You can now write event handlers on several pre-defined events on tables, forms, form data sources, form controls, and others.
Below we are going to look at a basic example comparing Overlay with Extending (using event handler). As mentioned there are many forms of extensions but for simplicity we will focus on one (event handler).

Overlay

Overlaying is when you modify existing code by changing the system behaviour. For example lets look at overlaying the initValue method on the CustTable.
Right click on the object and click Customize. This will add it to your project.
2016-01-19_0729
You will see a little [c] to indicate it is a customised object.
2016-01-19_0735
I added a bit of code to the initValue method. Notice how it has changed colour to indicate it has been customised.
2016-01-19_0738

Extension (using events)

Extensions are used in AX7 much more and is much more flexible. Extensions allow you to leave the system behaviour but adding your piece to it. In some cases you have to overlay but try to avoid that if possible. As overlaying may seem simple but it will cost you on upgrades, hotfixes, maintaining code and merging code.
Lets look at the same example but now using extensions.
On the CustTable right click and Opern designer.
2016-01-19_1608
Right click and copy the vent handler method (this will copy into clipboard).
2016-01-19_1607
Now create a class. Paste whats in clipboard. It will paste the highlighted section below.
Add your code. The vent will now be triggered after the initValue method.
2016-01-19_1610
Warning: Code block to update languageId is for illustration only. Not runnable.
More detail on the wiki page:

Dynamics AX7 Development 4–Overlay or extend

In this post I will look at the decision of overlaying or extending. In AX7 you can extend metadata by adding a field to a table or adding a control to a form, and extend business logic by defining event handlers. You can now write event handlers on several pre-defined events on tables, forms, form data sources, form controls, and others.
Below we are going to look at a basic example comparing Overlay with Extending (using event handler). As mentioned there are many forms of extensions but for simplicity we will focus on one (event handler).

Overlay

Overlaying is when you modify existing code by changing the system behaviour. For example lets look at overlaying the initValue method on the CustTable.
Right click on the object and click Customize. This will add it to your project.
2016-01-19_0729
You will see a little [c] to indicate it is a customised object.
2016-01-19_0735
I added a bit of code to the initValue method. Notice how it has changed colour to indicate it has been customised.
2016-01-19_0738

Extension (using events)

Extensions are used in AX7 much more and is much more flexible. Extensions allow you to leave the system behaviour but adding your piece to it. In some cases you have to overlay but try to avoid that if possible. As overlaying may seem simple but it will cost you on upgrades, hotfixes, maintaining code and merging code.
Lets look at the same example but now using extensions.
On the CustTable right click and Opern designer.
2016-01-19_1608
Right click and copy the vent handler method (this will copy into clipboard).
2016-01-19_1607
Now create a class. Paste whats in clipboard. It will paste the highlighted section below.
Add your code. The vent will now be triggered after the initValue method.
2016-01-19_1610
Warning: Code block to update languageId is for illustration only. Not runnable.
More detail on the wiki page:

Dynamics AX7 Development 6–Adding Delegates for Customised Code in Higher Package

Dynamics AX is split into multiple models the main ones are as follows; Application suite, Application Foundation and Application Platform.
2016-01-20_1256
If you look at some of the debendencies you see how the organization of model affects the assemblies (how it is compiled).
In some cases you have to modify the Foundation or the Platform. The issue with that is you are unable to reference code sitting in a higher package (model). In short a higher model can take dependencies and access elements in a lower model but not the other way around.
To get around this limitation you have to use a delegate method. Below is an example of a delegate. The PrintMgmtDocType has a method to get the default report form.
2016-01-29_0612
See how the delegate method is called from the Foundation and uses an EventHanderResult to pass around the result.
2016-01-29_0610
The delegate method doesnt have to have any code. It is there to allow other methods to subscribe to it.
2016-01-29_0611
Now this is the calls in the Suite. Notice how to subscribe by adding the SubscribesTo attribute.
2016-01-29_0623
Refer to the Microsoft Wiki. It has some really good examples.

Dynamics AX7 Development 5–More on extensions (Table extension)

Previous post compared the difference between overlaying and extending. It is clear the benefits of extensions.
So to continue that discussion, in this post I will look at extensions by adding a field to a table.
Right click on a table you want to extend (ie you want to add a new field).
2016-01-20_0951
This will create a table extension SalesTable.Extension. See that in the project (right hand side in the Solution Explorer).
Open up the table design.
You can now drag an EDT or create it by right clicking. The bold items are what have been added to the extension. The greyed items are what is in the base.
2016-01-20_1005
Thats it. We have added a new field to the table without having to modify the base table.

Dynamics AX7 Development 5–More on extensions (Table extension)

Previous post compared the difference between overlaying and extending. It is clear the benefits of extensions.
So to continue that discussion, in this post I will look at extensions by adding a field to a table.
Right click on a table you want to extend (ie you want to add a new field).
2016-01-20_0951
This will create a table extension SalesTable.Extension. See that in the project (right hand side in the Solution Explorer).
Open up the table design.
You can now drag an EDT or create it by right clicking. The bold items are what have been added to the extension. The greyed items are what is in the base.
2016-01-20_1005
Thats it. We have added a new field to the table without having to modify the base table.

Dynamics AX7 Development 6–Adding Delegates for Customised Code in Higher Package

Dynamics AX is split into multiple models the main ones are as follows; Application suite, Application Foundation and Application Platform.
2016-01-20_1256
If you look at some of the debendencies you see how the organization of model affects the assemblies (how it is compiled).
In some cases you have to modify the Foundation or the Platform. The issue with that is you are unable to reference code sitting in a higher package (model). In short a higher model can take dependencies and access elements in a lower model but not the other way around.
To get around this limitation you have to use a delegate method. Below is an example of a delegate. The PrintMgmtDocType has a method to get the default report form.
2016-01-29_0612
See how the delegate method is called from the Foundation and uses an EventHanderResult to pass around the result.
2016-01-29_0610
The delegate method doesnt have to have any code. It is there to allow other methods to subscribe to it.
2016-01-29_0611
Now this is the calls in the Suite. Notice how to subscribe by adding the SubscribesTo attribute.
2016-01-29_0623
Refer to the Microsoft Wiki. It has some really good examples.

Tuesday, 1 March 2016

Extension methods AX7

The extension method feature lets you add extension methods to a target class by writing the methods in a separate extension class. The following rules apply:
    • The extension class must be static.
    • The name of the extension class must end with the ten-character suffix _Extension. However, there’s no restriction on the part of the name that precedes the suffix.
    • Every extension method in the extension class must be declared as public static.
    • The first parameter in every extension method is the type that the extension method extends. However, when the extension method is called, the caller must not pass in anything for the first parameter. Instead, the system automatically passes in the required object for the first parameter.
It’s perfectly valid to have private or protected static methods in an extension class. These are typically used for implementation details and are not exposed as extensions.
The example below illustrates an extension class holding a few extension methods:
public static class AtlInventLocation_Extension
{
public static InventLocation refillEnabled(
InventLocation _warehouse,
boolean _isRefillEnabled = true)
{
_warehouse.ReqRefill = _isRefillEnabled;
return _warehouse;
}

public static InventLocation save(InventLocation _warehouse)
{
_warehouse.write();
return _warehouse;
}
}

Extension methods AX7

The extension method feature lets you add extension methods to a target class by writing the methods in a separate extension class. The following rules apply:
    • The extension class must be static.
    • The name of the extension class must end with the ten-character suffix _Extension. However, there’s no restriction on the part of the name that precedes the suffix.
    • Every extension method in the extension class must be declared as public static.
    • The first parameter in every extension method is the type that the extension method extends. However, when the extension method is called, the caller must not pass in anything for the first parameter. Instead, the system automatically passes in the required object for the first parameter.
It’s perfectly valid to have private or protected static methods in an extension class. These are typically used for implementation details and are not exposed as extensions.
The example below illustrates an extension class holding a few extension methods:
public static class AtlInventLocation_Extension
{
  public static InventLocation refillEnabled(
    InventLocation _warehouse, 
    boolean _isRefillEnabled = true)
  {
    _warehouse.ReqRefill = _isRefillEnabled;
    return _warehouse;
  }

  public static InventLocation save(InventLocation _warehouse)
  {
    _warehouse.write();
    return _warehouse;
  }
}