Dynamics AX
  RSS Feed  LinkedIn  Twitter
Want to turn you're data into a true asset? Ready to break free from the report factory?
Ready to gain true insights that are action focused for truly data informed decisions?
Want to do all of this across mutliple companies, instances of Dynamics and your other investments?
Hillstar Business Intelligence is the answer then! (www.HillstarBI.com)

Hillstar Business Intelligence for Microsoft Dynamics AX and NAV on Mobile, Desktop, Tablet


Let us prove to you how we can take the complexity out of the schema and truly enable users to answer the needed questions to run your business! Visit Hillstar Business Solutions at: www.HillstarBI.com

Wednesday, November 11, 2009

Reflection - Where and why

With Dynamics AX, the ability to make use of Reflection, or Reflective Programming, exist.

For those that don't quite understand what reflection is, please check out the following wikipedia link, which explains it well.:
Wikipedia.org Link



As described on the wikipdeia article.:
"In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. It is a particular kind of metaprogramming.

In many computer architectures, program instructions are stored as data - hence the distinction between instruction and data is merely a matter of how the information is treated by the computer and programming language. Normally, instructions are executed and data is processed; however, in some languages, programs can also treat instructions as data and therefore make reflective modifications. Reflection is most commonly used in high-level virtual machine programming languages."


So the idea of reflection, or meta-programming, has actually been around for a long while. Since the mid 1980's. And the ability to perform reflective programming exists in many langauges, including C#, Java, and X++.

There are plenty of examples actually of reflection, that are common use with Dynamics AX. These may not be considered full reflective based programming, but aspects of this exists all through the common code base of Dynamics AX.

Take for example the Common object. All tables actually extend from this Common object.

As it's base, you have then the ability to send a table of unknown type, and operate it on a method that does not know until runtime, what the Common object actually represents.

So there eixsts, for the Common object, the ability for it to describe itself to later executing code, and therefore can then be acted upon at runtime.

For example.:

public void SomeMethod(Common Rec)
{
Table1 tbl1var;
Table2 tbl2var;
;

switch(tableid2name(rec.tableid))
{
case "Table1" :
tbl1var = Rec;
break;

case "Table2" :
tbl2var = Rec;
break;
}

// Do some code here on the Tables...
}


In the above example we see a somewhat common approach for having a method find out what kind of table that is being passed in, and then operate on that table based on the table name.

Now if you expand this further, there are other classes, and objects that exists inside Dynamics AX that help us really implement Reflection.

These classes include.:

  • SysDictTable

  • SysDictField

  • SysDictClass

  • Etc.



You can browse the AOT, Classes and go to SysDict* and see what all exists there.

The reason I bring reflection up, is I think it's not a well understood topic, and one that if more people understood could really benefit from.

I have done some development work before, that making use of reflection could read an entire SQL Server databse, read in datatypes, field lengths, tables names, and use that to create tables, extended data types, and import the data into these new tables, with a single object.

There is also possibilities for making use of reflection for say you want to have a class framework, and have a class tied to a given customer to operate some kind of acknowledgement to that customer.

You don't however want to have a mega object that operates all this for you, and you want flexibility. What to do?

Well you can make use of reflection, to create a framework that will execute code, not knowing for sure what it's executing actually. Only knowing that the given class it is executing has a specific method. And you made sure of that this method exists, because you make use of reflection to see if it actually does exists.

This can be done with making use of the SysDictClass, and the Object, Object. You could also get fancy, if need be, and create code at run time, compile that code and run time, execute that code at run time, and have that code return it's output to operate on. Again, All at run time!

To see a decent example of this, check the following out.: Generating and running code RUNTIME

Also, a great resource on this very matter, with X++ as the focus is the SysDictCoder blog, found here.: SysDictCoder Blog

The following are just some of the great highlights that have been from this blog, and I myself have made use of most or not all of these in my lifetime of AX.:


Those are just some recent few actually, and there is a lot more.

The whole idea of reflective development, making use of reflection is smart coding. Sometimes people look at it as code that makes code. If I can create a single object that saves me time from coding 200 objects, that that is the better way.

And that last example, true example. Not a made up number. I have done this before, specifically relating to data imports.

That's all for now, I hope this is enough to get you thinking about reflection, some of it's common uses in X++, and maybe how it can help you solve that next need.

Till next time!




"Visit the Dynamics AX Community Page today!"


Labels: , , , , , , , , , , , ,

4 Comments:

Blogger tommy.skaue said...

One of the great things with reflection in my opinion is the ability to make a "plug-in architecture". I've been working on a project that lets me drop new code into the application, have a routine search and find this code and add it's reference to a table. Some other code initialize the new classes using reflection, making the new functionality available.

Reflection is great stuff!

2:32 AM  
Anonymous Anonymous said...

Nice blog for reflection..but can anyone please tell me why we need the reflection concept..? i am new in axapta and C# so please tell me if you want to change structure put the "if" condition ..? or "generate the code at run time" i am not getting the line because anyhow you have to write the code and complie the code...then what is the need of reflection ..? please forgive if sound kiddish.
AT

3:46 AM  
Anonymous Anonymous said...

With reflection, you can make your own tools to audit code or to generate code and this way automate many things that today probably you programme.

//Ivan

4:34 PM  
Blogger tommy.skaue said...

Reflections is one of the reasons intellisense in Visual Studio works as good as it does. Instead of showing intellisense based on guesswork, reflections allows for a type safe list of suggestions.

12:29 PM  

Post a Comment

<< Home


Copyright 2005-2011, J. Brandon George - All rights Reserved