how to generate barcode in c# web application Structuring and testing with the MVVM/ViewModel pattern in Visual Basic .NET

Generator QR in Visual Basic .NET Structuring and testing with the MVVM/ViewModel pattern

Download from Wow! eBook <www.wowebook.com>
Make QR Code JIS X 0510 In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create Denso QR Bar Code image in VS .NET applications.
www.OnBarcode.com
Scan QR Code JIS X 0510 In VB.NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Structuring and testing with the MVVM/ViewModel pattern
QR Code Creation In VB.NET
Using Barcode encoder for VS .NET Control to generate, create QR image in .NET applications.
www.OnBarcode.com
1D Barcode Printer In VB.NET
Using Barcode creator for VS .NET Control to generate, create Linear 1D Barcode image in .NET applications.
www.OnBarcode.com
In this section, you ll do a little refactoring to make the ViewModel class a bit lighter and allow reuse of code, starting with the business logic to add the vacation bonus.
Encoding Matrix In VB.NET
Using Barcode maker for .NET framework Control to generate, create Matrix image in VS .NET applications.
www.OnBarcode.com
Draw Code 128 Code Set C In VB.NET
Using Barcode generator for VS .NET Control to generate, create Code 128 image in .NET applications.
www.OnBarcode.com
16.3.1 Business rules and logic
EAN 13 Encoder In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create EAN13 image in VS .NET applications.
www.OnBarcode.com
Bookland EAN Drawer In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create International Standard Book Number image in .NET applications.
www.OnBarcode.com
The easiest thing to pull out of the EmployeeListViewModel is the code that adds the vacation bonus. You can deal with this several ways I prefer using a service approach. That is, rather than bake the bonus into a special employee class, you have a service you can call that deals with bonuses using a simple function call. This is distinct from the idea of a web service. Create a new folder named Services in the Silverlight client. In that folder, add a class named EmployeeVacationBonusService. Listing 16.10 shows the code for this class.
QR Code Maker In Java
Using Barcode printer for Android Control to generate, create QR-Code image in Android applications.
www.OnBarcode.com
QR-Code Generator In None
Using Barcode creation for Microsoft Word Control to generate, create Denso QR Bar Code image in Office Word applications.
www.OnBarcode.com
Listing 16.10 The EmployeeVacationBonusService class
Generate Barcode In Visual Studio .NET
Using Barcode maker for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Create UPCA In None
Using Barcode creator for Online Control to generate, create UCC - 12 image in Online applications.
www.OnBarcode.com
public class EmployeeVacationBonusService { public static void AddVacationBonus(Employee employee) { int vacationBonus; DateTime dateOfHire = employee.HireDate; DateTime today = DateTime.Today; int yearsInService = today.Year - dateOfHire.Year; if (dateOfHire.AddYears(yearsInService) > today) yearsInService--; if (yearsInService < 5) vacationBonus = 10; else if (yearsInService < 10) vacationBonus = 20; else if (yearsInService < 20) vacationBonus = 30; else vacationBonus = 40; employee.VacationHours += vacationBonus; } }
EAN128 Generation In None
Using Barcode generation for Font Control to generate, create EAN 128 image in Font applications.
www.OnBarcode.com
Creating ANSI/AIM Code 128 In .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
www.OnBarcode.com
Dependency on Employee
Generating GS1 DataBar Stacked In Visual Studio .NET
Using Barcode maker for VS .NET Control to generate, create GS1 DataBar Truncated image in .NET framework applications.
www.OnBarcode.com
Painting EAN-13 In C#.NET
Using Barcode encoder for .NET Control to generate, create EAN-13 Supplement 5 image in .NET applications.
www.OnBarcode.com
The vacation bonus algorithm has been beefed up. Rather than a blanket 10 hours, you use some of the data to reward those with the longest time at the company. You also implement the functionality using static methods here. Some developers prefer to use instance methods. Either way is fine as long as you understand why you re doing it and what flexibility you lose when going with static methods (such as the ability to mock), and you have some consistency to your decisions. There are also multiple ways you can model this class. For example, it could modify the class directly as shown here or it could return a bonus amount based on a set of
Matrix 2D Barcode Creation In VS .NET
Using Barcode creator for ASP.NET Control to generate, create 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
Barcode Generation In .NET
Using Barcode generation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Download from Wow! eBook <www.wowebook.com>
PDF 417 Drawer In None
Using Barcode encoder for Word Control to generate, create PDF417 image in Word applications.
www.OnBarcode.com
Drawing PDF 417 In None
Using Barcode encoder for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Factoring out reusable code
parameters such as current vacation hours, date of hire, some sort of level information, and so on. Taking in individual parameters like that, rather than passing in an Employee object, helps reinforce the SRP and decouple from the rest of the system because the class no longer needs to be changed if the Employee class changes. Listing 16.11 shows the final version of this service, taking individual parameters rather than the Employee object.
Listing 16.11 A better version of the EmployeeVacationBonusService class
public class EmployeeVacationBonusService { public static int GetVacationBonus(DateTime dateOfHire) { int vacationBonus; DateTime today = DateTime.Today; int yearsInService = today.Year - dateOfHire.Year; if (dateOfHire.AddYears(yearsInService) > today) yearsInService--; if (yearsInService < 5) vacationBonus = 10; else if (yearsInService < 10) vacationBonus = 20; else if (yearsInService < 20) vacationBonus = 30; else vacationBonus = 40; return vacationBonus; } }
In this version, it s the responsibility of the calling code to add the bonus to whatever employee class it happens to be working with. That removes the dependency from this class and makes it reusable in places where you may have different employee entities or perhaps just a few key fields. With that change made, the EmployeeListViewModel code to add the employee vacation bonus now looks like this:
public void AddVacationBonusToSelectedEmployee() { if (SelectedEmployee != null) { SelectedEmployee.VacationHours += (short)EmployeeVacationBonusService.GetVacationBonus( SelectedEmployee.HireDate); } }
The EmployeeListViewModel class is no longer responsible for calculating the vacation bonus. That s one extra responsibility down. Now, let s look at that web service logic.
Download from Wow! eBook <www.wowebook.com>
Structuring and testing with the MVVM/ViewModel pattern
16.3.2 Data access and service calls
In Silverlight, it s a given that data will come from a web service. Or will it Who says the data can t come from reading a local file in elevated trust mode, or from isolated storage Perhaps with the new Elevated Trust mode, one of the pure-.NET SQL databases will be an option. Plus, for all you know, a future version of Silverlight may have local database access built in. If every ViewModel class in the project is making a web service call to get the data, that means you have to change each and every one of them if anything about the service call changes obviously, not great design. In a small project like this, it s not a huge problem; but when you get into an application with dozens of pages and ViewModels, it gets pretty ugly. I ve seen lots of great examples of how to abstract service or data access calls away from the rest of the application. Some use singleton classes with names like ApplicationData to host a number of collections and load functions, with built-in caching. Others use individual classes, each responsible for a specific type of data. Others use combinations of the two ideas, but with no singleton involved so dependency injection works better. I m not going to weigh in on the merits of the various approaches; I don t think there s a one-size-fits-all solution. Instead, you ll create a simple example to solve just the problem at hand. The version I m showing doesn t support cross-view data caching, because you d need to keep an instance of the data service alive in a locator class or an IoC container. In the Services folder, add a new class named EmployeeDataService. The code for this class is shown in listing 16.12
Copyright © OnBarcode.com . All rights reserved.