240 likes | 373 Views
.NET Harmony. A look into efficiencies with EF Code First, Web API, & OData. Introduction. Travis Gosselin Senior Solutions Architect @ ToolBox Solutions Inc. 7 Years Development Experience in .NET Enjoy Working in the Full Stack of .NET
E N D
.NET Harmony A look into efficiencies with EF Code First, Web API, & OData
Introduction Travis Gosselin Senior Solutions Architect @ ToolBox Solutions Inc. 7 Years Development Experience in .NET Enjoy Working in the Full Stack of .NET … but I do have a special place in my heart specifically for Web Development
Goals & Outline • .NET Harmony • Entity Framework • MVC 4 • ASP.NET Web API • REST • OData • Entity Framework Code First • ASP.NET Web API • OData • Integration & Consumption *Focusing on concept introductions, with attention to integration & consumption.
Entity Framework Introduction • Entity Framework is an ORM (Object Relational Mapper) • EF increases developer productivity by providing simple pre-generated data access components, accessible from a LINQ syntax.
Entity Framework • Open Source as of EF 5 (August 2012) – Current Version • Works with any relational database • Available Types: • Database First • Model First • Code First**
Entity Framework Code First • EF Code First was introduced in EF 4.1 • Gets rid of the nasty EF Designer & the need for undesirable meta data and code generation only files (they may be POCO but they don’t quite cut it in EF DB First) • Convention Over Configuration (if you want) • Limitations: • Stored Procedure Support (available in EF6) • Enumeration Support (available in EF6)
DEMO: Entity Framework Code First A Simple Music Database
ASP.NET Web API “ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework” (asp.net/web-api/). • Simply: Building pure HTTP based endpoints without hassle • MVC4 (.NET 4.5) & Nuget Packages (*Available in ASP.NET Web Forms) • Take advantage of HTTP features • Uses HTTP verbs / actions by convention (or go custom) • Makes use of pure HTTP return codes • Content Negotiation (JSON, XML, & Custom) • Model binding & formatting
Why Web APIwith .SVC & .ASMX Around? • WCF Web API development turned into ASP.NET Web API • Provides pure HTTP endpoints for all application types (HTML5, mobile, desktop) • A conventional way for accessing data without WCF configurations & Contracts • Flexibility to create any type of web services: • RPC, CRUD, REST, OData • .SVC is still your choice on .NET 3.5 or you require SOAP based • Web API offers content negotiation • RPC vs REST? Am I doing it wrong if I’m not using REST?
REST = REpresentational State Transfer • URIs – can access all components via direct URI • Uniform Interface - GET (Retrieve), POST (Create), PUT (Update), DELETE (Delete) • Stateless – No state / context between requests. • Hypermedia (links) – allows expansion and navigation • Cache-ability – certain verb types define themselves as cacheable *REST is a style, SOAP is a protocol
REST = REpresentational State Transfer http://www.restapitutorial.com/lessons/httpmethods.html
OData “ODatais a standardized protocol for creating and consuming data APIs. OData builds on core protocols like HTTP and commonly accepted methodologies like REST. The result is a uniform way to expose full-featured data APIs” (odata.org). • Simply: Standardized method of accessing rich data APIs. • Open Microsoft Initiative • REST based • Provides metadata descriptions of entire API • Has its own OData querying syntax that gets used on the URI
OData Query Syntax • http://service.com/simple?$orderby=Name • http://service.com/simple?$top=10 • http://service.com/simple?$skip=2 • http://service.com/simple?$filter=Name eq ‘Travis' • http://service.com/simple?$filter=substringof(‘rav', Name)
WCF Data Services VS Web API OData Web API OData WCF Data Services Was available here first Most complete OData Support Full Exposure of Data Stores without Business Logic No immediate Content Negotiation • Limited OData support on launch • Simple integration with custom service layers & logic • Easy to use with non-LINQ provider data • Mix OData with other Service Types • Short release cycle on updates • Extensibility • Supports most important OData features • Does not easily support hypermedia linking
DEMO: Further Investigation into better OData in Web API BreezeController & EntitySetController
Client Integration with OData • Use new HttpClient for Web API • What about WebClient in .NET 2.0? … HttpClient offers more flexibility to pure HTTP • WebClient is akin to ASMX as HttpClient is akin to Web API • JavaScript: Breeze, DataJS, JayData • .NET / Silverlight / Windows Phone / WinRT – WCF Data Services Client • iOS– OData4ObjC • Android – OData4j
Final Thoughts • Things to investigate: • Web API Security will work with ASP.NET Authorization & Security • Web API Formatters & MediaTypeFormats Provide Ultimate Flexibility • Further investigation around the EntitySetController • Web API provides ultimate flexibility in most scenarios if you don’t require pure OData. • Simple straightforward exposure of OData from WCF Data Services if you have no business logic. • Thin slice your server code for JS Heavy SPAs by exposing your data store with OData • Web API conventions & simplicity are very attractive
Deck & Examples (VS2012.2) • Available at Blog: www.travisgosselin.com/blog/?p=370 • Will be available soon on GitHub site. • Questions?