Dialogue You can use layer diagrams to design your application's architecture and verify whether your code aligns with the ...

Dialogue
You can use layer diagrams to design your application's architecture and verify whether your code aligns with the design. You can track work associated with your models by linking Team Foundation Server work items to model elements. This provides traceability between the models, the IDE, and the code, whether you are designing a new solution or updating an existing solution. 

Let's start looking at an application's architecture by reviewing its layer diagram. 

The team‘s architect has suggested refactoring part of this application so that it aligns with the design on the layer diagram. This task is in our backlog and is linked to the layer diagram.



Action
In Team Explorer, under My Work, double-click the task work item to open it. 

In the work item, click Other Links.

Under Model Link, double-click the "model link", which opens the Petshop.layerdiagram in the PetShopModels project.

Dialogue
A layer diagram shows the permitted dependencies between namespaces, which are mapped to layers. Layer diagrams are very similar to drawing on a whiteboard. They have shapes and relationships between these shapes. By having layer diagrams in your solution, you can do more than just describe the intended architecture. You can validate your code against a layer diagram to discover whether invalid dependencies exist. By including this step as part of your check-in and build process, you can more easily discover changes in your code that conflict with the design.



Action
On the Architecture menu, choose Windows, Layer Explorer.

On the layer diagram, click the Business layer. Look at the artifacts in Layer Explorer.

On the layer diagram, right-click the Business layer. Choose Validate Architecture. (this might take a while)

In the Error List window, look at the errors (each marked by a red X). Double-click the first error to see the code that produced this error.

Dialogue
We found some dependencies in the code that conflict with the design. The Insert method in the Order class makes invalid calls to methods of two other classes. On the layer diagram, the Insert method is mapped to the Business layer, which should not communicate directly with the layers for the other two classes. The Business layer should communicate with the PaymentGateway layer instead.

Layer validation showed us what is wrong with the code, so when we fix these conflicts, revalidating the diagram should confirm we did the right thing.



Action
On the layer diagram, click the PaymentGateway layer.

In the Layer Explorer window, see the PaymentGatway namespace.

In Solution Explorer, expand the PaymentGateway project, the PaymentProcessor type, and then review the member methods.

Open the PaymentProcessor.cs file to see the member method definitions.

Dialogue
Looking at the code that is mapped to the PaymentGateway layer, we can delegate some of the Insert method functionality to the ProcessPayment method. Let's create sequence diagrams so we can visually compare the high-level structures and call sequences for these two methods.



Action
Double-click the first error to view the code. We see the definition of the Insert method.

Right-click anywhere inside the Insert method definition. Choose Generate Sequence Diagram. Keep all the proposed options. Click OK.

The sequence diagram shows that the Insert method calls methods for the CreditCardService class and the OnlinePaymentService class.

In the PaymentProcessor.cs file, right-click inside the ProcessPayment method definition. Choose Generate Sequence Diagram. Keep all the proposed options. Click OK.

Flip back and forth between both sequence diagrams and compare which classes are targeted by the Insert method and the ProcessPayment method and which classes are not. Show the similarities and differences.

Dialogue
We see these two methods are very similar with a few differences. Let's refactor the Insert method to call ProcessPayment. We'll then run validation again to make sure our changes resolved the dependency conflicts. 



Action
In the Insert method definition, replace the code between the following statements:

if (order.CreditCard.CardType == "CreditCardProviderA" || order.CreditCard.CardType == "CreditCardProviderB") 

and:

CalculatePaymentInfo(order);

with the following code:

PaymentGateway.PaymentProcessor processor = new PaymentGateway.PaymentProcessor();
if (!processor.ProcessPayment(order))
{
          return OrderStatus.Failed;
}


Validate the architecture.




Dialogue

In this video, we showed how layer diagrams help you describe an application's intended architecture and how you can use layer validation to see where your code conflicts with the design. We also showed how you can use sequence diagrams to see the method flow across different classes. To learn about using UML models to describe your architecture, see our related video.