views:

1390

answers:

4

Hi there,

Can anyone confirm the best way to integrate the repository pattern with webservices.... Well actually i have my repository patter working now in c#. I have 3 projects, DataAccess, Services and my presentation layer.

Problem is my presentation layer is a number of things... I have a ASP.NET MVC site, I have an WPF application and we are about to create another site + an external company needs access to our repository also.

Currently i have just added the services layer as reference to each of the sites... But is not the normal way to provide data access via web services? (WCF) - if this is the case will this break the services layer? or should i convert the services layer to a web service?

ANybody know what the PROS and CONS are of this, speed??

Any ideas or help really appreciated

Mark

A: 

Well first - not all callers have to use the same repository API; this is especially true of an external company.

WCF is interface based. This means that if you need to re-use some logic code, it is possible to use IoC/DI to inject WCF rather than a DAL (but using the same interface) - by using assembly sharing. It sounds like this is what you are doing. This works in many cases, but not all; fundamentally web-service based APIs often need to be designed differently in order to be optimal. It also isn't 100% pure from an SOA viewpoint, but it gets the job done, and allows more intelligent domain entities, so in an intranet (etc) scenario it is (IMO) perfectly reasonable.

An external caller would typically just use the wsdl/mex-based APIs (rather than assembly sharing), but anything is possible...

Marc Gravell
Hi thanks for the reply. Yes currently with WPF and ASP.NET I am using assembly sharing in a pure form of the repository patter - example.. There is 1 main Data Access layer and 1 main Service layer and both the asp.net mvc and wpf have references to the service layer...
In effect the asp.net mvc and wpf presentation layers are the same - they are a different way of accessing the app. I am ok with designing WCF (web services) - so what would be the situation here? WCF would have a reference to services which it would call and then provide Web methods ???
A: 

Maybe webservices are not the best way, if i have full access to the service assembly then i suppose it always better to assembly share the services layer with my applications.

My applications do similar things, but they all need to access the service layer - well the business logic and get back information...

In this case - its always preferable to use assembly sharing with the service layer rather than provide a WCF Web service using HTTP protocol or using TCP on wcf - for example?

Thanks again

+3  A: 

I think I understand your dilemma. If I understand correctly then your services layer consists of pure fabrications. http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design).

If I assume correctly above, then your services layer should not be impacted at all by the introduction of WCF. WCF is essentially an additional presentation layer that provides interoperability, sitting between your UI presentation layer and any business logic layers. So your WCF services would then call your services layer, which may access repositories as needed.

WCF provides a high degree of interoperability so I think it is an excellent choice. I would use basicHttp bindings though, if you intend to interop with different programming languages as this is the most flexible. Don't worry about the speed. There are plenty of solutions out there to mitigate any bottlenecks that result due to WCF.

Good luck, and let me know if I can help in any other way.

mkelley33
I voted +1, but I think there might be one mistake in your post: basicHttp is only there to provide backwards compatibility support for ASP.NET ASMX style web services (which were limited and don't support WS-*). If you want more flexibility and compatibility with different programming languages (i.e. Java, C++), then wsHttp is what you need to use.
jrista
Thanks for the correction. We had switched to basicHttp at work because one of my co-workers asserted that it was more interoperable. I can't wait to share with him your comments and the research I've engaged in since your comments. Much appreciated!
mkelley33
Also, a couple of notes about the compatibility comment: If you need to target the .net 3.0 framework or are using Silverlight 2.0, then then you MUST use basicHttpBinding as wsHttpBinding is not supported. jrista do you have any an links or references I could provide to my co-workers regarding the compatibility with Java and C++? Thanks again.
mkelley33
We use WCF on our backend, and Java for our front ends (one of them is Liferay...ugh). We have been using the wsHttpBinding, and its worked great so far. The wsHttp and wsFederatedHttp bindings are fully standards compliant and support WS-* standards, so if you need compatability with Java, C++, or any other standards-compliant consumer, then those are the bindings for you. As for resources, the only one I have ever really used is "Programming WCF Services", o'Reilly, ISBN: 978-0-596-52699-3 Hope it helps. :)
jrista
I love that book! Its the only resource I've been using too. Thanks for the help, examples and explanations.
mkelley33
+1 for making me think that wcf is a form of a presentation layer. Had been deciding on structuring my project for weeks.
Shawn Mclean
Thanks for the +1 Shawn. Glad I could help. I hope the project goes well :)
mkelley33
A: 

Whether to share your Service/API assemblies with your client applications is fairly subjective. If you are a full Microsoft shop, and use .NET for your entire application stack, then I would say sharing the API is a great way to gain code reuse (you have to be careful how you design your API so you don't bleed domain concerns, like repositories, into your presentation.) If you don't have any plans to migrate your client applications to other platforms (i.e. you plan to stay on .NET for the foreseeable future), then I think its perfectly acceptable to share your Service/API assemblies (and even then, in a multi-platform client environment, sharing Service/API with .NET clients should still be acceptable.) There is always a trade off between the 'architecturally ideal' and the 'practical and achievable within budget'. You can spend a LOT of time, money, and effort trying to achieve the architecturally ideal, when the gap between that and the practical often isn't really that much. The choice NOT to share the API and essentially recreate it to maintain "correct" SOA, consuming only the contract, can actually increase work and introduce maintenance hassles that quite possibly are not worth it for your particular project at this particular time. Given that you are already generally 'service-oriented', if at a future point in time you need the benefit that contract-only consumption on the client can offer, then your already set to go there. But don't push too far too soon.

Given your needs, from what I have been able to glean from these posts so far, I think your on the right track from your services down too. A repository (a la Evans, DDD) is definitely a domain concern, and as such, you really shouldn't have to worry about it from the perspective of your presentation layer. You services are the gateway to your domain, which is the home of your business logic. Repositories are just a support facility that helps you achieve domain isolation from a data store (they are glorified collections really, and to be quite frank...they can be a bit of a pain in a dynamic and complex domain. Simple data mappers, (Fowler, PofEAA) are often a lot easier to deal with and less complex in the long run, and allow more adaptable behavior around your data retrieval logic to be centralized in your domain services.) Aside from heavy use of AJAX calls to REST Services, if you expose adequate Services/API around your domain, that is the only thing that your clients should have worry about. Wrap up all the rest of your business logic entirely within the confines of your domain, and keep your clients as light weight as possible and abstracted from concepts like 'Repository' or 'Data Mapper' and whatnot.

In my experience, the only non-service or API concept that needs to be shared across the Client-to-Domain boundary is Context...and it can be notoriously difficult to cross that boundary in a service-oriented application.

jrista