Deriving NameFormatter to handle combining properties in .NET

Drawing Code 128 Code Set B in .NET Deriving NameFormatter to handle combining properties

Listing 18.13 Deriving NameFormatter to handle combining properties
Code 128A Printer In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create Code 128 Code Set A image in ASP.NET applications.
www.OnBarcode.com
Printing Data Matrix In .NET
Using Barcode encoder for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
www.OnBarcode.com
public class NameFormatter : BaseFormatter<Name> { protected override string FormatValueCore(Name value) { var sb = new StringBuilder(); if (!string.IsNullOrEmpty(value.First)) { sb.Append(value.First); } if (!string.IsNullOrEmpty(value.Middle)) { sb.Append(" " + value.Middle); } if (!string.IsNullOrEmpty(value.Last)) { sb.Append(" " + value.Last); } if (value.Suffix != null) { sb.Append(", " + value.Suffix.DisplayName); } return sb.ToString(); } }
Encode PDF-417 2d Barcode In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
www.OnBarcode.com
Code128 Generator In .NET Framework
Using Barcode creation for ASP.NET Control to generate, create Code 128C image in ASP.NET applications.
www.OnBarcode.com
Uses StringBuilder to craft output
Universal Product Code Version A Generation In VS .NET
Using Barcode printer for ASP.NET Control to generate, create UPC-A image in ASP.NET applications.
www.OnBarcode.com
QR Code Drawer In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create QR image in ASP.NET applications.
www.OnBarcode.com
Applies basic formatting logic
Draw Barcode In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Code 93 Full ASCII Maker In VS .NET
Using Barcode creator for ASP.NET Control to generate, create USS-93 image in ASP.NET applications.
www.OnBarcode.com
Summary
Code 128 Code Set A Recognizer In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Encode Code 128A In None
Using Barcode encoder for Software Control to generate, create USS Code 128 image in Software applications.
www.OnBarcode.com
Harnessing AutoMapper allows the developer to write this code once and apply it in many places with just a declaration. When configured like the profile in listing 18.8, this formatter will be applied to all source members of type Name.
Scanning EAN-13 Supplement 5 In Visual Studio .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Print UCC - 12 In Java
Using Barcode creation for Eclipse BIRT Control to generate, create USS-128 image in BIRT reports applications.
www.OnBarcode.com
18.3.5 Another look at our views
QR-Code Decoder In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Generate QR Code In Visual Studio .NET
Using Barcode encoder for Reporting Service Control to generate, create QR Code image in Reporting Service applications.
www.OnBarcode.com
With our configuration complete, our markup is focused only on layout. The tedious logic from listing 18.3 has been replaced. Listing 18.14 shows the resulting view.
GS1 - 13 Creator In .NET
Using Barcode creation for .NET framework Control to generate, create EAN / UCC - 13 image in .NET framework applications.
www.OnBarcode.com
Creating UPC Symbol In Objective-C
Using Barcode creation for iPad Control to generate, create UPCA image in iPad applications.
www.OnBarcode.com
Listing 18.14 The final view markup
QR Code JIS X 0510 Creation In None
Using Barcode encoder for Software Control to generate, create QR Code ISO/IEC18004 image in Software applications.
www.OnBarcode.com
Code39 Creator In Objective-C
Using Barcode generator for iPad Control to generate, create Code 3/9 image in iPad applications.
www.OnBarcode.com
<h2>Customer: <%= Model.Name %></h2> <div class="customerdetails"> <p>Status: <%= Model.Status %></p> <p>Total Amount Paid: <%= Model.TotalAmountPaid %></p> <p>Address: <%= Model.ShippingAddress %></p> </div>
Paint Matrix 2D Barcode In .NET
Using Barcode generator for .NET Control to generate, create 2D image in Visual Studio .NET applications.
www.OnBarcode.com
Create USS Code 128 In None
Using Barcode generation for Office Word Control to generate, create Code 128C image in Microsoft Word applications.
www.OnBarcode.com
18.4 Summary
In this chapter, we looked at how views can quickly become unmanageable when they re filled with logical checks and formatting that s best handled elsewhere. We first tried manually mapping custom presentation models, which worked well but is tedious and error prone. We then looked at AutoMapper, which maps values from one object to another according to its configuration. We saw how to initialize and configure AutoMapper, how to follow the conventions, and how to leverage AutoMapper hooks to globally apply formatting. In the next chapter, we ll look at how to keep controllers lightweight and under control. By striving to reduce duplication and eliminate developer friction, we ll craft small and targeted controller actions.
Lightweight controllers
This chapter covers
Using lightweight controllers to simplify programming Managing common view data without filter attributes Deriving action results to apply common behavior Using an application bus
Do you remember those swollen and unwieldy Page_Load methods in Web Forms Those methods can quickly grow out of control and stage a revolt against your code base. Controller actions are dangerous too. Nestled snugly between the model and view, controllers are an easy place to put decision-making code, and they re often mistaken for a good place to put that logic. And it s quite convenient, at first. It just takes two lines of code to build a select list in an action method. And adding a filter attribute to the controller is a simple way to manage global data for a master page. But these techniques don t scale with greater complexity. Orchestrating a process to find a particular order, authorize it, transmit it to the shipping service, and email a receipt to the user, before redirecting the client to the confirmation page That s too much for our controller to handle.
Why lightweight controllers
19.1 Why lightweight controllers
It s important to focus on keeping controllers lightweight. Over time, controllers tend to accumulate more code, and large controllers that have many responsibilities are hard to maintain. They also become hard to test. When creating controllers, think about long-term maintainability, testability, and a single responsibility.
19.1.1 Maintainability As code becomes hard to understand, it becomes hard to change; as code becomes hard to change, it becomes a minefield of errors and rework and headaches. Deep technical analysis must be rendered for each seemingly simple enhancement or bug fix, because the developer is unsure what the ramifications of a given change will be. The single responsibility principle (SRP)
The guiding principle behind keeping a class small and focused is the single responsibility principle (SRP). Basically, SRP states that a class should have one and only one responsibility. Another way to look at it is that a class should have only one reason to change. If you find that a class has the potential to be changed for reasons unrelated to its primary task, that means the class is probably doing too much. A common violation of SRP is mixing data access with business logic. For example, a Customer class probably shouldn t have a Save() method. SRP is a core concept of good object-oriented design, and its application can help your code become more maintainable. SRP is sometimes referred to as separation of concerns (SoC). You can read more about SRP/SoC in Bob Martin s excellent article on the subject, SRP: The Single Responsibility Principle (http://mng.bz/34TU).
Not only that, but bloat makes understanding how to make a change difficult. Without clear responsibilities, a change could potentially happen anywhere. As developers, we don t want building software to be a guessing game in which we blindly slap logic into action methods. We want to create a system in which software design exists apart from controllers so that we don t struggle when working with our source code.
19.1.2 Testability The best way to ensure it s easy to work with our source code is to practice test-driven development (TDD). When we do TDD, we work with our source code before it exists. Hard-to-test classes, including controllers, are immediately suspect as flawed. Testing friction problems writing tests or with test management is a clear and convincing indicator that the software s design has room for improvement. Simple, lightweight controllers are easy to test. We ll discuss TDD in detail in chapter 26. 19.1.3 Focusing on the controller s responsibility
A quick way to lighten the controller s load is to remove responsibilities from it. Consider the burdened action shown in listing 19.1.
Copyright © OnBarcode.com . All rights reserved.