Generic opearations/data contracts in WCF

Topics: General Discussion Forum, July and December Releases Forum, Service Factory Modeling Edition Forum
Dec 1, 2009 at 2:30 PM
Edited Dec 1, 2009 at 4:36 PM

Hi All,

I am trying to use generics in WCF.

Below is my requirement. Please suggest the design for this service.

  1. The service exposes 4 operations. Create, Update, Get and Delete. The signature of these operations is:  bool Create( genricObject); bool Update( genricObject); genricObject Get( int genricObjectId); bool Delete(int genricObjectId);
  2. These opeartions are used to create, update, Get and Delete different objects , that’s the reason why I would like to use a generic object for it.
  3. Service will have say 40 objects(data contracts) and these 40 objects needs to be updated at different point of times using single service operation.
  4. The class definitions of the 40 data contracts are similar but not same.

How Can I design my service just to have only 4 generic operations to meet the above requirement.

Please consider service performance also.

If it is not possible, suggest an efficient workaround.

Thanks,

Srinivas Akella

 

Developer
Dec 1, 2009 at 8:47 PM

As you may alredy know, the CRUD "anti-pattern" in terms of SOA is widely accepted to be avoided if possible. Assuming that you may have good reasons to use it, I suggest some reading to get in context and explore some refactoring if applicable.

http://msdn.microsoft.com/en-us/library/ms954638.aspx

http://it.toolbox.com/blogs/the-soa-blog/soa-design-patterns-17563

There are many more resources out there that will be helpful in this regard.

Thanks,

Hernan

Dec 2, 2009 at 12:01 AM

Hey Thanks.

I would appreciate if share how to use generics in WCF. That is my mail requirement.

 

Developer
Dec 2, 2009 at 9:20 AM

About using generics in your service contract and data contract definitions, there are some constrains not only in the WSSF tool but also some inherent to the WSDL spec. Here are some comments about that, http://it.toolbox.com/blogs/alexexmachina/wcf-service-factory-pattern-the-problem-24857.

You may also need to consider serialization and its issues like described here; http://jeffbarnes.net/blog/post/2007/05/10/WCF-Serialization-and-Generics.aspx

And if you feel like to experiment something about this you can use this post as a head start; http://www.eggheadcafe.com/software/aspnet/35325691/wcf-services-and-generic.aspx

As I said above, WSSF will not provide an "out of the box" solution for using generics in your contracts other than collections and lists with a specific primitive type defined. Other options like the one described in the links will require manual coding and further experimentation.

Let me know your findings or further questions.

Thanks,

Hernan

Coordinator
Dec 2, 2009 at 3:29 PM

Hey Srinivas, thanks for your message. I completely agree with Hernan on this topic. I just wanted to add a few more ideas.

First, using generics with WCF is not your requirement, it's the strategy you're considering to address your real requirement which is hopefully tied back to the users of your application. I know this is just semantics, but by calling it a requirement, it might mentally prevent you from identifying more appropriate designs.

The biggest problem I have with your design is you're not using the widely-accepted "best approach" for this kind of service: REST. Please do some research in Representational State Transfer. Basically, HTTP already has the verbs/actions/operations defined for the entity service you intend to build - so you should use them and not reinvent your own. The services will be simpler to implement and maintain and you'll be using the technologies for what they were designed for, which translates to much less risk.

Jon Flanders has a small (very good) book on how to build REST services with WCF. You should also check out the REST Starter Kit on codeplex (aspnet.codeplex.com) if you're going to build a REST service using WCF. Unfortunately, the Service Factory doesn't have the features to build REST services yet. Let me know if you can't find these things and I'm happy to help.

Good luck,
Don