2

In a personal application i work on at the moment i have global application settings which should affect how certain objects behave, in terms of architecture i figured it would create an unnecessary dependency to directly reference them, e.g.

public void Update()
{
    if (App.Settings.AutoCacheImages) CacheImages();
    ...
]

So i would prefer to create an interface like this:

public interface IFeedSettings
{
    bool AutoCacheImages { get; set; }
    ...
}

And make sure that my object gets a reference during construction:

private IFeedSettings _settings;

public Feed(IFeedSettings settings)
{
    _settings = settings;
}

Now the problem is that Xml-Serialization requires a parameterless constructor, so what would the best way to approach this so that after deserialization all instances of Feed will have a reference to my global settings?

(I have a hierarchical folder/file-like data-structure using the composite pattern)

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Have you considering making specific models for serialization, and then using something like AutoMapper to map from your entity to your model? – Tejs Apr 08 '11 at 19:57
  • @Tejs: I would appreciate it if you could elaborate on that since i have no idea what you are talking about. – H.B. Apr 08 '11 at 20:03
  • What object is the subject of Xml Serialization? The settings or the Feed? – quentin-starin Apr 08 '11 at 20:08
  • @qes: The Feed. (The settings get serialized as well but they do not have any dependencies and it is just one instance so restoring them would not be a hassle if there were any) – H.B. Apr 08 '11 at 20:10
  • You might get some troubles deserializing that interface: http://stackoverflow.com/questions/1333864/xml-serialization-of-interface-property – ngm Apr 08 '11 at 20:25
  • @ngm: No, the settings reference inside the Feed is *not supposed to be serialized*, and it *will not be serialized* because it is a private field, i do not want to have the same settings saved with every single feed, that would be horrible. – H.B. Apr 08 '11 at 20:30

1 Answers1

0

Clarification:

You have your object of type Feed. You want to serialize that, but you can't with a non default constructor. Instead, you can create a FeedModel, with a parameter less constructor and public properties for all the data you want to send. Then, you can use AutoMapper to quickly transfer data from your instance of Feed to an instance of FeedModel and serialize the model class.

Tejs
  • 40,736
  • 10
  • 68
  • 86