alexanderzeitler.com - REST using the WCF Web API – refactoring to IoC style (Part 3)









Search Preview

REST using the WCF Web API – refactoring to IoC style (Part 3) | Alexander Zeitler

alexanderzeitler.com

.com > alexanderzeitler.com

SEO audit: Content analysis

Language Error! No language localisation is found.
Title REST using the WCF Web API – refactoring to IoC style (Part 3) | Alexander Zeitler
Text / HTML ratio 65 %
Frame Excellent! The website does not use iFrame solutions.
Flash Excellent! The website does not have any flash contents.
Keywords cloud = public WCF Web API REST Part ContactsResource Id Contact List class return ContactRepository container Scott database inside code application
Keywords consistency
Keyword Content Title Description Headings
= 25
public 20
WCF 16
Web 16
API 16
REST 13
Headings
H1 H2 H3 H4 H5 H6
1 0 2 0 0 0
Images We found 1 images on this web page.

SEO Keywords (Single)

Keyword Occurrence Density
= 25 1.25 %
public 20 1.00 %
WCF 16 0.80 %
Web 16 0.80 %
API 16 0.80 %
REST 13 0.65 %
Part 11 0.55 %
ContactsResource 11 0.55 %
Id 9 0.45 %
Contact 9 0.45 %
List 9 0.45 %
class 8 0.40 %
return 7 0.35 %
ContactRepository 7 0.35 %
container 6 0.30 %
Scott 6 0.30 %
database 5 0.25 %
inside 5 0.25 %
code 5 0.25 %
application 5 0.25 %

SEO Keywords (Two Word)

Keyword Occurrence Density
Web API 16 0.80 %
WCF Web 14 0.70 %
the WCF 13 0.65 %
using the 12 0.60 %
REST using 11 0.55 %
Id = 9 0.45 %
Name = 9 0.45 %
Contact Id 9 0.45 %
new Contact 9 0.45 %
public class 7 0.35 %
= Scott 6 0.30 %
List Get 6 0.30 %
public List 5 0.25 %
to the 5 0.25 %
of the 5 0.25 %
our ContactsResource 4 0.20 %
is done 4 0.20 %
Get return 4 0.20 %
return new 4 0.20 %
of our 4 0.20 %

SEO Keywords (Three Word)

Keyword Occurrence Density Possible Spam
WCF Web API 14 0.70 % No
the WCF Web 13 0.65 % No
REST using the 11 0.55 % No
using the WCF 11 0.55 % No
Contact Id = 9 0.45 % No
new Contact Id 9 0.45 % No
Name = Scott 6 0.30 % No
public List Get 5 0.25 % No
Web API getting 4 0.20 % No
List Get return 4 0.20 % No
1 Name = 3 0.15 % No
Block new Contact 3 0.15 % No
Glenn Block new 3 0.15 % No
= Glenn Block 3 0.15 % No
Name = Glenn 3 0.15 % No
return new List 3 0.15 % No
= 1 Name 3 0.15 % No
Id = 1 3 0.15 % No
List new Contact 3 0.15 % No
new List new 3 0.15 % No

SEO Keywords (Four Word)

Keyword Occurrence Density Possible Spam
the WCF Web API 13 0.65 % No
REST using the WCF 11 0.55 % No
using the WCF Web 11 0.55 % No
new Contact Id = 9 0.45 % No
WCF Web API getting 4 0.20 % No
public List Get return 4 0.20 % No
1 Name = Glenn 3 0.15 % No
Name = Scott Hanselman 3 0.15 % No
Contact Id = 2 3 0.15 % No
Id = 2 Name 3 0.15 % No
= 2 Name = 3 0.15 % No
2 Name = Scott 3 0.15 % No
Id = 3 Name 3 0.15 % No
= Scott Hanselman new 3 0.15 % No
Hanselman new Contact Id 3 0.15 % No
Contact Id = 3 3 0.15 % No
Glenn Block new Contact 3 0.15 % No
= 3 Name = 3 0.15 % No
3 Name = Scott 3 0.15 % No
Name = Scott Guthrie 3 0.15 % No

Internal links in - alexanderzeitler.com

Fixing Chrome 58+ [missing_subjectAltName] with openssl when using self signed certificates
Fixing Chrome 58+ [missing_subjectAltName] with openssl when using self signed certificates | Alexander Zeitler
Accessing an HTTP API running on your MacBook in a Docker container from your iPhone/iPad using dnsmasq
Accessing an HTTP API running on your MacBook in a Docker container from your iPhone/iPad using dnsmasq | Alexander Zeitler
Deploying Ubuntu Mate Desktop as a developer environment in a Azure VM
Deploying Ubuntu Mate Desktop as a developer environment in a Azure VM | Alexander Zeitler
Deploying Ubuntu Mate Desktop as a developer environment in AWS EC2
Deploying Ubuntu Mate Desktop as a developer environment in AWS EC2 | Alexander Zeitler
Debugging mocha tests in a Docker container using Visual Studio Code
Debugging mocha tests in a Docker container using Visual Studio Code | Alexander Zeitler
Debugging mocha tests with Visual Studio Code when Node.js is installed using nvm
Debugging mocha tests with Visual Studio Code when Node.js is installed using nvm | Alexander Zeitler
Debugging a ES6 Node.js application in a Docker container using Visual Studio Code
Debugging a ES6 Node.js application in a Docker container using Visual Studio Code | Alexander Zeitler
Some thoughts on Open Source Software
Some thoughts on Open Source Software | Alexander Zeitler
Resizing a VMware Workstation VM partition using GParted - get the swap partition out of my way!
Resizing a VMware Workstation VM partition using GParted - get the swap partition out of my way! | Alexander Zeitler
Workflows: using Docker Machine and Docker Compose together in development
Workflows: using Docker Machine and Docker Compose together in development | Alexander Zeitler
Chrome crashes on Ubuntu in VMware Workstation
Chrome crashes on Ubuntu in VMware Workstation | Alexander Zeitler
Rename Visual Studio project including files, folders and namespaces
Rename Visual Studio project including files, folders and namespaces | Alexander Zeitler
A lap around AWS and docker-machine
A lap around AWS and docker-machine | Alexander Zeitler
Integration-testing ASP.NET 5 / MVC 6 Controllers on DNX Beta 4
Integration-testing ASP.NET 5 / MVC 6 Controllers on DNX Beta 4 | Alexander Zeitler
Deploying a ASP.NET MVC 6 API as Azure API App in Azure App Services
Deploying a ASP.NET MVC 6 API as Azure API App in Azure App Services | Alexander Zeitler
lowerCamelCase JSON with ASP.NET MVC 6
lowerCamelCase JSON with ASP.NET MVC 6 | Alexander Zeitler
Running ASP.NET 5 Beta 4 in Docker with DNX runtime #aspnet5 #docker
Running ASP.NET 5 Beta 4 in Docker with DNX runtime #aspnet5 #docker | Alexander Zeitler
Stop complaining or improve yourself
Stop complaining or improve yourself | Alexander Zeitler
#NodeJS / #ExpressJS: Adding routes dynamically at runtime
#NodeJS / #ExpressJS: Adding routes dynamically at runtime | Alexander Zeitler
mongoose: Referencing schema in properties or arrays
mongoose: Referencing schema in properties or arrays | Alexander Zeitler
MongoDB development environment journal size management using mongoctl
MongoDB development environment journal size management using mongoctl | Alexander Zeitler
Farewell...
Farewell... | Alexander Zeitler
Inspecting AngularJS $scope using Firebug and Chrome Developer Tools
Inspecting AngularJS $scope using Firebug and Chrome Developer Tools | Alexander Zeitler
Useful @Brackets extensions for Web Developers
Useful @Brackets extensions for Web Developers | Alexander Zeitler
.NET User Group Karlsruhe: StreamInsight: Complex Event Processing by Microsoft
.NET User Group Karlsruhe: StreamInsight: Complex Event Processing by Microsoft | Alexander Zeitler
“Commit all must be called when a managed buffer has focus” in Microsoft SQL Server Data Tools
“Commit all must be called when a managed buffer has focus” in Microsoft SQL Server Data Tools | Alexander Zeitler
.NET User Group Karlsruhe: Git
.NET User Group Karlsruhe: Git | Alexander Zeitler
Open Source Windows 8 App: Sprudelsuche
Open Source Windows 8 App: Sprudelsuche | Alexander Zeitler
Open Source– Scheissegal?
Open Source– Scheissegal? | Alexander Zeitler
Lowercase JSON with ASP.NET Web API
Lowercase JSON with ASP.NET Web API | Alexander Zeitler
.NET Usergroup Karlsruhe: Think Big – Entwicklung skalierbarer Anwendungen mit Windows Azure
.NET Usergroup Karlsruhe: Think Big – Entwicklung skalierbarer Anwendungen mit Windows Azure | Alexander Zeitler
ASP.NET Web API and RavenDb
ASP.NET Web API and RavenDb | Alexander Zeitler
ASP.NET Web API Round-Up Slides
ASP.NET Web API Round-Up Slides | Alexander Zeitler
ASP.NET MVC 4 public beta including ASP.NET Web API
ASP.NET MVC 4 public beta including ASP.NET Web API | Alexander Zeitler
Hash Bang Urls in Single Page Applications mit ASP.NET MVC
Hash Bang Urls in Single Page Applications mit ASP.NET MVC | Alexander Zeitler
.NET User Group Karlsruhe: Know your Tools
.NET User Group Karlsruhe: Know your Tools | Alexander Zeitler
Git flow init liefert: flags: FATAL unable to determine getopt version unter Cygwin
Git flow init liefert: flags: FATAL unable to determine getopt version unter Cygwin | Alexander Zeitler
Using JsonValue and JsonArray in WCF Web API
Using JsonValue and JsonArray in WCF Web API | Alexander Zeitler
.NET User Group Karlsruhe: PowerShell 3.. 2.. 1..
.NET User Group Karlsruhe: PowerShell 3.. 2.. 1.. | Alexander Zeitler
What’s new in WCF Web API Preview 5 – introducing the test client
What’s new in WCF Web API Preview 5 – introducing the test client | Alexander Zeitler
Externe Templates in KnockoutJS laden
Externe Templates in KnockoutJS laden | Alexander Zeitler
WCF Web API: Role based access control with basic authentication
WCF Web API: Role based access control with basic authentication | Alexander Zeitler
.NET User Group Karlsruhe: Produce Cleaner Code with Aspect-Oriented Programming (AOP)
.NET User Group Karlsruhe: Produce Cleaner Code with Aspect-Oriented Programming (AOP) | Alexander Zeitler
REST using the WCF Web API – POST it!
REST using the WCF Web API – POST it! | Alexander Zeitler
REST using the WCF Web API – getting more RESTful responses (Part 5)
REST using the WCF Web API – getting more RESTful responses (Part 5) | Alexander Zeitler
TypeLoadException when mocking COM objects - Could not load type … from assembly. The type is marked…
TypeLoadException when mocking COM objects - Could not load type … from assembly. The type is marked… | Alexander Zeitler
REST using the WCF Web API – getting RESTful the BDD way (Part 4)
REST using the WCF Web API – getting RESTful the BDD way (Part 4) | Alexander Zeitler
REST using the WCF Web API – refactoring to IoC style (Part 3)
REST using the WCF Web API – refactoring to IoC style (Part 3) | Alexander Zeitler
REST using the WCF Web API – Basics (Part2)
REST using the WCF Web API – Basics (Part2) | Alexander Zeitler
Microsoft Web API – the REST is done by WCF (Part 1)
Microsoft Web API – the REST is done by WCF (Part 1) | Alexander Zeitler
Microsoft Web API – den REST macht WCF (Teil 1)
Microsoft Web API – den REST macht WCF (Teil 1) | Alexander Zeitler
.NET User Group Karlsruhe: Flexible Softwarearchitekturen mit Inversion of Control (IoC)
.NET User Group Karlsruhe: Flexible Softwarearchitekturen mit Inversion of Control (IoC) | Alexander Zeitler
machine.fakes ReSharper Templates
machine.fakes ReSharper Templates | Alexander Zeitler
CreateCert.cmd aus dem Windows Azure Platform Training Kit funktioniert nicht
CreateCert.cmd aus dem Windows Azure Platform Training Kit funktioniert nicht | Alexander Zeitler
JSON via jQuery direkt an einen ASP.NET MVC 3 Controller senden
JSON via jQuery direkt an einen ASP.NET MVC 3 Controller senden | Alexander Zeitler
POSTing Json data using the HttpClient from WCF Web API
POSTing Json data using the HttpClient from WCF Web API | Alexander Zeitler
Using the new WCF Web APIs HttpClient with ASP.NET Forms Authentication
Using the new WCF Web APIs HttpClient with ASP.NET Forms Authentication | Alexander Zeitler
Keyboard Jedi unter x64
Keyboard Jedi unter x64 | Alexander Zeitler
Using WCF Web APIs / WCF Http with ASP.NET Forms Authentication
Using WCF Web APIs / WCF Http with ASP.NET Forms Authentication | Alexander Zeitler
WCF Web APIs / WCF Http mit ASP.NET Forms Authentication verwenden
WCF Web APIs / WCF Http mit ASP.NET Forms Authentication verwenden | Alexander Zeitler
ILSpy – Open Source .NET Assembly Browser und Decompiler
ILSpy – Open Source .NET Assembly Browser und Decompiler | Alexander Zeitler

Alexanderzeitler.com Spined HTML


REST using the WCF Web API – refactoring to IoC style (Part 3) | Alexander Zeitler Toggle navigation Home Feed REST using the WCF Web API – refactoring to IoC style (Part 3) Written on April 18, 2011 Postings published on this series so far: Microsoft Web API -- the REST is washed-up by WCF (Part 1) REST using the WCF Web API -- Basics (Part2) REST using the WCF Web API -- refactoring to IoC style (Part 3) -- this one REST using the WCF Web API -- getting RESTful the BDD way (Part 4) REST using the WCF Web API -- getting increasingly RESTful responses (Part 5) REST using the WCF Web API -- Post it! (Part 6) Recap In the previous posting I gave you a short introduction to the shiny new WCF Web API from Microsoft. Of undertow the shown implementation is faraway from stuff a real world using sample -- so let's get started to move things forward. When looking then at our ContactsResource there is at least one thing that (hopefully) won't happen in real world code: [ServiceContract] public matriculation ContactsResource { [WebGet(UriTemplate = "")] public List<Contact> Get() { return new List<Contact>() { new Contact() { Id = 1, Name = "Glenn Block" }, new Contact() { Id = 2, Name = "Scott Hanselman" }, new Contact() { Id = 3, Name = "Scott Guthrie" } }; } } Moving forward As you can see, our List of Contacts is instantiated by hand. This for sure won't happen in a real world using happen considering it is likely that our contacts are stored in a database. Thus it will moreover be likely, that we'll have an Repository handling the CRUD-stuff versus this database. This will make our ContactsResource squint like this: [ServiceContract] public matriculation ContactsResource { [WebGet(UriTemplate = "")] public List<Contact> Get() { ContactRepository repository = new ContactRepository(); return repository.Get(); } } Whereas the ContactRepository looks like this: public matriculation ContactRepository { public List<Contact> Get() { return new List<Contact>() { new Contact() { Id = 1, Name = "Glenn Block" }, new Contact() { Id = 2, Name = "Scott Hanselman" }, new Contact() { Id = 3, Name = "Scott Guthrie" } }; } } Ok, this is much largest now. Guess what: It's faraway from that. It's not the fact, that we're new'ing up the Contacts themselves in the Repository as I don't want to lawmaking lanugo the well-constructed Repo for demo. Instead we didn't make an progress to the previous version as we're now simply instantiating the ContactRepository instead of the List of contacts inside our ContactsResource. In terms of testing we now have an dependency on the database by referencing to the touchable instance of our contacts repository. Your CI server may have other database settings (or plane worse: no database installed at all) than your local machine and your tests will goof miserably. Poor boy! In order to get rid of instantiating our repository inside our ContactsResource we will pass it to our constructor and remoter more, we'll pericope an interface tabbed IContactRepository in order to be worldly-wise to mart the touchable implementation of our repository whenever we want to. Doing it this way is a pattern tabbed dependency injection. Our updated code: [ServiceContract] public matriculation ContactsResource { readonly IContactRepository _contactRepository; public ContactsResource(IContactRepository contactRepository) { _contactRepository = contactRepository; } [WebGet(UriTemplate = "")] public List<Contact> Get() { return _contactRepository.Get(); } } public interface IContactRepository { List<Contact> Get(); } public matriculation ContactRepository : IContactRepository { public List<Contact> Get() { return new List<Contact>() { new Contact() { Id = 1, Name = "Glenn Block" }, new Contact() { Id = 2, Name = "Scott Hanselman" }, new Contact() { Id = 3, Name = "Scott Guthrie" } }; } } Yet, there's flipside problem: Who will instantiate our Repository? In terms of our ASP.NET MVC using hosting the ContactsResource, this is washed-up inside theGlobal.asax.cs file. You may remember ContactResourceFactory: public matriculation ContactResourceFactory : IResourceFactory { public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request) { return new ContactsResource(); } public void ReleaseInstance(InstanceContext instanceContext, object service) { throw new NotImplementedException(); } } Using this our lawmaking won't compile now, considering the instantiation of the ContactsResource is missing the IContactRepository instance. How do we pass it in then? One way could be -- as once written -- instantiate the dependency by ourselves inside the global.asax.cs. When having only a few dependencies, this may be ok. But speaking of a real using you'll have tons of them and you don't want to take superintendency of them the whole day. But we're fortunate. Lots of people ran into this surpassing and solved it by creating so tabbed IoC-Containers whose job is to create instances of our classes and handle dependencies between them. My favorite IoC-Container is LightCore and of course, it is misogynist through Nuget. After subtracting the Library Package Reference to our project, we'll update our lawmaking as follows -- starting with the updated RegisterRoutes method in Global.asax.cs: public static void RegisterRoutes(RouteCollection routes) { ContainerBuilder builder = new ContainerBuilder(); builder.Register<IContactRepository, ContactRepository>(); builder.Register<IResourceFactory, LightCoreResourceFactory>(); IContainer container = builder.Build(); var configuration = HttpHostConfiguration.Create() .SetResourceFactory(new LightCoreResourceFactory(container)); RouteTable.Routes.MapServiceRoute<ContactsResource>("contacts", configuration); } The LightCore IoC container mainly consists of a matriculation namely ContainerBuilder and an interface tabbed IContainer and works this way: A ContainerBuilder instance registers all dependencies by calling its Register method and passing in the interface name and the touchable type to be instantiated on using startup. When registration is done, an IContainer is build by the ContainerBuilder calling its Build method. The created container is passed into the LightCoreResourceFactory (formerly known as ContactResourceFactory). The fact that we're using the IoC container now, leads to a generic implementation of the LightCoreResourceFactory: public matriculation LightCoreResourceFactory : IResourceFactory { readonly IContainer _container; public LightCoreResourceFactory(IContainer container) { _container = container; } public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request) { return _container.Resolve(serviceType); } public void ReleaseInstance(InstanceContext instanceContext, object service) { throw new NotImplementedException(); } } As you can see, there's no reference to the ContactsResource or the ContactRepository at all inside the ResourceFactory. Thus we don't need flipside Factory when subtracting remoter Resources. In fact there's increasingly room for optimization as the SetResourceFactory method has an overload that allows us to inject the soul of the GetInstance and ReleaseInstance methods directly which makes the megacosm of the LightCoreResourceFactory obsolete and leads us to the pursuit configuration instantiation: var configuration = HttpHostConfiguration.Create() .SetResourceFactory((serviceType, instanceContext, request) => container.Resolve(serviceType), null); Did you notice that we didn't really speak well-nigh the WCF Web API itself since the whence of this post which simply highlights the fact that it does not get in way using this pattern -- well done. Speaking of done: we've finished flipside hands on lab on the WCF Web API -- thanks for reading. Next time we'll make it increasingly RESTful and add some tests in BDD style. Postings published on this series so far: Microsoft Web API -- the REST is washed-up by WCF (Part 1) REST using the WCF Web API -- Basics (Part2) REST using the WCF Web API -- refactoring to IoC style (Part 3) -- this one REST using the WCF Web API -- getting RESTful the BDD way (Part 4) REST using the WCF Web API -- getting increasingly RESTful responses (Part 5) REST using the WCF Web API -- Post it! (Part 6) .aspx&bgcolor=3169AD&fgcolor=FFFFFF&border=000000&cbgcolor=D4E1ED&cfgcolor=000000) Please enable JavaScript to view the comments powered by Disqus. Copyright © Alexander Zeitler 2003 - 2016 | Impressum