1 / 36

Building Server Applications with SOA and WebAPI

Building Server Applications with SOA and WebAPI. Doncho Minkov. Telerik Software Academy. http://academy.telerik.com. Technical Trainer. http://minkov.it. Table of Contents. Application Layers Data Layer Repositories Layer Services Layer The repository pattern

thea
Download Presentation

Building Server Applications with SOA and WebAPI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building Server Applications with SOA and WebAPI Doncho Minkov Telerik Software Academy http://academy.telerik.com Technical Trainer http://minkov.it

  2. Table of Contents • Application Layers • Data Layer • Repositories Layer • Services Layer • The repository pattern • Creating repositories to unify database interactions • Inversion of Control and dependency Injection • Using DependencyResolver

  3. The Repository Pattern

  4. The Repository Pattern • The repository pattern wraps the access to data stores (like databases, XML, services…) • Exposes only interfaces to interact with a data store • Used for higher code-reusability and testability • The app's business layer contains a single instance of a repository • Used to perform CRUD operations over the data store

  5. The Repository Pattern: Example public interface IRepository<T> { T Add(T item); IEnumerable<T> GetAll(); … } public interface IPlacesRepository : IRepository<PlaceDto> { } public class DbPlacesRepository: IPlacesRepository { public PlaceDto Add(PlaceDto Add){ … } public IEnumerable<PlaceDto> GetAll(){ … } }

  6. The Repository Pattern Live Demo

  7. Why These Repositories? • Repository pattern has the f0llowing features: • Testability of the application • When the testing of the repositories is done, they can be mocked to test the business layer • Reusability or the repositories • When creating a new business layer working with the same Database (like admin panel) • Extensibility of the business layer • In need of more repositories, they can be easily produced

  8. Repository Pattern in WebAPI

  9. Repository Pattern in WebAPI • To use the repository pattern in WebAPI just create an instance of the IRepository<T> interface inside the controller • And use the repository to interact with the DB public class PlacesController : ApiController { private IPlacesRepositoryrepository; public PlacesController() { this.repository = new DbPlacesRepository(); } public IEnumerable<PlaceDto> GetAll() { return this.repository.GetAll(); } }

  10. Repository Pattern in WebAPI (2) • Yet in the example the controller instantiates the repository • The controller is tightly coupled with the DbPlacesRepository class • This can be fixed using Inversion of Control and Dependency Injection

  11. Repository Pattern in WebAPI Live Demo

  12. Inversion of Control (IoC)

  13. Inversion of Control (IoC) • Inversion of Control is a way to loosen the coupling between components • Makes testing easier • Makes extensibility easier • IoC is a technique that allows coupling of objects at run time, instead of compile time • IoC gives objects the dependencies they need • If the controller expects an instance of the IRepository<PlaceDto> interface, the IoC gives it a suitable instance

  14. IoC Implementations • There are many ways to implement IoC: • Factory design pattern • Service locator pattern • Dependency injection • Template method design pattern • Strategy design pattern • Etc… • WebAPI has built-in dependency injection for controllers

  15. Dependency Injection • Dependency injection removes the hard-coded dependencies between objects and allows changing them run time • The primary idea behind DI is selection of single implementation of interface between many present • Decide run time which of the many implementations of IRepository<PlaceDto> to use for the controller

  16. Dependency Injection in WebAPI • WebAPI has by default a dependency injector for controllers, called DependencyResolver • Instantiates controllers with their default constructor • Yet this can be changed to instantiate another constructor • Each WebAPI application has a DependecyResolver, deciding how to initialize controllers

  17. DependencyResolver • The dependency resolver of a WebAPI app can be changed with a custom implementation • Create a class that inherits from IDependencyResolver and implement its GetService() method: public class DbDependencyResolver: IDependencyResolver { static IRepository<Place> placesRep= new DbPlacesRepository(); public object GetService(Type serviceType) { if (serviceType == typeof(PlacesController)) return new PlacesController(placesRepository); else return null; } … }

  18. Inversion of Control (IoC) Live Demo

  19. Application Layers

  20. Application Layers • A server application is build from three layers • A data layercontains • Access to the data source (database, XML, etc…) • EntityFramework DbContext, XDocument, etc… • A repositories layer contains • Repositories with CRUD operations over a DB • A service layer contains • A reference to a repositories layer • A reference to the data layer • Controllers with actions for a REST API

  21. Application Layers MS SQL, MySQL, XML, Web services The Data Source Data Layer Repositories Entity Framework, OpenAccess, Linq-to-XML Services Layer(Business layer) WebAPI controllers

  22. The Places Database Creating a Sample Application

  23. The Places Database • Develop an application to store places of interest • Each place has coordinates, name, a set of categories and optional description • Every user can leave a comment or vote for a place • The user needs to type in their username • No user authentication required • Categories have a name and set of places

  24. The Database

  25. The Database • Create a database for the application • i.e. using MS SQL Server • Create tables, relations, schema, etc… • Create store procedures and indexes • Create everything needed for an app database

  26. Creating The Database Live Demo

  27. The Data Layer

  28. The Data Layer • The data layer contains a way to connect to the database • Entity Framework • Database-first or Code-first • ADO.NET • LINQ-to-SQL • LINQ-to-XML • Etc…

  29. Creating the Data Layer Live Demo

  30. The Repositories Layer

  31. The Repositories Layer • The repository layer exposes repositories to work with the Database • Using the Repository Pattern • The repositories introduce methods to perform CRUD operations over the data store • In our case over the Places database

  32. Creating the Repositories Layer Live Demo

  33. The Services Layer

  34. The Services Layer • The Services layer is the layer that contains the business logic • It uses the repositories for data interactions • Yet has no direct dependency over the data store • The Services layer contains all the controllers that are used by the Service Consumer • Handles computing and error handling

  35. The Services Layer • The Services Layer is the place where ASP.NET WebAPI steps in • It is the only layer that is dependent to the WebAPI framework • The Services layers uses the repositories to interact with the Data store and WebAPI to communicate with the Consumers • Each controller has a repository instance for data store interactions • The repository instances are received by IoC

  36. Building Server Apps with WebAPI

More Related