Design patterns for developing repeatable IP for Dynamics 365 Business Central
Hello. And welcome in this video I will discuss some design patterns that, you can use to develop apps for Microsoft, Dynamics, 365. Business, central these. Are just a very few of the design patterns, that are available throughout, the base application, but. They represent some, of the more important, ones my. Name is Daniel Rimmel Swan and I am a partner with cloud ready software, one of the ISV development, centers that. Can help you with just about anything related to business central, I want, to also take this opportunity, to thank Eric routers, also known as Waldo who's. One of my fellow partners, at CRS, for providing, the content of this video. Let's. Take a look at our objectives first. I'll talk about how. You can apply software. Design as a methodology, itself. So. This, is where, I'll discuss, how you can use, the awareness, of design, patterns, as a tool, in thinking, about how you are going to implement, business. Requirements. In your app, next. I'll talk about some, of the design patterns, specifically. And the, elements, that make them so powerful, and then finally, we'll. Cover. How. Visual, Studio code snippets, can be used to, help you implement. Those design patterns. The. First section is applying software, design as a methodology. What. You need to be. Aware of is that we are talking about an event-based software. Architecture. And, in that context, I will talk about agents, I'll talk about encapsulations. Decoupling. And readability, of your, code and, these. Four concepts. Are very important, when you think about event. Based software and, about you. Know properly, applying. Software. Design to, a business, application, when you link the, methods. In the functions, in your business, application. To, the specific. Technical software design, that you are going to apply. So. You have, a business. Requirement. And. You've decided, that you. Have a function, and the function is, a. Function of, the, just order. And that, function, is, creating. A pallet and so from your purchase, order you want to create a pallet, and so, then, your next decision is okay I'm going. To implement a function some, logic, some code and, I'm going to program this, process. Where. I'm going to create a pallet, based on some variables, and some values, that are within, my pio so. Then your. First one. Of the first decisions. That you'll make is where, do I put this new code right, so you, can visualize, you're looking at your purchase order screen and you have a button on your screen. That says create palettes, you can have an, action on your, screen so in our screenshots, we have a page, extension, for. The purchase order subform, and on that. Page. Extension, we have an action, called create, palette and you, know we've programmed that action, to do. The actual thing itself that's. One, one thing to do one way to go this. Is of course not the correct way to go because now we have business, logic the actual, business logic on, the page. Object. Which. Is not the right place, for business, logic. To live so business logic should. Not live on a, page object, not. In the least because, you. Want to be able to reuse this perhaps, in other spots in the, system and you. Don't want to have to create. Variables. Based on a page where you can then use. The action, code in there and it's it becomes very, messy that way so, the. Next layer in maybe. Is. The. Table. Maybe. We can create a table object, and in the table object we have a procedure, called create, new palette and we. Can call it from there so. In this case in this screenshot we have a table, extension, object, that, extends, the purchase line table, and in there we have a, function. That creates. A new palette this. Is better. Than, on the, page because, this way you, can call, the, create, new palette function. Anywhere. That you have a variable, based on the purchase line but, still this is still part. Of your purchase line so anytime that there is a change on. This you have to go into the purchase line. Table. In order to make. A change to, create. New palette function, so you. Have the. Create new palette, function. As a business. Piece. Of business logic but. It is contained. Within the scope of the purchase line so instead. Of working. On you. Know the new palette, functionality. You're, working, on the. Purchase line in general, and so you. Might work on create, new palette somebody, else might work on you, know a different function, and now you're working in the same object, and it, becomes very messy because now this one object. Contains. Logic, for a whole, bunch of different purposes and so. Again. It's better than having the code on the page but.
Still It's not really, the. Best option so. Then, you know and this is quite common we, could create a, management. Code unit and this is common in a lot of. Applications. A lot of ISV products, a lot of verticals. And add-on products, have, some. Sort of notion of a management, code unit which is almost. A functional, collection, of functions, that. Provide. Different pieces of functionality so. You would have a palette, management. Code unit and within that you have you. Know create new palettes and you can see in the bottom of the screenshot, you have a sign palette to purchase line maybe, there are some other functions. As well and, there's. Probably some common. Logic, that you, know are used by different methods, this. Is even. Better than, having. It on the page certainly. Also. Better than having. It on the table but still, now, you have a code unit that has different, methods, for different purposes, inside, the scope of a thing code unit. So. It's still not optimal, so. You. Might think well doesn't, matter because the, computing, result, will, still be the same it doesn't matter whether. The code is on the. Page or, on the table or in a collection, code, in it to management code unit the. Result is the same so, whether whether. The action. Code. Has, that function, or whether it's on the table or wherever that logic, executes, and the result is the same anywhere. Anyway. There. Are a number of aspects, that you have to think about think. About transparency. Think. About testability, your, ability, to upgrade or, to easily expand, or, repeat. Reuse. The. Functions that you create if. You think about all these aspects. You. Might come up with a way, to reduce, the total cost of ownership minimizes. The risk and opens up possibilities for. You, know all these different ways. Of designing. Your software, and so, it does matter where you put code because. You want to have a system in which. The. Way that, the code is written, the way that these technical. Pieces. Are put together are. Consistent. So you have a way where. If I have a business requirement, to create a pallet, I will.
Have A method, code unit that creates the pallets that, method code unit is called, from, a function, on the table and from, that table I can then call it anywhere, that I'm using that table so wherever I, am, using the purchase, line I can, just simply call the, create pallet, function, on the. Purchase. Line which, then calls the code unit that, puts that business logic to use and so if you do that your. Application. Turns into something that's much more transparent. Based. On those method, code units you have a a. Very. Clearly, defined piece, of logic for which, you can create tests, that. Are easy to update because, you, know the business. Logic for, a single. Function. Is contained, within a single object and so by. Coming. Up with. Consistently. Applied, design. Philosophy. A way of thinking about how you implement, these business, functions. The, cost of your total ownership your, total cost of ownership will. Be reduced significantly and, it'll be you, know easier to implement new. Functionality. As well because, your, technical. Design. Thinking. Drives. The way that, you approach. You. Know creating. New functionality. So. This. Is what I'm talking about when, we, talk about the age and action business model it's a structured, approach to writing code. Where. We transform. Business, requirements. Into coding elements. Which. Means, that you, take a business requirement, and, everybody, that you that works with product development or, even, custom. Client. Implementations. Will. Have some sort of methodology, where, you have a list, of things that you work on and those, are called business requirements, and so if you have a way to. Implement. Business. Requirement, in a certain pattern of, coding. Elements, the. Way that, you pick. Those things apart, becomes. A way of approaching. A, technical, design in, itself so it becomes, a conscious, selection, and usage of the coding elements, based on how. You're going to implement a business requirement, and, so your development, decisions, become. Almost. Self-explanatory. Which. Which you know that's the the key of a methodology, is because now we. Have a common approach a common way of thinking, and so now we can learn, from each other we can teach it to others and we can apply it to whatever. Situation, is. Necessary. So. The. First thing you do is you. Determine, any, development, at any business requirements. By determining. Who, is acting, and we, call this the agent, and so agent, in, development. Is always a table. And so I, just. Want to cover real quick these, elements. This concept. Is really an object-oriented. Programming. Concept. And, since. You, know we are working with business central and we know business central is not, object-oriented.
Programming. We. Don't, necessarily have, classes, but where we say class we mean tables, so in the context, of making apps for. Business central, when, we talk about a class we, talk about a table, because that's. The easiest, way to kind. Of translate, those. Object-oriented. Programming. Concepts. To, the way that we are going to be developing. For business. Central. In. Some cases it can also be a code unit but, that's only when there's no table. Inside, of Business Central but, a code unit represents, the methods of an external, class so. This hardly ever happens, most. Of the time we're, talking about tables. So. In the agent, action, business, model, you. Determine who does. What where. And so the who is the agent or the class or the table in business central does. What and what is the action, is the function, or, a method and. Then, the where is the context, within. That. Action, and so based. On that you have a business requirement the. Business requirement. Covers, a certain, entity that. Generates, a certain action and that. Comes with a flowchart that, explains that process, and so most, business, requirements. Do, cover those three elements who. Does it what do they do and what's the context, within that. Action, so. That. Goes into our. First. Concept. Called encapsulation. Encapsulation. Is one of four, fundamentals. That off of. Object-oriented. Programming. Those. Four. Fundamentals. Are encapsulation. Abstraction. Inheritance. And polymorphism. I have, to say I'm not a computer. Sciences. Nerd, so. I only. Know very, little about what. This all means, but. Based on you, know the. Principles. Of object-oriented. Programming. Is. Is. Essentially. What we're trying to say is we. Want, to apply. Professional. Fundamentals. Professional. Concepts. Towards. Designing. Solid. Business solutions. And by looking. At, industry. Standard. Accepted. Object-oriented. Programming, techniques, we. Can take those, lessons and apply them, to. Business. Central, objects. And so, encapsulation. Is one of the things that we can actually accomplish. Abstraction. Inheritance. And polymorphism are. Concepts. That are not available to us because we. Don't do object-oriented, programming, but it capsulation, and decoupling, are. Some. Things that we can approximate. Very very. Nicely. It, also means packing, of functions, into, a single, component. And hiding. Methods. In an object by building an impenetrable, wall, to. Protect the code from accidental, disruptions, so. Basically what we're talking about is, when, we think about, encapsulations. We, think about taking. A business, requirement. And tracing. Or translating. That into a, single. Object that, does a simple single thing and all, the stuff all the plumbing, that is part, of that process we. Try to keep that within the. Object. Without. Exposing. That to the outside and so we'll go into what, that means in. A couple of minutes. Encapsulation. Is also a language mechanism. To restrict access to defined, components, in. Other words be as local as possible so, one. Of the key things. That we always try. To hammer. Home in our workshops. Is using. Global. Variables. Global. Cedar's everything, global is is basically. Not. Good. Except. In certain exceptions. But. Other than that we always try to be as local, as possible so, take a variable make, that a local, variable and pass it into other functions.
And Do, something with that variable. As a parameter rather, than a globe a global. Fields. Are. Declared. On the class so, in object-oriented. Programming that's called attributes, on the class, or. Properties. And. These attributes, on the class, for. Business central we use the, term you know we use tables, for class and fields. As attributes. Methods. The functions, should, always be, declared. On the class and so when, we have a, function. But for instance calculate. The item classification. Which, is a function, of the items so the item is the agent, for the classification, calculation. Then, we create, a method on the, class so the table, the item table gets, a global. Procedure, called, calculate, item classification. But. The method itself the, business, logic is, contained. Within that code unit so the screenshot is an example where we, have a table, extension. That extends, the item table and we. Add a procedure. And that's not declared as a local so it's accessible. From other, objects. So, we can call item, dot calc, item classification. And when, we invoke that method that. Really calls, the. Code unit and as you can see it passes, rec it passes, the item record into. That method code, unit and so now anywhere. In the system where, we are using an item variable. And, we want to calculate the, classification. For that item all we, have to do is call it a calc item classification. Method. And it'll, do it the, same way throughout. The entire system, if, we have to do, some maintenance on the. Item. Classification. Logic. We have a single, object. That's that code unit and all the logic deck is contained. For, all the logic that's related, to that calculating. Of that classification. Is part. Of that single code unit so any. Maintenance. That's done to that method, has. To be done in just one single place and by, way of organizing. The code by way of making. It a method on the class we. Apply. This as a standard. Way of providing. This functionality. The. Advantages. Of this, method declaration. Is you. See explicitly, which, functions, are existing, for each table so, in, object-oriented. Programming, lingo. You say which methods, exist for each class I still. Think it's a little silly to talk about classes, and methods but you. Know the table in business central we have tables and functions, and so by. Looking, at intellisense, you know you have req dot and that shows you know all the functions. That are available for. That table and so it becomes really easy because you. Have access to the table now you also have access to all its functions so, you don't have to browse through the, code unit list anymore, to see if there is a function, for something. That you want to be doing with that item all you have to do is declare an item you. Know do item dot and intellisense. Will show you all of the methods. That are available for, that entity. For that table, B. Methods. Are available. On. That table, but, the methods themselves are not coded, on the class so the actual method, itself the business logic that is contained. Within that method, lives, in its. Own code. Unit so each method, is the code unit of its own so in the screenshot, here you see this. Is a screenshot of a list of code, units so we have a calculate, item classification. Method. Code units we, have an insert, default, values, method code, unit and those, are single, objects. That contain a single, method so, we, don't have a management, code unit that includes, methods. For you. Know a dozen, different things now each method. Has its, code, units. Now. I've, talked about being local, this. Means that every method, code unit only has a single global, function so, in the screenshot, that you see now we, have one. Procedure that's not declared as a local, and that, one procedure is calc item classification. So the, top you see code unit 50000, the name is calc item classification. Method we, have one local, procedure, that calculates, the classification. And then, we have a number of local procedures that, function, as you. Know subroutines. For the. Main function, itself and we'll go into why. We have those and. Why it's necessary to have those as a couple. Of a. Number, of procedure, calls rather than you, know just putting everything into one single. Method. And. So. The method, itself is, always, invoked, through, its class which. Means you know in business, central terms the. Function. Is always called from, the, table, so we have req, dot, calc, item classification. Right, so we don't call. The, code unit from other spots, like, what we could have done in, the action, trigger we could have created, a code. Unit variable, and then have, an item variable, and then send the item variable, into the code unit variable. But, if this page. That we're working on is already, a page.
That Based, that, is based on the item table now. We can utilize, that and say okay I'm looking at the item table now anyway, that, item table itself, has a method, to calculate the item classification. So, I can call that directly, from the page without, having to, declare any. Additional. Objects. In order for me to be able, to program that and so. The. Code unit that represents. The method, is never called directly, even, though technically, you could do that but. Because. We have a table function it's it's just much easier to do it, like that. So. Let's. Go into the, next concept. Which is readability. Readability. When, I talk about readability, of. Code. We're, really talking about, how. The main, function, of each method, is a readable, flowchart. Of what the method does and so, I have a screen, shot in the next slide that kind of. Explains. That a little bit more, but basically, instead. Of having all the code in there we, take. Some effort into splitting. Everything up into steps. And so those steps you. Have to think about if if we're talking about a business process, you. Should be able to create a flowchart for, that business process and, that, flowchart, has you, know boxes, for each step, in that flow in that process so. You can think about or that about, that main method. As a. Representation. Of, that flowcharts though, you, should be able to take the business requirement. In which you should have a. Flowchart, if you don't use flow charts in, your business requirements you, should start doing flowcharts. Today because, using the flowchart you can translate. That almost directly, into function, calls inside, of a main method code unit and so, it should include all the relevant steps, and. It. Should resemble. Understandable. Plain text, so it, should be readable for. Quote-unquote, normal human beings and, so you know we're all developers. So we should be able to, understand. When something, goes. Into custody, at, instead, of get customer, even. Though cusp, get, is you know that's syntax, that's programming syntax so that's nerdy get, customer, is you. Know else is a function calls are really inside. There is the same stuff so really you will you, should be able to understand, it as a developer. But, in in your main, method. You, should have function, calls rather than actual. Syntax. And so all the business. Logic itself, all the coding, itself should, be split up into those steps out of your flowchart and into, each of those steps, you, contain, the. Logic, for that step, within. That the, confines, of that. Procedure. Because. It becomes more readable it, becomes better able. To be maintained, so, you know this is a, screenshot. Of one of those main, functions. We, have batch post warehouse, costs we have a number of function calls in there representing. The steps in that flowchart. So. We. Have a number. Of function, calls like one, of them is called error if no posting, date that's. Almost self-explanatory, so we're going to show an error if there's no valid posting date so if you go into. That function it'll, check for the posting date and. It should not do anything other than, checking the posting dates if you. Want to add another, check then, either. The, check is part, of the, period. Checking. Or posting, they checking or you want to change a third thing and so in that case you would create a new function to check that third thing and then, you place, that function, call in the, sequence, of your flowchart in here and, so by, structuring, your code this, way it, becomes a lot easier to, read the. Code. Like, this and. It was pointed out to me that in. The clear general journal posting, function, the Elves look a little bit like ones, and they. Do. And. Another. Thing, I've set this before. You, have. To keep the, functions, to single, task functions. And so the. Function, has, a single, task you don't put multiple, tasks, inside, of a function so if a function has, more. Than one step so, in, the previous one, instead. Of having four calls to do errors, you might have, a. Function, that calls, that's called check. For all errors, and then in that check for all errors, you, might have four, different. Calls to four different categories of Eros you might be able to structure. The code in that way so it's.
It's A judgment call right so if you have for. Error calls. You. Think to yourself you know this is becoming a little bit confusing, and. They're always grouped, by you. Know I check. All the errors as the first few steps maybe. You can put those calls, into, a single error call, error. Function, but, that function. Itself is then split up into other, steps and so you're, not going to program a single, error function. To, check six, different errors, right, so you would create, six error functions. And then, put that as six different steps, into another function. Right. So that's that's the key to organizing. Your code into, doing. A single task per, function. Which. Takes us in to. Decoupling. Actually. Coupling, is the degree of interdependence, between software. Modules it is, a concept. That was introduced, some time in the late 60s, early 70s a, guy, called Larry Constantine I, know, this because it's on this slide and, I looked at the wiki, article. And was, very fascinated, by this look this guy up he. Wrote the, book in nineteen, seventy five structure. Design with edu, worden. Basically. The. Concept is where. You, have a, piece. Of software, that. Depends. On other, pieces of software and, when, there is a change to one element. That. Precipitates. Into changes, of other elements, so you. Have to think about a function. Call within a function call if that. Function, call gets. An additional parameter. Not, only do you have some maintenance to the function itself because you have to add a parameter but. You also have to maintain the code that calls that function and so the, more intricate that system, is the more, coupled, that system, becomes and so. Decoupling. Is the process, of removing those dependencies. And so loose. Coupling is a sign, of well-structured, software, and good, architectural. Design and so the rule of thumb is loose. Coupling is good tight, coupling is bad, also, known as low, coupling and high coupling. Like. I said I'm not a computer. Science, person, but, I can understand, that you know having. Seen enough nav. Code in my life I see, that you know all these management, code units that are almost. Some sort of network of interdependent. Functions. Become really difficult to maintain, especially. When you, know functions. Call each other almost recursively. And it becomes really difficult to maintain. So. The different those, are the disadvantages of tight coupling right so a change, in one routine forces, a ripple effect of changes in others, routines. Are hard to reuse because dependent, routines must be included so if you, have, loose. Coupling and. You have a you. Know a single, method in, a single code unit and that is a an independent. Piece of software, that is just for that single task you. Can port, that task anywhere, that you want and so it. Becomes, really easy to maintain. And. It's. Much more effective that way, calling. The function from the table, is. Already implemented. Right so we've already implemented. This decoupling, by calling, out from our method on our table. This, is only done in this one location so if. There's a change if you change from one code a unit to another, code unit you. Would keep the, function, declaration on, the table the same you, just call a different code unit and. Every implementation. Of that method in, the system will, then be changed, simply because, the, method, on the table the function on the table has changed, code units and so instead.
Of Having, to go into each spot, where that code, unit is called, the. Implementation. Is very effective. Because instead. Of calling the code, unit you've called, method on the table, instead. Of calling the code unit and so, assuming. That you, know the the function declaration on, the table remains the same you know if the signature, stays the same. You don't have to make any changes if you want to make it and. Similarly. If the, logic, inside of the code unit changes, that, has no effect on the method on the table itself or anywhere, that that method is called throughout, the system. You've. Talked about this before calling. The method from, the table, so the code unit from the table we have the item extension. We have the calc item classification. Which, calls the chiton classification. A procedure. In, the, couch item classification. Method, code unit right, so that code unit has a single, global. Function. That, is called from the table and the table now has a, method. Of function, that can be called anywhere else and so that's. The function of that method. Code unit so the method code unit in the, bottom you see has, one procedure, called coq item, classification. And it, receives, an item, as a record. Also. Part of decoupling. There's. Only one global function and we've, seen this before as, well so I, hope you can see the pattern here within. The couch item classification. Method code unit we. Have a single procedure that's the global, procedure, and, all, methods. That are part of that flowcharts. Are steps, all the steps in the flowchart, has. Procedures, that are local, inside. Of the code unit. So. You, know going into the, global function that calculates and classification. Function, you. Can go in there and now you can see the structure of the global function itself so, we have a number of steps first. We have a, conformation, then, we have a non before calc item classification. Event. Then, we have the do calculate, item, classification. That's the main. Function. And then, we have another event on after. And then finally, an acknowledgement, for. That and so, that's that's, the main function those are the steps in there so first we do confirmation. Like that's a UI wrapper right. So the structure, of the global function, has a UI, wrapper, for, confirmation.
Then. It has the first part of an event wrapper which, is the on before, then. We have a method, header which is the main function. Then, we have another, event wrapper, for on after, and then, finally, we have a UI wrapper, to, acknowledge, the, completion, of this process and all these steps. Are. Local. Function so you, don't, see all the ones here but everything, is local procedure, except. For the first a calc item classification. So, this. Guy over here is, global. All of. These are local, functions. The. UI wrapper, so. We have a, confirm. Calc item classification. That, passes, the item, record in there and then, we have a high dialog, boolean. And. Then. It says you know if not duly allowed or high dialog, then. Exit, and then we exit true so that means we go on with a process, and, otherwise it shows a confirmation. Message and the, same is for the acknowledgement now, I want to say, these. Are not obligations. Right you don't have to do a UI wrapper, so this is a matter of choices. If. You, choose to not. Implement, a UI wrapper, within, the method code unit that's, the valid choice but. That would be a choice of you, and your development, team to make a you, know a conscious decision make, a conscious decision to say am I, going to do a UI element in. My method code unit or do. I do this. On the. Page object so do i implement, a UI wrapper, on the page object, and then take, care of calling, the method code, unit without UI. Wrapper or not we. Chose for it you know purpose of this video to include. A UI wrapper, in the method code unit just. To show you that, and, is where you would put it. Then. We have event, rappers and the, screenshot here says business, events, you'll. Most likely put everything in integration, events because. Most of the events if not all of the events, in standard. Business. Central, are also business. Events or an, integration events, sorry business. Events, are if you, are dead. Sure that you know this, signature. Of this event. Is not going to change you. Might have noticed that we have a handled, boolean. Handled. Boolean is basically. To. Facilitate. The. Ability to bypass, logic. And so if you. Can handle the business case within. The main function. Or if. You want to meet certain conditions, and skip the. Processing. You. Can handle that in this event and so by doing, a handled, boolean, you, provide, the mechanism to, extend, the logic and, then, by, having this handled boolean mul explained, in later slides. How that interacts, with the main function. So. Then you have the main function. Ducal. Chiton classification. And as you can see if handled and exit, means you, know if my event, the shows that all conditions, are met and the business case is cleared then handle. This is, set to true and, then, you can return that and then you know it'll, exit, out of the. The, logic, based on that handled and so this, is you, implement, a handled boolean, if you, want to provide, a way to bypass. Standard. Logic, so you know maybe, you have like a you, know a price calculation. And you have a standard, price calculation. But, you also want to be able to provide, a way to bypass, the, standard price calculation. And, give. Other, developers. A way to, replace. That method, of price calculation. With, a different, one and that, would be a mechanism that you can do that another. Way could be this, is where you can check for. You know whether the license has been paid so did. The client pay, for their subscription, if it. Didn't. Pay for the D'Souza's for, the subscription, you.
Set Handle to true and then, you know it doesn't execute, any logic. The. On before, main wrapper, you, know the in this case implemented. By the on before, calc item classification. Event. Is. You. Know the, extension, for, for. That function and in, there what you would do the use case for the on before main you. Do a preparation, or a validation, in, which case you would not send handle to true or. And. You. Have the addition or the exclusion. Right so this is where, you say I am. Going to. Subscribe. To you, know on before, main. And. Then I'm going to check and if, I have additional. Logic. I can run that logic, or exclusion. Logic so instead, of the standard process I want to do something, else and if, that executes. Successfully. Then. You said handle to true and that passes, back into main, and if Exodus or if handled is true it would just bypass that logic. And. This is an example of you, know on before create pallets it, just the field for document, number I does, not set the handle to true but it does check that document, number and so there's, nothing in the document, number you'll get an error message. An. Addition, or an exclusion. Except. If already handled, likes it if you don't want it to handle disabled, so that's in the scenario, for the, licensing, you know the subscription. If you've, paid or not and then exit if it does not belong to your sub universe, in. That case you set handle to true and, then what, you would do is you, would, respect. The handled, in your main as well, so closer. Look at a main method and this, case we have a do create pallet if, handled and exit right so if that exclusion, happens, if that other, extended. Logic. Handle. Business case then we're not going to do, anything then, the next step is error if not item line then we have an error if empty number, and then, we have a step for do insert, pallets in size you can see, everything. Is local procedures right, so even do create pallet is a local procedure that's, not that not something that we expose to the heights outside, but. The pallet creation. Is, done. By this do insert, pallet, function. Procedure. You, can also, extend. Your, own main. Method, so. You know in this case in the, do insert, palettes we've, added an on before. Insert, pallet, record, event. In this, case this is an integration, event, just like everything, else would be an. Integration, event, and, you, know you can you. Can do something, in addition to your standard logic so in this case the.
Standard Logic, sets the number document, type document, number and document, line number you, could theoretically. Subscribe. To the on before insert pallet record and then, set additional fields, and then you know that passes back to this main method and then, it inserts, it so. This, is a way to extend dialogic. This. Is all contained, within what. We call a generic. Method pattern. So. This is a, little. Bit of an extended, example. Where. At. The top you have the, main procedure. And this, is the. You. Know that the top one is the global procedure, so the create, pallet. Procedure. Is the. Function. That is declared, on the purchase line record, so, in other spots in the system you would call purchase. Line dot create, pallet, and, that would then you, know implements. The Met decree pallet method, code unit where, you have these steps, and so the top one is do create, palettes which is the local procedure that, is your main function, right so at first you have your UI wrapper, then, you have your event wrapper. And then you have do create palettes that's this guy. The. Main business. Process, that do create palette. Procedure. If I were to expand, that you. Would have these three, procedures. As function, calls. Then. We have a UI wrapper, that's, the confirmation. And the acknowledgments right that's up here we have the confirmation, and then, down here we have acknowledgment. And then. Those, are implemented. With an, event wrapper we, have an on before, create palette and after. Create palette, and then, finally, we have the business extension. Which extends. The main business. Process, to the. Outside world and so. This is an implementation of, one of the design. Patterns so you've, already seen two. Important. Design. Patterns, that we're going to be talking about next. The. Next section is about design. Patterns and. We've. Kind, of already looked. At a couple of important. Design patterns, up until now but let's, talk about exactly what, design patterns really are. Barter. Knutson who was the printer, principal, nav software architect. Says. The architecture, of nav is based on design patterns engaging. In the nav design patterns, is the best way to really understand, the software and learn how it should be enhanced, this. Statement, was made before. The. Introduction. Of Business Central and so this was really applicable. To nav. Design patterns, but, you know of course since we are on the same, application. Logic most. Of the design patterns, that applied to and maybe also, apply to business. Central so this is still a very relevant. Statement. So. Really what is a design pattern, design. Patterns are really things. That you. See returned, over, and over and over again so one of the examples. That I always show in. My workshops, is the, numbers. So, every, master. Table every document table, has a field called n-o, and. That's always a code, twenty field and those. Numbers. Are, the, primary, key of those tables, and there, is a functionality. That's called numbering, series, that. Is implemented. In pretty much the same way throughout. The entire system, and so in the, customer table and on insert, if the number is blank, and it goes to the sales set up it gets the customer, numbers and it gets the next number from that number series, the, same in vendor if the number is blankey goes to purchase set up it, gets the vendor numbers number, series, and it gets the next number the same applies the items to sales headers, to other master.
Tables And document, tables and so the. Way that that is implemented. It's. Called a design pattern so, in addition to what we've looked, at before where. There is a somewhat, implementation. Of an industry, standard, design patterns, we. Also have design, patterns, inside, the application. Where. Similar. Functions, are done, in similar, ways, and. It's important, as a business. Central developer, to, get familiar, with those design, patterns, and you. Know use. Those to your advantage when you create, custom. Functionality, or products, unshelled. The. Wikipedia. Definition of, design pattern, says in software engineering a design. Pattern is a general, reusable. Solution, to a commonly, occurring, problem. Playing. Design, patterns is a standard, way of coding, in non nav contexts. There's, in computer, science there's, theories. Out there and, there. Are standard, sets of design, patterns, that are applicable, in. In, various, different. Context, of software. Projects. You. Know if you're interested, going and and and find, out the design patterns, you know like the industry standard design patterns, those, are, general. You, know technical, patterns, that are used like. That. You can use within the. Context, of, object-oriented. Programming, but also other, types, of implementations. The. Design patterns, are not random, or definable, by anybody, they need to undergo validation. And be approved and to, that end there's, been a an. Effort by, Microsoft, to. Put. Together a, design patterns, wiki, this, was, a, few, years ago I can't remember exactly how, many years ago but it, was started, and there are quite, a few design. Patterns. That are in there that have, gone through some scrutiny by. The Microsoft, team and have gone through evaluation. And validation, and then. It kind, of laid dormant for a while I know. From, some, sources inside, the nav, team that for, business central. They, want to revive their attention, to these design patterns so. They have, planned, to. Pick this back up these. Design patterns still apply right so they are still valid design patterns, but. They want to extend, these, patterns, into also, being based. On event based architecture. And specifically. To apps and so the, plane is to pick this back up and to. Have. New design, patterns, and revise and. Re-evaluate. Whether. This is still applicable and so this is a community site so go in there provide your feedback and, they. Are looking at this and so like I said they're going, to pick it back up so keep. Your eye out. We. Are going to look at, for. Design, patterns, one, of them we've already looked at kind of that's the generic method pattern the. Author was Gary winter mine one of my fellow, partners. At cloud ready software, then. We've, also looked. At the handler pattern that was written. By Thomas, Helzberg, at Microsoft. We. Have something, called a discovery event, pattern, also offered. By Thomas, Helzberg, at Microsoft. And then finally the, variant, facade, pattern, written, by Gary, from, Claude Rudy software, chunk, alpha mom from, cloud ready software, and Waldo you go out there's cloud ready software I don't mean to brag. About this and it's of course not a coincidence that, these are written by cloud ready software, because we. Include, these patterns. In our workshops. And so the workshop is the basis for this video and so of course we picked ones that we were involved in that, doesn't mean that we think these are better than ones, that we didn't write, there's. A bunch of awesome. Design patterns, I just wanted to you, know walk through these because we've put quite a bit of time into the, presentation, of for, instance that generic, method pattern I think is one of the most valuable, event. Based. Design. Patterns, out there and so that's one that I also. Use in, development. Project myself so, I'm not going to spend. Too much time on the generic method pattern because we've already gone over that. Declare. Each method so you you, create a method as a code, unit on it on its own and each, method, is derived. From a business, case right so you have a business requirement that, business requirement. Results, in the. Creation of a method and each, method, is implemented.
In A single code unit and that code unit is then declared as a method, on its class which. Is you know a procedure, on the table and then, you invoke, that method only. From its class so although. It. Is technically, possible to, call the code unit, from. Pages, from, action. Buttons. And that sort of thing the way to invoke, the, method is through. A call, from its class so the, table gets a procedure, that has, that method, in it and then if you want to invoke that you, do record dot, table. Variable, dot method. Rather than sending. A record into, the function. Directly in the code unit each method code unit only has one single global function, and local, functions, include, the following categories in. That order you have a main which is a function. Method. Header and form a readable flowchart, and so I've gone, over this right so all the, steps in the main, procedure. Should. Come almost one-on-one. From the flowchart that represents. That business, requirement, then, you have a main business process, and those are multiple, functions right a function for each of those steps we, have a UI wrapper, we have a business extension, and then, we have the event wrapper and. Our. Recommendation. Is for. Each of these method, code units you, order, the, functions. In. That manner, and so if, you do it like that then. Your method code units will become very readable right so you always expect. You know like your UI wrapper, to. Be in a certain spot you expect. Your business extension, to be in a certain spot you expect, your events. They publish the event to be in the, same spot and so that's, that, fancy. Colorful, slide that. We ended, the last or the previous section, in this is the generic method, pattern and. This. Would be an awesome, candidate. For creating. A snippet I believe. We have actually. Have a snippet that I. Can show you the. End of this video. Next. Pattern is the, handler, pattern and we've talked, about this this was part of some, of the functions, inside, of the generic. Method pattern the. Handler pattern, is used. In the on before, main event, wrapper, we, also have, a use case for additive. Or exclusive. Business. Logic and so, this this. Is a, way. To signify, in, additional.
Or Exclusive. Business, logic to say is my, use, case handle, and so the, use case is derived from your business requirement. If your use case for that business requirement. Can. Be. Handled. By on, before. Main then, inside, that code you would say ok handle, this true and that means, that I don't have to execute, the main code in, our method code unit and more because I've already. Handled. My use, case. So. The description of the pattern, is you, pass a boolean, variable to all imma four main events, plus main you. Do not execute. Exclusive. Mains if already handled, so the first line in your in, your main. Function, is if handled, then exit right, so you completely, bypass, that if it's already handled you define a subset of business, cases in each exclusive. Main subscriber, event, you. Do not execute, execute. Exclusive. Means if they do not belong to the business case and so. The, handled boolean. Variable, is set to true if that case is. So. You respect, that on both ends right so you have a subscriber, to the arm before main, in. There you might set handle and. Then in your main you. Respect, handled, and so if handled is true then you exit you don't execute the business logic and. This is what that looks like so. In. The on before. Main event, wrapper, the event gets raced and that's the on before create pallet that has a red outline then. You, pass the boolean, to the event publisher, right so you have on before, create, pallet gets the purchase line that, was sent into the create pallet, main. Or that. Put the procedure to create pallets, procedure, and then. The event publisher has that purchase line and we also pass, it the handled, which is a local. Boolean. Variable, of that main global. Procedure. In. The event, subscriber, you, respected. The handler, so in that on before create pallet disable, you. Have your business case you your business logic and if that's if, that handles the business case you said handle to true and then, in the main function when you get that back you go in to do create pallet, then. If, handle, then exit. So. That's the handler, next. We go into the discovery. Event, pattern. Discovery. Event, pattern searches, for modules, functionality. Behavior extensions, that. Can perform a, specific, tasks or achieve, a specific goal. It. Allows, modules. To advertise, their presence, to, the application. Some. Examples. In the standard application are. Service. Connections. Assisted. Setup and my. Notification. And I'm going to show you the, assisted, setup in just a few minutes. The. Pattern components, is. As. Follows, we have a discovery, event, that, requests. The discovery, of the available, modules, then. On that, event. There is a registration, method, which, allows modules, to advertise, their presence and then, we have modules table, and that can the table contains, information, physically. Or temporarily, about. Available. Modules. And, an. Optional, component is, you. Might provide, a set of page ID or used to configure specific. Modules, and so let, me just show you the, way that that looks so. In, this screenshot, at, the top you, have the, page, 1279. The. Service connections, when. That opens, it runs, that reload service, connections, and the first thing it does is it deletes, everything and, then, it runs the on register service, connection, procedure, which, is a an event publisher, and so, on the table, 1400. The service connection table, there is that, unregister. Service, connection, event, and. When. You subscribe. To that in a code unit so. Let's say you want to implement a service, connection, this, service, connection, is just a list of service. Connection that serves multiple pieces, of functionality in, the app and so, you can, add your, service. Connection, to that list by. Subscribing. To the unregister, service, connection, event, and so what you would do is you would create a code unit in the, code unit you have a. Subscriber. To this integration, event, and that, sends. In that service connection, record and that service connection, record is a. Temporary. Table. Variable. That, you, can then you use the insert, service, connection, method. To. Register. Your. Service, connection, and so, in, your, subscription.
You. Have a, parameter. Of service, connection, and then one of the lines in your code is service, connection, dot insert, service, connection, and that's, where you put. That service connection, into the service connections, screen, and so the, example that I'm about to show you is the. Assisted. Setup which has a very similar way. Of implementing, that so. This is an, example, of a, code. Unit, in which I have. Program. This is one of the demos in the SAS, user experience. Video so, if you want to see how this is put together watch. That video I'll. Take you through this step by step this, is an example where I have, a company. Info wizard, page, and I, want to add that to the assisted, setup screen. Assisted. Setup screen. Hey is a list, of assistive, setup based on the aggregated. Assisted, setup table, and that. Shows temporary, records, and so those records are not stored in the database they, just show on the screen and so that page has, a, spot. Where it refreshes. These, assistant. Set up and so aggregated. Assistant setup table has, a, unregister. Assistant, setup event that i'm subscribing, to so i'm subscribing. To. That temp, aggregated. Assistant, setups, on register. Assistant set up event. And in, there that. Table, the temp aggregated. Assist that setup table provides, a method that, says add extension. Assistant, setup and, what that does is this, creates, a record in that list of assistant, setups and so for. Your module, to be added. To that list all, you have to do is subscribe. To, that register, assistant. Setup and, you. Know that's the mechanism for the discovery, pattern, and so, this. Is one of the examples, where that's really useful and, it's, a very interest. Pattern. Because it's not something that as an nav developer, you think about very, much but, once you see the. Use of how this could work you, know with the service, connection and my notifications. And assists that set up and there's a number of other. Examples that are very useful or, something, like that so it's, a very useful, common. Way. To. Discover. To provide common, functionality, by you, know having a list and within, that list you have access, to functionality, is very powerful. The. Next. Pattern. Is the. Variant, facade, pattern which. Provides, a single interface that can take any record, or record, ref or record, ID as an argument and so in, here there's a little screen shot and in there we have a parameter. Variant. Record, that, is a variant, and we have an argument table, that, argument table is different, design, pattern, actually so. Take. A look at the argument, table design pattern, and see.
What That's about the, most important, part of this guy though the variant facade pattern is that it, implements, one way to, provide. A single, parameter, that. Can be a variety. Of different things. So. What's, what's, the problem, this screenshot. Shows, two functions, of code unit, 229. The, document, print code unit to different, functions to to print to different, documents, and so you can see on the left hand side we have print purchase header and, on the right hand side we have print service header the. Code, inside, is almost, identical, there's. Some. Little differences. But. It is necessary. To have two. Functions, because, C a is strongly, typed and, the problem here is that most of these two. Functions are identical so, it's very difficult to see where. The differences, are and what, really is the difference but, they implements. Very similar, functionality. So, if there's, a change in the way that documents, are printed you, might have to do. It in two different places. Except. There's. Not two of them there's 12. Of them except, there's not 12 of them there, are 20 of them this. Is problematic, because of a number of problems, you. Need to carefully, read each block of code and they almost look, the same so, it's very difficult to discover the differences, between those functions, and. So if something needs to be changed, you now have to probably make that same change in all of those twelve or twenty different functions, and. If there's a conflict in an upgrade then the same conflict, need to be resolved, in each of those functions. And. Then another problem. Is testing, you know if you want to do automated, testing now instead, of having a single function, that applies, to all these different tables, you. Have twenty, of them and so you have to write 20 automated, tests, when you add a new document type, you also need to add additional function. To this code unit with almost. Cloning, that the same code and applying it to the different table. The. Solution to that problem could, be a print. Document. Procedure. That. Takes a record, variant. And so. In that variant, facade pattern we tried to provide a single interface that's flexible enough to handle the current and future usages. And this, can take any record as a parameter and we can keep records specific, code separate, from common, code and that eliminates, the need for cloning. Most of the code and it, makes this much easier to maintain in this, example, we. Have a single variant, plus an argument stable like. I said the arguments Turbo loops itself, is another design pattern, that's. Kind of optional. So you, know we start with a common code block then, we have some specific code, block so in that prepare, record. Function. In the middle there. Are you, know definitely some, record, specific, logic like. This shows and so there are definitely. Downsides, but this is one way to, take a look at how. This can be done and so these. Are. Some. Of the issues in, that. Particular, design, pattern, it's not possible to find usages, because, it's strongly-typed, there. Are no errors at compilation because it's completely. Disconnected. From the tables themselves so it's harder to debug be. Careful with filters, and marks. If the function needs to support multiple records, a key, statement, can explode if there are too many tables that require specific processing. So there's definitely. Downsides. To, this, particular, one but it. Was a design pattern that we. Came up with that implements. You, know a single function for multiple, tables. In. The final, section I'm going to show, you a, snippet.
That. Was. Created, to implement. The generic, method. Pattern, just. A short. Bit, about. Snippets. So, Microsoft has provided a bunch of snippets that comes with the, al extension, for visual studio code and in. The community, there are a number of people that are creating, additional, functionality. Like an extension of the Al language, extension, and, one of them is the CRS, extension. They all language. Extension, from cloud-ready software, which, Waldo, has developed, and in, there he's, created. A snippet for a generic, method pattern and so, just to refresh your memory the, generic. Method pattern has, one, method for main that's, the main. Procedure and, then, we have a UI wrapper, an, event, wrapper, and a business extension, and. This particular, snippet has this, pattern, implemented. Except, it doesn't have UI wrapper, and so like, I said before in this video it's. It's, really, your choice whether to include, a UI wrapper, in. A generic method code, unit, but. You know there you have it so let me just go into visual. Studio code and I, can show, you what, that looks, like so, this. Is my workspace. Right. Now I am, going to create, a new file and I'm going to call it Daniels. That. You know. One. Thing I want to point out is in the extensions, I have the CRS, al language, extension, which, includes that snippets, part of that language, extension, is I type. In T, code unit, and. You can see that the snippets, that are provided, there, I'll have, CRS. In parentheses, and so there's this method, no UI, snippet. Nice. Like this you know it comes up with snippet. With the placeholders, as you can expect. So. I call a Daniel's method, with. A procedure, that's called. And. Then, take, a look at what is created. In here and so now the snippets, done I have. One. Global. Procedure called Daniels method so. In. My table if I implement this in a table, I would, create a procedure. In that table that's called Daniel's method and then, I have a variable of that code unit and I call, this. Method, because. That's the only global procedure, that's available and that then, has, the. Event wrapper, you know and on before, and, on after as integration. Events those, are the integration. Event publishers, and then. I have a do Daniels, method. Procedure. That, respects, the handle. So. I don't, know if when, you, were listening to me explain this generic. Method, code unit pattern. You're, thinking to yourself oh my gosh that's a lot of programming, for every single method go doing it well you. Saw how quickly I created, this particular, code unit with, assistance. Of it, so, we've. Talked about you, know putting together some, more snippets. I was, thinking about you know a snippet for say. A master, table maybe. Not, a snippet but some functionality, some sort of extension, that creates all the object, for a master, table so when you create a master table, it. Automatically, creates a card page and a list page that. As you know global dimensions. And I'm bringing series, and all that stuff already, part of the snippet but when you start thinking about that it becomes, pretty elaborate. Real quick so. This, method, the, generic method code unit is, a fairly, simple object, type and so this, is the kind of stuff that you can do and so if you're, in the community and you want to work on these things you, can actually submit them to. The al team as the. Snippets, when you go to the github page you. Can submit. An issue you, can put a pull request in, there and put. The object in there so very powerful, stuff. That. Was it for today's video if you, want to learn more go to this, website. AKMs, slash, a business, central apps and there, is a wealth of information in, there that leaves. Me to, say thank, you very much for your time in watching this, video I hope, that you found it very useful and, I. Just. Want to say, wonderful. Day.