Do we need DataContract attribute on POCO classes in Ado.net entity Framework 2010 - wcf

I read somewhere in stackoverflow itself that when we use POCO classes for WCF contracts using Poco generator , we need not use DataContract and DataMember attributes.WCF do it auto for you? . I don't know how it manages this.
I created a sample application without using these attributes and I was able to generate those entities on client side and use them. I disabled proxy generation and Lazy loading.
Am i missing anything here.? Is there really no need of putting these attributes.

You did it right way. Since WCF 3.5 SP1 it is not needed to add DataContract and DataMember attributes. If attributes are not used all properties with public getter and setter are serialized.

Related

WCF: how to create a large number of classes in the contract?

Well, I am creating a WCF service, that have a large number of classes to communicate with the client, and this classes have also many properties.
Mainly, this classes are the POCO classes that is created with the code generator from the edmx, and I have the .tt file.
To can use this classes and properties, I have to use the DataContract and DataMember, so in each classes I have to set the DataContract and in each property of the each class the DataMemeber. So this a big work, so if I need to do some modify to the data base, I must generate again the tt file and then repeat the work.
Is there any way to do this automatically? I am using .NET 4.0 and EF 4.1.
The whole point of the .tt file being added to your project is so that you can modify the template to suit your needs. All you need to do is change the template so that it adds [DataContract] to the entity class definition and [DataMember] to the entity property definitions.
From there, any time the DB is changed you simple use the "Update model from database" feature and your entities will automatically have their code regenerated using the existing template.
All that said, I'm going to recommend you do not expose your DB entities, POCO or not, directly from your service layer. You should really be designing with domain separation and using messaging and CQRS type patterns at the service level. Then you just have some simple mapping methods that translate the data between those messages/commands to your entities.
There is Entity Framework Provider with WCF data services, might be it can help you.

Can I tell the WCF WebAPI serializer to ignore nested class objects?

I am using WCF WebAPI to write REST services using WCF.
I am returning my POCO classes as json/xml objects from my service.
Most of my POCO classes contain ICollections as they are part of EF4.1 Code First,
hence I get error :
Cannot serialize member of type ...
System.Collections.Generic.ICollection ... because it is of type -
because it is an interface
To avoid that XMLIgnore and ScriptIgnore is suggested. And there are some problems in custom serialization of JSON in WCF.
Just thought someone might have come across a similar problem and have a better solution or way to configure serialization classes, otherwise I will have to decorate each such attribute with XMLIgnore, etc.
More often than not you probably would use DTOs or (view models in ASP.NET MVC) to decouple from your domain model. And you can use AutoMapper to minimize your code converting between domain models and DTOs.
You have to add [XmlIgnore] and [IgnoreDataMember]. Then the property will be ignored for xml and json response.
Thats right Sandro, [IgnoreDataMember] and [XmlIgnore] is needed.
A little bit more explaination:
[IgnoreDataMember] is needed to exclude the field in json serialization,
[XmlIgnore] is needed to exclude the field in xml serialization.
I found that in WCF WebAPI [ScriptIgnore] or [JsonIgnore] are not working and have no effect, so I'm going back to ASP.NET MVC for json-related REST APIs.
Update[24-Jul-2012] This answer is old, and Microsfot WebAPI has come a long way since, please check before relying on this answer.

About serialize the object having generic type in WCF

I implemented a generic way to get the object by id from Entities defined in Entity Framework.
But the problem is the object I got has a very weird type like this
{System.Data.Entity.DynamicProxies.MyEntity_C71732021C3A9D6A58BDB6087D29E98CFDE09DA9D53AF0892AFB7918AEF7E61F}
And WCF will fail when serialize this object as the type of MyEntity.
How to make the generic type to be the specific type I want?
It sounds like you're using Entity Framework 4.0 POCO objects. If that's the case, MSDN has a great walk-through on how to get things working:
Walkthrough: Serialize POCO Proxies with WCF
Also take a look at:
Working with POCO Entities (pay close attention to the Serializing POCO Proxies section)

How can I transfer an NHibernate PersistentGenericSet over WCF

I'm trying to send objects retrieved by NHibernate over WCF, but whenever a have a property of ICollection I get an exception.
When NHibernate gets the data from the database this property is intitialized with an instance of PersistentGenericSet.
Is there a way I can send a PersistentGenericSet over WCF?
-or-
Is there some way making NHibernate initialize these properties with another type?
The PersistentGenericSet is part of NHibernate (used to track changes in collections). It is based on the ISet interface and classes from Iesi.Collections, which was used to fill a gap in the .Net framework as there isn't a Set type. I guess that WCF has a problem serializing this type.
A quick fix is to change your NHibernate mappings to use a Bag instead of a Set. Then you can use a normal IList<T> instead of Set<T> in your classes w.
A better solution is to create a remote facade which sends DTOs to your WCF endpoints. This will allow you to keep the interface of your internal types separate from those exposed as remote services. Jimmy Bogards Automapper is a great tool which will help with the mapping process.
Edit
After re-reading the problem I had a look around the and came across this article which describes a workaround for sending NHibernate collections over WCF. David Brion has written a good follow up article.

Reuse classes and objects for both WCF and non-WCF

I have several classes such as Order, Customer, etc. These classes serve for holding data and nothing more. I want to be able to reuse these classes in other projects in the future, but for some reason I don't quite understand, WCF forces me to decorate the data members with the [DataMember] attribute, forcing me to reference WCF plumbing that I will never use in other projects.
I would imagine that WCF lets you take any serializable class and use it as a content type. Am I understanding this correctly?
Yes, with .NET 3.5 SP1, the WCF DataContractSerializer will now serialize any POCO class just the same way as the XmlSerializer will - any public property will be serialized.
I don't know for sure whether that's a good thing - one of the pillars of WCF is being explicit, in order to clearly state your intent. I personally find it a good thing to mark your classes with [DataContract] and your fields and properties you want to have serialized explicitly with [DataMember] - it makes it clearer as to what's going on, and it doesn't hurt your POCO class at all.
And btw: you don't have to reference any "WCF plumbing" to do this - those attributes live in System.Runtime.Serialization - a very generic system assembly.....