Learning Dynamics NAV Patterns - Sample Chapter
Learning Dynamics NAV Patterns - Sample Chapter
$ 34.99 US
22.99 UK
P U B L I S H I N G
Sa
pl
e
P r o f e s s i o n a l
E x p e r t i s e
D i s t i l l e d
Learning Dynamics
NAV Patterns
ee
Mark Brummel
P U B L I S H I N G
In 2010, he started a think tank called Partner Ready Software together with four
other Dynamics NAV experts. Partner Ready Software brings fresh ideas about
designing applications in NAV, and creates awareness of applying Design Patterns
in creating repeatable solutions.
Mark is an associate in the Liberty Grove Software network, a member of the
NAVUG advisory board, the co-founder of the Dutch Dynamics Community,
the vice-president of the Association of Dynamics Professionals, and an advisor
to Dynamics HUB.
Mark has undertaken a special project and performance tuning of the Dynamics
NAV product on SQL Server. As a unique specialist, he has done breakthrough
research into improving the performance of Dynamics NAV on the SQL Server.
On http://nav-skills.com/, Mark maintains a blog. This blog contains a wide
range of articles about both the Microsoft Dynamics NAV and the SQL Server
product. He is also a frequent speaker at Microsoft events, and publishes articles
on Pulse for LinkedIn.
Since 2006, Mark has been rewarded by Microsoft with the Most Valuable Professional
award for his contribution to the online and offline communities. He has received this
award ten times.
Mark is married and a father of four, and lives in a small town in The Netherlands.
Preface
Hi, and welcome to the world of Design Patterns. This is how I open my short
introductory video about this subject that I made for Microsoft. You can find it on
my YouTube page together with more than 15 hours of Patterns content.
This book is about Patterns and clean coding practices. It is about creating software
that users will recognize as Microsoft Dynamics NAV, that other developers can
maintain, and that is as easy to merge and upgrade as possible with the technology
we have today.
Microsoft Dynamics NAV, and before that, Navision Software, always had a specific
footprint. I often call it Navision DNA. It is what makes our beloved product unique
and successful.
But what is "it"? What is this Navision DNA? This is the question that we will try to
answer in this book. You will find that many answers can be found in the product,
dating back to 30 years ago. We will also find some answers in the object-oriented
programming best practices and the clean coding concepts.
Together, these form a recipe for writing good software in C/ALour programming
language. Despite the fact that we are not object oriented, we can learn from other
initiatives around Patterns and practices.
After reading this book, I hope that you have some fresh ideas, or maybe even
recognition that you're already doing a lot of these things in the correct way.
Preface
[1]
Chapter 1
Non-object-oriented patterns
The base design patterns from object-oriented programming are pretty much
targeted at structuring applications, running as Windows services or applications.
With Microsoft Dynamics NAV, we get all of that out of the box. We don't have to
think of how to get the data from the database, or how to handle concurrency.
For a better understanding of object-oriented patterns and why they
don't apply to Dynamics NAV and C/AL, please read and watch
the information at http://www.newthinktank.com/2012/08/
design-patterns-video-tutorial/.
[3]
Architectural patterns
When a pattern describes table definitions or a methodology to move data from
one set of tables to another, we refer to it as an Architectural Pattern.
By learning Architectural patterns, we understand how the application's large
building blocks are constructed. There are only a dozen of these patterns that are
most commonly used. Examples are Master Data, Journals, and Rules tables.
This does not mean that any other way of designing tables and moving data is
wrong by definition. It might just not be a common way of doing it, making
your code harder to understand for others, and less easy to maintain.
Design patterns
Any pattern that is not related to the table definitions, or moving data in the
database, is a Design Pattern. There are dozens of patterns, and in this book we'll
only focus on the ones that are most commonly used, such as number series and
entity state.
Structuring code
Even if you follow the patterns in this book step-by-step, your solution will be hard
to maintain if you don't bring structure to your code. There are standard ways and
best practices for doing this. You can find a lot of them online, mostly related to
object-oriented programming, but also outside of object-oriented programming.
We can translate these best practices so that they can work with C/AL. This will
help us implement best practices in Dynamics NAV, which are widely accepted in
computer science.
[4]
Chapter 1
Anti-patterns
Where there are best practices, there can also be ways that are proven to be less
productive. These are referred to as anti-patterns. Anti-patterns are bound to context,
which means that a good pattern applied to the wrong problem can be an anti-pattern.
Code cloning
A typical example of an anti-pattern in computer programming is code cloning. This
means copying and pasting similar code in your application rather than normalizing
it into classes and functions. Code cloning is applied in Dynamics NAV in large
proportions.
When working with C/AL, being able to copy a part of the application and making
it specific to certain needs is considered powerful.
This does not yet make code cloning a good thing. We will explain good and bad
examples of code cloning, and help you with a set of criteria about when cloning
can be applied, and when it should be avoided.
There are many good and bad examples available in the standard application.
We will discuss them in Chapter 6, Anti-patterns and Handling Legacy Code.
Legacy code
The Microsoft Dynamics NAV application code started being developed in 1980s,
and grew organically.
Despite the fact that the original architect applied many patterns, we still know today
from the very early days we can safely say that many parts of the application are not
optimized in a way they could be if one would have the opportunity to start over.
[5]
Handling legacy code is highly important for the lifecycle of your application.
Thousands of companies have been using Dynamics NAV for more than two
decades, upgrading their solution every few years to the latest version.
To provide this upgrade opportunity, one should be careful when redesigning
the application.
This makes it even more important to start with the correct patterns
from the beginning.
Especially when dealing with limited resources, a situation that most Dynamics NAV
development centers face, we want to spend as much time as possible in making new
features instead of fixing issues or rewriting existing code.
We will discuss examples of how the legacy code in Microsoft Dynamics NAV
was preserved, and how and when it was updated when necessary.
Upgradability
Microsoft Dynamics NAV has a great track record of being upgradable from one
version to the next. It is possible to upgrade all the way from the earliest MS-DOS
versions of the product to the latest version running on Azure in the cloud.
This requires the application architects to be very careful in making decisions.
Changes made to the application may live for many years or even decades.
When upgrading to newer versions, there are two big challenges.
Code Upgrade: each time Microsoft ships a new version of the product,
we have to see if the changes that we made to their code still function as
intended. This requires not only merging code and testing of the application,
but also doing a fit/gap analysis each time new functionality is added to the
base application.
Data Upgrade: if there are changes in the way the data is stored in the table
objects, we have to provide software programs that convert the data from the
old structure to the new structure. This is referred to as the Upgrade Toolkit
for Microsoft Dynamics NAV. You will learn about this in Chapter 6,
Anti-patterns and Handling Legacy Code.
[6]
Chapter 1
However, this best practice of upgrading has shifted dramatically with the
introduction of a connected world, internet, and cloud. There is an increasing
demand for being up to date with application software, and always being current.
New technologies are introduced in a higher frequency than ever before, and this
has impacted the way Microsoft ships the Dynamics NAV product.
We changed to yearly release cycles and frequent rollups, which often contained
new features, and new technology. Instead of upgrading every few years, there
is a requirement to be (almost) always current on version, as illustrated in the
next image:
[7]
Delta files
Another method to reduce the costs of shipping and merging software is using the
delta files, which were introduced in 2014 by Microsoft.
Delta files are based on flat text definitions of Dynamics NAV objects, and are
handled by PowerShell commandlets. The algorithms can create and apply text
files on all the versions of Dynamics NAVfrom 2009 and forward.
The Delta file describes, as the name implies, the delta between two objects in such
a way that they can be applied to other systems quite easily. This saves time and
money in merging the software.
Repeatability
In a cloud-first mobile-first world, repeatability becomes increasingly important.
Where barely 20 years ago consultants were driving around in their cars to
customers to install software and explain its use, there is an increasing trend towards
click-try-buy ERP systems.
In this ecosystem, intuitive use is very important. Microsoft has invested in a user
interface that makes it easier to use Dynamics NAV from a metadata perspective.
However, to be really more user friendly, the metadata (UI) needs to be optimized too.
[8]
Chapter 1
Technology changes
Design patterns can be made obsolete due to technology changes. Some languages
have more built-in capabilities than others.
For example, C# has overloading, where the same function has different arguments.
This is something that Dynamics NAV does not have. Therefore, we have a design
pattern called the Argument table. This would be obsolete if we could overrule a
function from the design time, or based on business logic.
The patterns in this book have been tested and documented for Microsoft Dynamics
NAV 2015, but typically work in older versions of the product too.
Some of the design patterns in Dynamics NAV go as far back as the MS-DOS version
of the product.
[9]
This code pattern started its life in the MS-DOS days, and just stayed in the
application without being technically required. Developers apply it to their own
objects just because it exists in many places in the standard application. We'll
discuss alternative approaches in this book.
Other examples are patterns based on Query objects, and patterns for Activity Pages.
Neither of these existed in the older versions, hence there was no requirement for
Design Patterns.
Sometimes, we can reuse the existing patterns for new elements. The Activity Page
uses the Singleton Pattern that we typically see for the Setup data. This adds new
functional implementations for patterns, and the pattern documentation needs to
be updated.
Developing SolutionsWriting software can, in many ways, be compared to building
houses, or even building entire villages or cities. We need to plan ahead, but we also
need to adjust to time. Things that we thought were brilliant in the 1990's are now
considered old. Also with software, we need to maintain, rebuild, and rearrange
as time goes by.
[ 10 ]
Chapter 1
Metadata
Much of the program's behavior is determined by metadata, which is interpreted
at runtime.
The metadata is a combination of elements, methods, and properties that gives the
program its unique behavior. This also limits the possibilities that developers have
using the application that enforces a certain level of simplicity.
Understanding the Metadata and base application is core for achieving success in
developing your own application. The patterns and best practices in this book will
help you with this.
Building blocks
To develop solutions in Microsoft Dynamics NAV, we have seven main building
blocks. These blocks determine the behavior of our application together with the
programming code.
The following screenshot shows the Object Designer that allows the development
access to the main building blocks:
Table
The Table object allows us to define how the data is stored in the database, as well
as in business logic.
The only database option for Microsoft Dynamics NAV is Microsoft SQL Server.
The SQL Table definitions are automatically created, based on the Table object in
Dynamics NAV.
All the business logic is handled by the Dynamics NAV runtime. The SQL Server
options, such as foreign keys, triggers, or stored procedures are not used.
Unlike many other applications, Dynamics NAV is not fully normalized, given a few
exceptions. Tables are bound one-to-one with the user interface. This adds a certain
level of simplicity to the design that makes it easy to understand and work with. It
also allows us to store historical information in a relatively simple way. For example,
the customer address is copied to the posted invoice, as it was the time the invoice
was printed.
This way of designing tables is crucial to understand when we deep
dive into the design patterns in the later part of this book.
Page
The user interface of Microsoft Dynamics NAV is defined using the Page objects. This
is done by a combination of binding a Page object to a Table, and adding metadata.
The designer is not what you see is what you get (WYSIWYG) as we
design for multiple display targets using one object definition. This
makes defining the UI a highly abstract task.
The Page object inherits all the properties and methods from the underlying table.
Developers have the option of adding additional properties and methods, as well
as business logic that is specific to the behavior of the object.
All the logic to Create, Read, Update, and Delete (CRUD) from the database is
automatically generated by the system. This makes it very easy to work with, but
also enforces the relationship between a Page and Table.
We can overrule this by implementing the Model-View-ViewModel Pattern, which
we will discuss later in Chapter 3, Architectural Patterns. This pattern allows us to have
a UI, which is unbound to the way the data is stored in the SQL Server database.
[ 12 ]
Chapter 1
Report
In Microsoft Dynamics NAV, the Report objects are used for both: printing
documents, and creating batch routines for data modification.
Reports are typically used to print business documents such as invoices, reminders,
and balance due lists.
In addition to printing, they act as containers for batch processes, such as combined
invoicing and batch posting. The reasons for using reports instead of codeunits are the
possibilities of adding the UI (request page), and the built-in iteration capabilities.
This object type is outside of the scope of this book, although we occasionally
mention it when describing patterns.
Codeunit
When writing business logic, Codeunits are the preferred place to do this. They act
as containers for the code that allow us to structure our programming using known
concepts such as encapsulation.
In this book, we will discuss different types of codeunits that are used as part of
the design patterns, as well as best practices to structure our code.
We will discuss how, and why codeunits should be used for code in Chapter 5,
Coding Best Practices.
Query
Most business logic in Microsoft Dynamics NAV is bound to the table object, since
the table behaves as a class with methods and properties.
However, when using complex iterations over many data items, it can be costly to
loop over them one-by-one.
To solve this, we can define Query objects that act as predefined SQL Server queries,
allowing us to join multiple tables in one read and only read the columns that
we need.
XMLPort
To interface with Microsoft Dynamics NAV, we can use XMLPort objects. They are
outside the scope of this book.
[ 13 ]
MenuSuite
The MenuSuite object helps us to search the application. They are outside the scope
of this book.
To learn more about these object types and what you can do with
them, please read Programming Microsoft NAV 2015, by David
Studebaker, published by Packt Publishing.
Metadata
Table objects behave as classes in Microsoft Dynamics NAV, and are the base of our
application. They are bound to tables in the SQL Server, and need to contain all the
fields that will be used in the Page objects.
This does not mean that we cannot add references to other tables in a table object.
We can reference to other tables using Flowfields and Flowfilters objects.
Flowfields
When we define a field in a Table, it gets added to the SQL Server table definition
automatically, unless we change the FieldClass property. Flowfields and Flowfilters
objects are only a part of the Dynamics NAV Metadata. They do not exist as a part
of the SQL Server table definition.
If we change the property, we can join fields from other tables that we can display
on all the Page objects, since they are treated as normal fields. We can also use them
in C/AL code for filtering and sorting. The latter is a new option in Dynamics NAV
2015 where we can, for example, do GLAccount.SETCURRENTKEY(Balance). This
used to be impossible.
The following screenshot shows the property in the Object Designer:
[ 14 ]
Chapter 1
Flowfilters
When using Flowfields, you can use any field in the table as a where clause.
However, sometimes you might need values that are set by users at runtime. This
is what Flowfilters are used for. They are defined as fields in the table, but are not
added
to the SQL Table definition, or available as filter or sorting criteria.
Base application
Most development projects in Microsoft Dynamics NAV don't start from scratch
as empty projects. They often start from the base application that Microsoft ships.
This is an ERP application that allows their users to streamline business processes.
The application originated in the 1980s just as the programming language did,
and has grown organically since its first introduction.
To be an excellent Microsoft Dynamics NAV developer, understanding business
processes is maybe even more crucial than understanding the language, objects,
and design patterns.
Functional processes
Microsoft Dynamics NAV allows you to administer many of the functional processes.
Although they all have different purposes, such as general ledger, inventory, sales,
purchasing, jobs, and manufacturing, they all use the same elements that make the
application recognizable for the end user.
If a user is able to post something to the general ledger, it is very likely that they can
easily understand posting to the inventory. Posting sales and purchase documents
have the same similarities.
This is possible by using the same structures across the application. This also makes
it easier for developers to learn new parts of the application.
Many of these structures have been in the application for a long time, since they are
very closely related to ERP programming. Others have been added more recently,
since they are more related to intuitive UI in correlation with app-like programming.
To learn more about these functional areas and what you can do with
them, please read the Microsoft NAV 2013 Application Design book by
Mark Brummel, published by Packt Publishing.
[ 15 ]
Summary
In this chapter, we have learned what design patterns are, and how they started
and were introduced in computer science. We learned how design patterns can
be applied to Microsoft Dynamics NAV. We also learned about anti-patterns.
In the next chapter, we will build a small application in Microsoft Dynamics
NAV that consists of different design patterns brought together.
[ 16 ]
www.PacktPub.com
Stay Connected: