Export (0) Print
Expand All

Part 3: Architecture and Application Modeling

 

In the last section of this booklet, we examine a natural extension of the Enterprise Templates theme: architecture and modeling. Whereas the former specifies the manner in which an application is designed, the latter is a technique whereby such a design is codified and implemented on the highest level. Unsurprisingly, Microsoft provides both the resources and tools to aid developers in these two important areas.

Architecture and Patterns
Application Modeling
Microsoft Visio
Visio Modeling Example

Architecture and Patterns

An Enterprise Template is only as valuable as the architecture it advocates. The example at the beginning of this booklet results in an application modeled after a three-tier architecture. This setup might be appropriate for an enterprise-based system, wherein data, business, and presentation code should be cleanly separated.

The usefulness of Enterprise Templates is most apparent when combined with the concept of a pattern. A pattern describes a common problem that occurs within a given context, and then posits a model to solve that problem. The solution is usually a simple relation between two or more classes, objects, services, processes, or components, which work together to address the problem at hand.

The value of patterns is multifold:

  • They are proven solutions (developed by experts) to common problems.
  • They assist in creating flexible and reusable designs.
  • They describe the circumstances in which the pattern is appropriate.
  • They describe the consequences/trade-offs of using the pattern within a larger design.
  • They introduce a common vocabulary into the project.
  • They serve as good teaching aides.
  • They Enable a higher level of reusability (Introducing solutions to high-level problems, not code snippets that can be added to APIs).

There is a general underappreciation for patterns because of their theoretical nature (developers want to write code!) To a large extent, Microsoft has addressed this bias by through the Patterns and Practices Web site (sometime referred to as the Patterns and Architecture Group or PAG). Although patterns are generally abstract—with such names as Singleton, Class Factory, and Command—on the PAG Website you will find patterns with a more practical emphasis. Four of these patterns are succinctly explained below.

Front Controller

Front Controller is a standard enterprise pattern for Web applications that require centralized navigation and control. The Front Controller is an HttpHandler that sits in front of the ASP.NET pages that make up the application's View components. By adding XML support and a command factory, the Front Controller can intercept any page request, look up the appropriate command, and direct the user to the correct view based on the output of the command.

Intercepting Filter

An Intercepting Filter is an enterprise pattern developed to implement pre- or postprocessing steps around Web-page requests. By using a custom HttpHandler object you can develop an intercepting filter that will allow you to implement common preprocessing or postprocessing tasks during a Web-page request. You can chain multiple filters together to perform complex tasks that can execute a set of common processing steps before the page request. New filters should be designed in such a way that they make no assumptions about the presence of other filters.

Master-Slave Replication

Master-Slave Replication is a data movement pattern developed to transmit data from the source to the target and apply appropriate overwrite rules on the target. Master-Slave Replication uses a single replication building block that contains a replication link. This replication link reads the data or its changes from the source, manipulates it for the target structure, and then writes it to the target. This pattern can be used to link two different databases or create a data warehouse from a transactional database.

Master-Master Replication

Master-Master Replication is a data-movement pattern developed to transmit data from the source to the target and from the target to the source when the common replication set is updateable on both sides during the replication interval. This is accomplished by copying data from the source to the target and detecting and resolving any update conflicts that have occurred since the last replication. The Replication building block has two links between the source and the target in opposite directions. Both links transmit the same replication set in both directions. The Master-Master pattern is most useful for high availability or "follow the sun" scenarios in which applications must access geographically separated databases. Each user can access a local database for fast response, yet the local databases are still synchronized with each other for complete data integrity.

Patterns and Enterprise Templates

Any of the above patterns would be an ideal candidate for implementation by means of an Enterprise Template. For example, by giving a Web-based project a class that inherits from IHttpHandler, along with some boilerplate code and an XML "controller" file, an ASP.NET project could easily be given a Front Controller pattern to manage page navigation.

The usefulness of a pattern must be carefully weighed against its impact on performance and maintenance. An invaluable Web site to this end is Microsoft's architecture site, which offers a wealth of architecture tips for the .NET Framework and other Microsoft technologies.

Application Modeling

Today's enterprise-based systems are considerably more complex than their desktop counterparts. When one considers the components that such a system consists of—database(s), business logic, user presentation, security, auditing, external communication, and so forth—it becomes obvious that designing and implementing such a system is a daunting task.

Application modeling is an attempt to formalize and simplify how complex systems are designed. Properly stated, application modeling is the process of identifying and documenting (and possibly implementing) the logical and conceptual requirements of an application. This usually involves an examination of the application's data and programmatic relationships, and then subsequently the use of some of the aforementioned patterns to implement them.

One of the most popular techniques for application modeling is to use Universal Modeling Language (UML), a pictorial system to represent how processes and components interact with one another. A full explanation of the UML standard is beyond the scope of this booklet (there are numerous resources on the Web to this end, however).

Because UML diagrams are graphical, they are effective at communicating an application's "high-level" architecture. With UML, one can easily represent existing and business processes, application data structures, and user-input parameters. In other words, UML diagrams can easily represent what an application does and how users are to interact with it. As the application evolves, such diagrams can be successively tweaked and overhauled so as to represent the subsequent versions.

Microsoft Visio

The Enterprise Architect version of Visual Studio .NET comes packaged with Microsoft Visio. Visio is a powerful application that allows developers to use the UML standard (version 1.2) to perform application modeling. Furthermore, the product can generate C#, VB.NET, or C++ skeleton code based on such visual models (a technique referred to "forward engineering").

The following step-by-step example affords the reader some appreciation of Visio's modeling capabilities. Note that you must have Visio installed on your system.

Visio Modeling Example

In this example, we create an application model using the Unified Modeling Language (UML) and Microsoft Visio, and then generate C# code based on the model.

Part 1: Creating a use-case diagram

In the first part of this example we will create a use-case diagram for a car rental system.

Step 1. Creating a new use-case diagram

  1. Click on Start, point to Programs, and then click Microsoft Visio.
  2. Select File | New | Software | UML Model Diagram.
  3. In the Model Explorer window, double-click the Top Package Static Model subfolder. If the Model Explorer window is not visible, select UML | View | Model Explorer to toggle the display of the window.
  4. Set the Name property to Car Rental System. Click the OK button.
  5. Right-click on the Car Rental System package in the Model Explorer window, select New | Use Case Diagram. A Use Case Diagram with the name Use Case-1 should appear under the Car Rental System package.
  6. Right-click on the Use Case-1 diagram in the Model Explorer window, select Rename, and rename the diagram to Use Case Diagram.

Step 2. Adding use cases to the diagram

  1. Select and drag a System Boundary from the UML Use Case stencil onto the Use Case Diagram drawing page. If the stencil is not visible, select File | Stencils | Software | UML Use Case.
  2. Select 100% from the Zoom drop-down list box on the toolbar.
  3. Scroll to the top of the drawing.
  4. Double-click the System Boundary shape, and type Car Rental System to rename the shape.
  5. Click anywhere outside the text area to exit text entry mode.
  6. Select and drag a Use Case from the UML Use Case stencil into the System Boundary. Repeat this two more times. Arrange the Use Cases vertically.
  7. Double-click one of the use cases on the drawing page, and set the Name property to Reserve Car. Rename the other two Use Cases to Pick Up Car and Return Car.
  8. Select and drag an Actor from the UML Use Case stencil onto the drawing page outside the System Boundary.
  9. Double-click the Actor and set the Name property to Customer.
  10. Select and drag a Communicates shape from the UML Use Case stencil onto the drawing page. Repeat this two more times. The lines should appear with a red error marking to indicate they have not yet been connected properly.
  11. Select and drag one of the Communicates shapes until one end intersects a connection point on the Actor. Connection points are denoted by small blue "x" markings on the shape.
  12. Select the unconnected end of the Communicates shape, and drag the end to one of the connection points on a Use Case. The red error marking on the Communicates shape should disappear.
  13. Repeat the previous two steps for the other two Communicates shapes, but connecting to the remaining Use Cases.
    Note   When connecting multiple Communicates endpoints to the same connection point on the Actor, make sure that the Communicates shapes connect to the Actor and not to each other. You can increase the Zoom level to see more clearly where the connection is made.
  14. Press and hold Shift while selecting each Communicates shape; continue holding Shift and right-click, then select Shape Display Options....
  15. In the UML Shape Display Options dialog box, uncheck the First end name, Second end name and End multiplicities options.
  16. Click the OK button.
  17. Choose File | Save. For the File name field, type C:\CarRentalSystem.vsd. Click Save.
  18. Click the OK button for the properties dialog box.

    Aa302171.vsent_enterprisetemplatesbk26(en-us,MSDN.10).gif

    Figure 26. Use case diagram for Car Rental System

Part 2. Creating a sequence diagram

In the second part of this example, we create a sequence diagram for the car rental system defined in part 1.

Step 1. Adding a sequence diagram to the model

  1. Right-click on the Car Rental System package in the Model Explorer window; select New | Sequence Diagram. A Sequence Diagram with the name Sequence-1 should appear under the Car Rental System package.
  2. Right-click on the Sequence-1 diagram in the Model Explorer window, select Rename, and rename the diagram to Sequence Diagram.
  3. Select 100% from the Zoom drop-down list box on the toolbar.

Step 2. Adding lifelines to the sequence diagram

  1. Select and drag an Object Lifeline from the UML Sequence stencil onto the drawing page. The shapes should appear with a red error marking to indicate they have not yet been configured properly.
  2. Repeat the previous step two more times. Arrange the lifelines horizontally.
  3. Double-click the left-most Object Lifeline shape on the drawing page.
  4. Click New... to create a new Classifier, and set the Name property to Customer on the UML Class Properties dialog box. Click the OK button.
  5. Set the Name property to Customer on the UML Classifier Role Properties dialog box.
  6. Click the OK button. The red error markings should disappear.
  7. Repeat the previous four steps on the next Object Lifeline using RentalAgent for the name of the UML Class and Rental Agent for the name of the Classifier Role.
  8. Repeat for the last Object Lifeline using CarInventory for the name of the UML Class and Car Inventory for the name of the Classifier Role.
  9. Press and hold Shift while selecting each Object Lifeline shape, continue holding Shift and right-click, then select Shape Display Options....
  10. In the UML Shape Display Options dialog box, check the Destruction marker option.
  11. Click the OK button.

Step 3. Adding Activation rectangles to the sequence diagram

  1. Select and drag an Activation shape from the UML Sequence stencil onto the drawing page. The rectangle should be marked with red to indicate that it has not yet been bound to a lifeline.
  2. Select and drag the topmost endpoint of the Activation shape near the top of the Customer lifeline until it intersects a connection point.
  3. Connect the other end of the Activation shape to a connection point near the bottom of the Customer lifeline.
  4. Repeat the previous three steps to bind Activation shapes to the Rental Agent and Car Inventory lifelines.
    Note   You can make the lifelines longer by selecting the lifeline and resizing, using the yellow diamond grip near the bottom of the lifeline. You will have to also resize the activation rectangle by selecting it and using the resize handles at either end.

Step 4. Adding messages to the Sequence Diagram

  1. Select and drag a Message (call) from the UML Sequence stencil onto the drawing page. The line should appear with a red error marking because it has not yet been connected properly.
  2. Select and drag the Message (call) until the end without the arrowhead intersects a connection point on the activation rectangle of the Customer lifeline. The Message (call) should be placed near the top right side of the activation rectangle.
  3. Select and drag the arrowhead end of the Message (call) to intersect the activation rectangle of the Rental Agent lifeline. The red error marking on the Message (call) shape should disappear.
  4. Double-click the Message (call).
  5. Set the Name property to DoRental.
  6. Set the Return type to C#::int.
  7. Add the text Returns a Rental ID to the Documentation property.
  8. Click the OK button for the UML Operation Properties dialog and the UML Message Properties dialog box.
  9. Select and drag a Message (call) from the UML Sequence stencil onto the drawing page.
  10. Connect the endpoints to the Rental Agent activation rectangle and the Car Inventory activation rectangle. Place the message below the DoRental message.
  11. Double-click the Message (call) in the previous step and set the Name property to CheckAvailability.
  12. In the Categories pane, select Parameters, click New, and set the value under the Parameter column to CarType.
  13. Set the value under the Type column to C#::string using the droplist.
  14. Click the OK button on both dialog boxes.
  15. Select and drag a Message (return) shape from the UML Sequence stencil onto the drawing page.
  16. Connect the arrowhead endpoint to the Customer activation rectangle and the other endpoint to the Rental Agent activation rectangle. Place the message below the CheckAvailability(CarType) message.
  17. Double-click the Message (return) shape on the drawing and set the Name property to Approve Rental.
  18. Click the OK button.
  19. Press CTRL + S to save the changes.

    Aa302171.vsent_enterprisetemplatesbk27(en-us,MSDN.10).gif

    Figure 27. Sequence diagram for Car Rental System

Part 3: Creating a class diagram

In this third portion of the example, we create a class diagram for the car rental system previously defined.

Step 1. Adding a class diagram to the model

  1. Right-click on the Car Rental System package in the Model Explorer window; select New | Package.
  2. Set the Name property of the UML Package to Classes.
  3. Click the OK button.
  4. Right-click on the Classes package in the Model Explorer window; select New | Static Structure Diagram. A Static Structure Diagram with the name Static Structure-1 should appear under the Classes package.
  5. Right-click on the Static Structure-1 diagram in the Model Explorer window, select Rename, and rename the diagram to Class Diagram.
  6. Select and drag the CarInventory class under the Car Rental System package to the Classes package.
  7. Repeat the previous step for the Customer and RentalAgent classes.
  8. Select 100% from the Zoom droplist on the toolbar.
    Note   A separate Classes package was added to avoid naming conflicts between the Customer Use Case actor and the Customer class, both of which were under the Car Rental System package.

Step 2. Adding classes to the class diagram

  1. Select and drag the CarInventory class from the Model Explorer window to the Static Structure drawing page.
  2. Repeat the previous step for the Customer and RentalAgent classes.
  3. Double-click Customer, select Attributes in the Categories pane, and click New.
  4. Set the value in the Attribute column to Name.
  5. Set the value in the Type column to C#::string using the droplist.
  6. Set the value in the Visibility column to public using the droplist.
  7. Click the OK button.
  8. Select and drag a Binary Association shape from the UML Static Structure stencil onto the drawing page. The line should appear with a red error marking to indicate it has not yet been connected properly.
  9. Click and drag one end of the Binary Association to intersect a connection point on the Customer class. Connect the other end to the Rental Agent class.
  10. Repeat the previous two steps to connect the RentalAgent and CarInventory classes.
  11. Double-click the Customer class, set the Visibility to public.
  12. Click the OK button.
  13. Repeat the previous two steps for the RentalAgent and CarInventory classes.
  14. Hold Shift and select both Binary Association shapes. While holding Shift, right-click and select Shape Display Options....
  15. Uncheck the First end name, Second end name and End multiplicities options.
  16. Click the OK button.
  17. Press CTRL + S to save the changes.

    Aa302171.vsent_enterprisetemplatesbk28(en-us,MSDN.10).gif

    Figure 28. Sequence Diagram for Car Rental System

Part 4: Generating code from the model

In this last portion of the example, we generate C# code based on the existing Visio model.

Step 1. Generate code from the model

  1. Select UML | Code | Generate.
  2. Set Target language to C#.
  3. Set Project Location to C:\.
  4. Check Add Classes to Visual Studio Project.
  5. Select Class Library from the Template droplist.
  6. Check Create directory for solution.
  7. Check Static Model under Select classes for code generation.
  8. Click the OK button. If a dialog-box prompt asks whether you would like to create a new directory for the project, click Yes.

Step 2. Examining the code generated by Visio

  1. Open Windows Explorer and navigate to C:\CarRentalSystem.
  2. Double-click CarRentalSystem.sln to open the solution in Visual Studio .NET.
  3. In the Solution Explorer, double-click the various class files under the Classes folder.

    Notice that the attributes have been turned into public variables and the documentation for the DoRental method has been turned into a comment.

  4. Close Visual Studio .NET.

Conclusion

In this booklet, we have examined powerful tools and techniques to aid developers and architects in designing applications for the .NET Framework. The majority of the content highlighted the usage of Enterprise Templates, which prescribe the project skeleton users receive when they create a project with a template and the policy enforced within the Visual Studio .NET environment for a template. In essence an Enterprise Template consists of:

  • A project template that can contain both static and dynamic content (the latter which is generated by means of subproject wizards).
  • Policy, which consists of the environment restrictions of the Visual Studio .NET IDE. Policy is implemented by way of template's policy file, which is written in Template Description Language (TDL) or which can be created graphically using the Enterprise Template Policy Editor add-in.
  • Guidance, which consists of custom help files exposed to the Visual Studio .NET environment, either through HTML or compiled Microsoft Help files.

After a brief survey of some tangential topics, this booklet examined the corollary topic of architecture and modeling. The topic of architecture is centered upon the pattern concept, which describes a common problem that occurs within a given context and then posits a model to solve that problem. The Microsoft Patterns and Practices Web site offers several useful patterns that can be used with the .NET Framework.

Last, this booklet examined how to use Microsoft Visio to model an application using UML, a graphical standard to describe an application's architecture and design. Using Visio, developers can use the UML standard to for initial application design, and can then forward engineer the design into skeleton code for implementation purposes.

Show:
© 2014 Microsoft