Adding [DataContract] to class with [Serializable] causes problems - datacontract

I am using the DataContractSerializer to persist objects. Most classes have the [DataContract] attribute but one (not sure why) has [Serializable]. The properties of this class have the [DataMember] attribute. So far, it has worked ok.
Recently, I tried to add a new property but get an error when deserialising. The error message is something like: 'property Notes was expected but found property ModuleNr'.
I tried adding the [DataContract] attribute but got a different error like: 'Deserialised object with id 15 not found'. It seems that the class is not being deserialised at all.
Can anyone explain what I am doing wrong and what I can do to deserialise the existing objects.
Thanks.

Is it the case that the object that was persisted was using the older class structure, and the property you added is a required property, causing the deserialization to fail?
Also, if the Serializable property is there and you are not sure why its there, it might be a good idea to verify if some other code is not using this class to serialize - if it is, the new property you are adding might break it :)
Hope this helps!

Related

difference between class and instance structure

I'm currently trying to learn how to use GObject and there's a point I absolutely don't understand: What's the difference between the class and the instance structure (like "MamanBarClass" and "MamanBar") resp. how do I use them? At the moment I'd put all my object attributes into a private structure (like "MamanBarPrivate"), register it with "g_type_class_add_private" and define properties/getters/setters to access them. But when I leave the class structure empty I get the following error at "g_type_register_static_simple":
specified class size for type `MamanBar' is smaller than `GTypeClass' size
And why are all object methods defined in the class structure (like "GtKWidgetClass")? Probably I'm just screwing up everything, but I only worked with Delphi for OOP yet (I know, nothing to be proud about :D)
Regards
I'm currently trying to learn how to use GObject and there's a point I absolutely don't understand: What's the difference between the class and the instance structure (like "MamanBarClass" and "MamanBar") resp. how do I use them?
The class structure is only created once, and is not instance-specific. It's where you put things which are not instance-specific, such as pointers for virtual methods (which is the most common use for the class struct).
At the moment I'd put all my object attributes into a private structure (like "MamanBarPrivate"), register it with "g_type_class_add_private" and define properties/getters/setters to access them.
Good. That's the right thing to do.
But when I leave the class structure empty I get the following error at "g_type_register_static_simple":
You should never leave the class structure empty. It should always contain the class structure for the type you're inheriting from. For example, if you're trying to create a GObject, the class structure should look like this (at a minimum):
struct _MamanBarClass {
GObjectClass parent_class;
};
Even if you're not inheriting from GObject, you still need the base class for GType:
struct _FooClass {
GTypeClass parent_class;
};
This is how simple inheritance is done in C.
And why are all object methods defined in the class structure (like "GtKWidgetClass")? Probably I'm just screwing up everything, but I only worked with Delphi for OOP yet (I know, nothing to be proud about :D)
Those are virtual public methods. As for why they're defined in the class structure instead of the instance structure, it's because the implementations are the same for every instance.

JPA <pre-persist> <pre-update> are being ignored but the #PrePersist and #PreUpdate work fine

I ran into strange problem. I have the whole domain model defined in the orm.xml file. All my entities in my project are just simple POJOs (no jpa annotations at all). I want to save the last update and the insert timestamps of my entities and I've decided to use the "pre persist" and "pre update" like most of us. So I've defined a base entity class and let all my entities to extend it.
Strange is that the "pre persist" (and all others events) are being called only when I define them using annotations. When I define them in the orm.xml file instead - nothing happens, they are just ignored.
This works for me:
public abstract class BaseEntity {
private Timestamp insertTimestamp;
private Timestamp lastUpdateTimestamp;
#PrePersist
public void onPersist() {
...
}
#PreUpdate
public void onUpdate() {
...
}
}
But after removing annotations and switching to the xml nothing works anymore:
<mapped-superclass class="com.my.model.BaseEntity">
<pre-persist method-name="onPersist"/>
<pre-update method-name="onUpdate"/>
<post-load method-name="postLoad"/>
</mapped-superclass>
According to the JPA specification the above declarations in xml seem to be correct.
I have no idea where to dig for the problem.
I'm using EclipseLink 2.2.0 with H2 in the SE environment.
UPDATE:
Thanks for your answer. There are no errors in log/console to see. Events just seem being ignored.
As you thought is might be a bug because moving the methods and XML declarations from the superclass to the subclass solves the problem. It is not a desired solution for me as I want to have a global solution for all entities but moved me a bit forward.
I've sent the bug report to the EclipseLink guys :)
As you suggested I've tried with entity listener and it works for me. so I will stick to this solution. It even looks better then the solution with base entity class ;)
Thanks !
Your XML looks correct. Do any errors occur in the logs?
It could be a bug with MappedSuperClass and entity events.
Can you try setting the event on a subclass and see if it works?
If it does, then it is probably a bug, please log the bug in Eclipse Bugzilla.
Another workaround would be to use an entity listener.

You must implement a default accessor on System.Configuration.ConfigurationLockCollection because it inherits from ICollection

I have been looking online for quite some time about this error. I cannot seem to be able to figure this one out.
I have a web service created with vb.net in vs 2010. Here is a look at my property
Public Class MyClass
Inherits ConfigurationSection
Protected _score As Integer
<ConfigurationProperty("score", DefaultValue:="12", IsRequired:=False), _
IntegerValidator(ExcludeRange:=False, MinValue:=6, MaxValue:=24)>
Property gt_score() As Integer
Get
Return CType(Me("score"), Integer)
End Get
Set(ByVal value As Integer)
Me("score") = value
End Set
End Property
End Class
When I try to add this as a service to a web app, also done in vs2010 with vb.net, I get the error in the title. Please help with this. I am not sure what is needed in order to implement a default accessor.
I have been struggling with a similar problem myself. I have a class that inherits from ConfigurationSection but I get the same error message when I try to use it in conjunction with XMLSerializer.
I believe this issue cannot be addressed within your class. The problem lies with the properties of the ancestor (ConfigurationSection). It has 4 public properties of type ConfigurationLockCollection (namely LockAllAttributesExcept, LockAllElementsExcept, LockAttributes and LockElements). During serialisation, the serializer finds that these properties implement ICollection but do not have the required default accessors, hence the exception.
For my part I have tried shadowing these 4 properties and decorating them with the XMLIgnore() but still get the same problem. AFAIK it will not be possible to serialize any class that inherits from ConfigurationSection until MS adds the default accessor to ConfigurationLockCollection.
For now I will be implementing my own methods that serialize just the properties I need from my customised configuration section.
I haven't worked with VB.net, but in general, ICollection is an interface class.
An interface describes the methods and properties that classes inheriting from the interface need to define. In your case, your derrived class MyClass needs to define a default accessor.
What if you try:
get
{
return this;
}
I encountered the same problem. I resolved it by creating an XML wrapper object that just contains the properties I want to serialize and create two methods that map from the the unserializable object (a custom configuration element derived from ConfigurationElement) to the Attribute object and back.
I can then serialize/deserialize a collection of the simple objects.
See My Answer To This Question

Issue with MVVMLight ViewModelBase public parameterless constructor in inherited base class for WP7 Tombstoning

I am handling tombstoning in Wp7 by dumping my ViewModel into the PhoneApplicationService state (see this link for more info).
My ViewModel (VM) inherits from the MVVM Light Toolkit (ViewModelBase) which has a protected parameterless constructor only.
This causes the serilization to fail with:
"The type 'GalaSoft.MvvmLight.ViewModelBase' cannot be deserialized in partial trust because it does not have a public parameterless constructor."
Excuse my ignorance but serialization is new to me - I think I understand why it's failing, but I am trying to think of ways around it. For example, can I mark the entire base class as non-serilizable or ignored like I do certain fields in classes ([IgnoreDataMember])? I don't need to store anything that is in this class.
Is there anyway around this? I don't want to edit the source of that assembly to mark it public instead of protected.
Public default constructors in abstract classes are frowned upon by StyleCop, which is why I made the ViewModelBase one protected. As you found out, this however causes issues with serialization. This issue is more acute in WP7 where it is tempting to dump the whole vm in serialization for safe keeping.
Right now, the only fix I can propose is to implement your own viewmodelbase class. I will consider changing the constructor to public in a future release.
Cheers,
Laurent

DataContract attribute -WCF

Is DataContract attribute is deprecated in ASP.NET 4.0 WCF ? I can see only DataContractFormat attribute.
I can't apply DataContractFormat attribute over struct.
example
[DataContractFormat]
public struct Contact
{
public string firstName;
public string lastName;
}
It throws an error saying that DataContractFormat artribute can only be used on class, interface and methods.
No, the .NET 4 still contains the DataContractAttribute:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute%28v=VS.100%29.aspx
and it should be able to be applied to class or struct.
You have to add a reference to the System.Runtime.Serialization assembly (right click References, add ref...)
DataContract is not deprecated - where the heck did you get that idea from?
DataCOntractFORMAT is something totally different. I suggest you please read the documentation ;) Helps a lot. Will also explain what DataContractFormat is for.
http://msdn.microsoft.com/en-us/library/system.servicemodel.datacontractformatattribute.aspx
As you can see in the example this attribute goes on the class/interface that defines the SERVICE CONTRACT. It controls how for that service data serializaton is (guess what) formatted.