I am looking for some opinions on how I have setup complex classes that are deserialized when the game launches. The problem I ran into was a need for nested classes so that I don't need to limit my configuration files with flat XML structures. At the same time I don't want anyone to instantiate these nested classes, they serve a single purpose: holding data for a configuration file. The constructor must be private but the properties should be public. Here is my solution:
[XmlRoot("ApplicationConfiguration")]
public sealed class ApplicationConfiguration : AConfiguration<ApplicationConfiguration>
{
[UsedImplicitly]
public ServicesConfiguration Services = new PrivateServicesConfiguration();
private ApplicationConfiguration() { }
#region Nested Objects
[Serializable]
public class ServicesConfiguration
{
[UsedImplicitly]
[XmlAttribute("webSocketServerBaseUrl")]
public string WebSocketServerBaseUrl;
[UsedImplicitly]
[XmlAttribute("restApiBaseUrl")]
public string RestApiBaseUrl;
protected ServicesConfiguration() { }
}
[Serializable]
private class PrivateServicesConfiguration : ServicesConfiguration { }
#endregion
}
The ServicesConfiguration class hides it's constructor but leaves the properties public for access. PrivateServicesConfiguration is private and is only accessible to the configuration class, it's constructor is public. This means that only the configuration class can create an instance of it. PrivateServicesConfiguration derives from ServicesConfiguration. The result is a nested class that cannot be instantiated outside of the containing class but still exposes it's properties publicly. Thoughts? I removed some of the unrelated properties and methods on the class. The config class derives from the AConfiguration class which derives from an abstract singleton class.
submitted by /u/kylestrader94
[link] [comments]
from Software Development – methodologies, techniques, and tools. Covering Agile, RUP, Waterfall + more! http://bit.ly/2UGwbtB