qr code c# source DECOUPLING THE MODEL FROM THE PRESENTER in Visual C#.NET

Making QR Code in Visual C#.NET DECOUPLING THE MODEL FROM THE PRESENTER

DECOUPLING THE MODEL FROM THE PRESENTER
Encoding QR Code JIS X 0510 In C#
Using Barcode generation for .NET framework Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
www.OnBarcode.com
Reading QR Code In Visual C#
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
The current design ties the Model to the Presenter because the LoadLists method instantiates the Model and uses the methods directly. By changing the Model class to inherit from an interface that expresses the Model s functionality, you can decouple the implementation of the Model from the Presenter:
Print EAN 13 In Visual C#
Using Barcode maker for VS .NET Control to generate, create EAN / UCC - 13 image in VS .NET applications.
www.OnBarcode.com
Creating Linear Barcode In C#.NET
Using Barcode printer for VS .NET Control to generate, create 1D image in VS .NET applications.
www.OnBarcode.com
public interface IListsService { IEnumerable<ListInfo> GetLists(); }
Code 128C Generation In Visual C#
Using Barcode printer for VS .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
QR Code Creator In C#.NET
Using Barcode generator for .NET framework Control to generate, create Denso QR Bar Code image in .NET applications.
www.OnBarcode.com
Once that s done, you edit the ListsService class definition so that it inherits from the new Model interface as follows:
Generate ANSI/AIM Code 39 In C#
Using Barcode drawer for VS .NET Control to generate, create Code39 image in Visual Studio .NET applications.
www.OnBarcode.com
British Royal Mail 4-State Customer Code Generation In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create British Royal Mail 4-State Customer Barcode image in VS .NET applications.
www.OnBarcode.com
class ListsService : IListsService
Painting QR Code In None
Using Barcode maker for Software Control to generate, create QR Code image in Software applications.
www.OnBarcode.com
Recognizing QR Code JIS X 0510 In Visual Studio .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
The Model-View-Presenter pattern
Code-39 Drawer In None
Using Barcode generation for Software Control to generate, create USS Code 39 image in Software applications.
www.OnBarcode.com
UPC - 13 Generator In None
Using Barcode generation for Software Control to generate, create EAN / UCC - 13 image in Software applications.
www.OnBarcode.com
Now there s a contract for how the Model is implemented, through the interface, and you can use dependency injection to provide multiple Models to the same Presenter and View. For instance, the current ListsService Model returns all lists within a site. If you need another Model, called LibraryService, that returns all document libraries, you can create a new Model implementation that has a GetLists method like this:
Make Code 128 Code Set B In Java
Using Barcode generator for BIRT reports Control to generate, create Code128 image in BIRT reports applications.
www.OnBarcode.com
EAN13 Generation In Java
Using Barcode creation for Java Control to generate, create EAN-13 image in Java applications.
www.OnBarcode.com
public IEnumerable<ListInfo> GetLists() { SPWeb web = SPContext.Current.Web; List<ListInfo> listInfo = new List<ListInfo>(); foreach (SPList list in web.GetListsOfType(SPBaseType.DocumentLibrary)) { listInfo.Add(new ListInfo() { Title = list.Title, Url = list.DefaultViewUrl }); } return listInfo; }
DataMatrix Creator In None
Using Barcode generator for Font Control to generate, create ECC200 image in Font applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
You must modify the Presenter implementation to support the dependency injection. Simply add a new local variable to the Presenter class and modify the constructor to support injection of the Model:
Encoding GS1 - 12 In .NET Framework
Using Barcode creator for Reporting Service Control to generate, create UPC-A Supplement 5 image in Reporting Service applications.
www.OnBarcode.com
Create QR Code In None
Using Barcode drawer for Word Control to generate, create QR-Code image in Office Word applications.
www.OnBarcode.com
class ListsPresenter { protected readonly IListsView _view; protected readonly IListsService _model; public ListsPresenter(IListsView view) { _view = view; _model = new ListsService(); } public void LoadLists() { _view.Lists = _model.GetLists(); } }
Barcode Maker In Objective-C
Using Barcode creation for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Encode Barcode In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
The IListsService is stored as an immutable variable in the Presenter class. The constructor is modified so that it sets the injected View and then creates one of the Model implementations. Finally, the LoadLists method is modified to use the Model interface instead of creating a Model class, as the previous implementation did. By using this pattern with decoupled objects, you can easily replace, change, or duplicate almost any part of the Web Part without affecting the other parts. Using the Visual Studio 2010 class diagram designer, the solution looks like figure 13.6.
Design patterns and testability
Figure 13.6 The Class Diagram designer in Visual Studio 2010 is a great tool to use when working with decoupled objects to visualize the system design.
13.3 SharePoint Service Locator
In a fully decoupled system, you want to write code that isn t dependent on the concrete implementation of the classes. To accomplish this goal, you can use a repository that stores mappings between the service interfaces and the service implementations. A service locator is such a repository; it handles registrations of types and maps them to one or more implementations. The Microsoft Patterns & Practices group has created a base service locator implementation and a specific service locator for SharePoint. The SharePoint Service Locator allows developers to store different type-mappings at different levels in SharePoint, such as farms or site collections. That way, your service can use different Models or Views depending on where it s located or depending on what features have been enabled at the current level.
SharePoint Service Locator
13.3.1 Service locators The problem with this implementation uses Service1 of the Web Part is that you re referencing a Model implementation in the code of the Presenter. To switch to another ClassA Model, the Web Part must be compiled and redeployed to SharePoint not a uses Service2 very flexible solution. It s also hard to test the Presenter in isolation using unittests because it depends on the Model, as you can see in figure 13.7. If you d like Figure 13.7 Using multiple services for a class to dynamically switch between different requires custom code that handles the request for the correct service during runtime. Model implementations, you need to provide logic for that. A service locator is a central repository that contains references to services and the different implementations of the services. Services are registered in the service locator, and classes can query the service locator for a specific service to get an instance of it, as you can see in figure 13.8.
Copyright © OnBarcode.com . All rights reserved.