I have to cache a set of lookup collection based on several objects. In other to use app fabric, the underlying object has to be serializable. My issue is that I have over 100 object that could potentially be cached and going and marking each of them with the [serializable] attribute just seems wrong to me. Since all of this object derive from a base class, I would think marking the base class as serializable would make the derived class serializable but attributes do not work with inheritance. Does enyone know how to go about making all these classes serializable without marking each of them with the property ? Can I make all classes in a namespace serializble for instance, in which case I can put all of them in a library.
Unless you're using the Azure version of AppFabric then you will just have to make the classes serializable, as IDataCacheObjectSerializer never made it into the Windows Server version of AppFabric.
Related
I am trying to implement a system where I have a template class which implements a Serializable interface.
Right now, the interface contains serialize/deserialize methods while the template Setting class has get/set, and private members settingName, settingValue and a template function T adaptType() to adapt string to correct type using the >> operator (https://gist.github.com/mark-d-holmberg/862733). The file also contains a custom struct with << and >> operators overloaded for everything to work.
Settings are serialized in form of settingName:settingValue or settingName:val1;val2;val3 in case of the struct.
There are two problems I'm facing with this design:
I want to hold all these setting objects in a map<string, ISerializable*(?)> to access them but then I can't call other functions get/set on these objects because the interface doesn't define the methods (they must be in Setting class because their type depends on the template type), if I switch the second type in map to template class, I must define a concrete type
When deserializing there's no way to know which type it is and ISerializable can't be instantiated since it's an abstract class, again I need to know which type I'm deserializing and instantiate the Setting class with appropriate type
Is there a better way to design this or something I'm missing, note that I am not very proficient with c++
Bit of a background for the problem:
I'm working on an embedded device where settings need to be loaded/saved to flash memory but there's also another framework running on the device which holds these settings in RAM and serves them on a webserver to be edited, I cannot change this part. My goal is to manually save these settings to my custom class that manages settings and save/load to flash so it is persistent between reboots and synced with the web framework.
Any help or advice is welcome
As far as I understand your problem it is similar to what the Oops library is doing. It creates a Type Descriptor class for all types having similar functionality: giving some description of the type and provide text and binary serialization for them. It is also designed to handle configuration, so it even may help to solve your problem.
It worth to have a look at least for getting some ideas: https://bitbucket.org/barczpe/oops
I am facing this problem:
An upstream application defines a class (e.g. box), and a member (say property) with a base class type. I would make a derived class for that member, add new members and methods without updating their application.
Essentially I do box->property = make_shared<myProperty>(). Is there a way to keep the interface of calling the members and methods the same? That is, to access a property using box->property->length or box->property->GetWeight(), rather than dynamic_pointer_cast<myProperty>(box->property)->GetWeight(). The challenge here is they won't update the base property class, and I am not supposed to change box. But we wish to keep the interface the same so our customers won't complain.
Is it possible? If not, how could we do to best keep the main app and my plugin relatively independent while minimize the changes on the customer side? Any suggestions are welcome.
Looks to me like the derived class for that member property violates Liskov's substitution principle.
You mentioned not being able to modify the Box class.
But are you allowed to modify the property base class? I suggest you add your "additional" methods of your derived class to the property base class.
The intent here being that the interface between the base and derived class should be one and the same. So do this only if it makes sense design wise.
You can traverse entities using entity methods, and you can do exactly the same using entity repository, the difference is that you will make extra class and attach it to the entity and the repository also will create some kind of confusion to other developers to know if a certain method is available, they will need to check the entity and the repository.
Is there any other difference? is there any benefit of using custom repositories ? or what is the best practice to handle such situation?
Generally:
methods in an entity should be connected only to this particular entity object (the exception are getter / setter with relations).
methods in a repository should be connected to whole entity class
For example: if you have an Article entity you may create "slugify" method inside entity because you want make slug from title for this particular object.
But if you need, let say, get all articles which are older than one year - you will make such method in repository (because it's not related to any particular object)
I have a class that is only for json serialization with field access using Jackson , now I have a method that takes some inputs and create an instance of that class with properties populated using setters.
To unit test this method, I need to compare every property of that class. Without public getters, it can only be done via reflection which I am trying to avoid. Also I don't want to override equals method on that class using all properties.
If I add getters, then I am changing the api of that class just for unit testing.
What is the best solution in this case and why? Any thought on this will be appreciated.
Thanks.
Or, you can test the form of the class as it would be normally used - its serialized version. Since serializator is a component which you can safely assume works, simply serialize object after tested class does its work and compare result with reference JSON. This will indirectly test that your class sets properties correctly.
On the other hand, one might argue how deeply impacting is adding getters. If this is the kind of autogenerated boilerplate code, maybe it's better to reconfigure generator to include getters. If not, and you deliberately decided not to implement getters, it probably should stay this way.
I'm creating a C++ object serialization library. This is more towards self-learning and enhancements & I don't want to use off-the-shelf library like boost or google protocol buf.
Please share your experience or comments on good ways to go about it (like creating some encoding with tag-value etc).
I would like to start by supporting PODs followed by support to non-linear DSs.
Thanks
PS: HNY2012
If you need serialization for inter process communication, then I suggest to use some interface language (IDL or ASN.1) for defining interfaces.
So it will be easier to make support for other languages (than C++) too. And also, it will be easier to implement code/stub generator.
I have been working on something similar for the last few months. I couldn't use Boost because the task was to serialize a bunch of existing classes (huge existing codebase) and it was inappropriate to have the classes inherit from the interface which had the serialize() virtual function (we did not want multiple inheritance).
The approach taken had the following salient features:
Create a helper class for each existing class, designated with the task of serializing that particular class, and make the helper class a friend of the class being serialized. This avoids introduction of inheritance in the class being serialized, and also allows the helper class access to private variables.
Have each of the helper classes (let's call them 'serializers') register themselves into a global map. Each serializer class implements a clone() virtual function ('prototype' pattern), which allows one to retrieve a pointer to a serializer, given the name of the class, from this map. The name is obtained by using compiler-specific RTTI information. The registration into the global map is taken care of by instantiating static pointers and 'new'ing them, since static variables get created before the program starts.
A special stream object was created (derived from std::fstream), that contained template functions to serialize non-pointer, pointer, and STL data types. The stream object could only be opened in read-only or write-only modes (by design), so the same serialize() function could be used to either read from the file or write into the file, depending on the mode in which the stream was opened. Thus, there is no chance of any mismatch in the order of reading versus writing of the class members.
For every object being saved or restored, a unique tag (integer) was created based on the address of the variable and stored in a map. If the same address occurred again, only the tag was saved, not the deep-copied object itself. Thus, each object was deep copied only once into the file.
A page on the web captures some of these ideas shared above: http://www.cs.sjsu.edu/~pearce/modules/lectures/cpp/Serialization.htm. Hope that helps.
I wrote an article some years ago. Code and tools can be obsolete, but concepts can remain the same.
May be this can help you.