Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the best practices for calling a WCF service from ASP .NET MVC3?

I am responsible for the solution architecture of an ASP .NET MVC 3 web application and want to ensure that I follow the best practices. I have worked with MVC 3 once before, but using a solution someone else put together.

My main concern is that the Web application will make use of a WCF service to retrieve and update data. I don't think that calling the WCF service directly from my controller methods would be best practice, but I am not sure what a good alternative would be (perhaps a Repository pattern, would this be unusual to use in conjunction with WCF?). I just would like to know if there is a standard pattern/practice I could use.

Additionally, the WCF service is a wrapper for many other webservices and so it has it's own classes for business objects. I am not sure if I should be creating another level of abstraction between the the WCF service classes and my model classes in the MVC application. So for example the WCF service has a Reservation class, do I need to create the same class in my Models for the Web Application?

Any assistance would be much appreciated. Thanks!

like image 366
King_Nothing Avatar asked Dec 21 '22 19:12

King_Nothing


2 Answers

My 2 cents

... I don't think that calling the WCF service directly from my controller methods would be best practice, but I am not sure what a good alternative would be (perhaps a Repository pattern [...])

No, don't do a yet another abstraction. Use your services directly, inject a proxy instance into your controller and just call WCF methods directly from there. If you wish you may create a wrapper class to handle some WCF faults or errors.

Will your application allow an easy switch between WCF and say a database (as a data source) in the nearest future? If not stay away from the Repository pattern - keep it simple, stupid!

Additionally, the WCF service is a wrapper for many other webservices and so it has it's own classes for business objects.

Do not write a copy-paste code, because it is hard to maintain it. If you need to change one type in one of your services, you must do the same mirror change in other services.

Instead separate all you common business objects into a separate project (a core library) and reuse this library in any other solutions.

like image 162
oleksii Avatar answered Feb 02 '23 00:02

oleksii


The Controller is responsible for fetching/transforming data, and so it would interact with the WCF service via a proxy. Something like this (in VB.NET):

'TODO: You could extract the channel creation in a generic reusable method (Of T)
Dim endpoint = New EndpointAddress() 'TODO: Initialize
Dim binding = New BasicHttpBinding() 'TODO: Initialize
Dim factory = New ChannelFactory(Of IYourContract)(binding, endpointAddr)
Dim channel = factory.CreateChannel()

Dim result = channel.YourOperation()

Your WCF service already transforms 'service objects' into 'business objects', so you can treat the same 'business objects' as 'Model' in your UI layer. If you have lighter page-specific or JSON-serialized entities, you may create additional Models as needed.

I would generally suggest to use a 'service adapter' on a WCF service, but in your case, the WCF service already seems to be doing that.

like image 31
Channs Avatar answered Feb 02 '23 00:02

Channs