Vous êtes sur la page 1sur 36

,

SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Building Server Applications


with SOA and WebAPI

Doncho Minkov
Technical Trainer
http://minkov.it
Telerik Software Academy
http://academy.telerik.com

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

Table of Contents

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Repository Pattern

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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(){ }
}

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Repository Pattern


Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Repository Pattern in
WebAPI

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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 IPlacesRepository repository;
public PlacesController()
{
this.repository = new DbPlacesRepository();
}
public IEnumerable<PlaceDto> GetAll()
{
return this.repository.GetAll();
}
}

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Repository Pattern in
WebAPI
Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Inversion of Control (IoC)

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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;
}

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Inversion of Control (IoC)


Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Application Layers

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Application Layers

A server application is build from three layers


A data layer contains
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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Data Source

Application Layers
MS SQL, MySQL,
XML, Web services

Data Layer

Repositories

Entity Framework,
OpenAccess,
Linq-to-XML

Services Layer
(Business layer)

WebAPI controllers

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Places Database


Creating a Sample Application

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Database

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

The Database

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Creating The Database


Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Data Layer

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Creating the Data Layer


Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Repositories Layer

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Creating the
Repositories Layer
Live Demo

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

The Services Layer

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

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

,



SEO -
,
, HTML, CSS, JavaScript, Photoshop
free C# book, C#, Java, C#
" "
" cloud "

BG Coder - - online judge


,
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
,
iPhone, Android, WP7, PhoneGap
-
-
C# , ,

Building Server Apps with


WebAPI

Vous aimerez peut-être aussi