My Import
method takes some data
and writes it to a stream. If the CompressedStore
property is true, the contents of that stream should be compressed.
This code works, however I just don't like it. For one, I call serializer.Serialize()
twice. I feel this code can be made more concise. Any ideas?
public void Import(IProvisionSource source)
{
InitializeStore();
// Call source.Export and populate local data store
var data = source.Export();
var serializer = new XmlSerializer(data.GetType());
var file = CompressedStore ? "KPCData.gz" : "KPCData.xml";
var path = Path.Combine(DataDirectory, file);
using (var fileWriter = new FileStream(path, FileMode.Create))
{
if (CompressedStore)
{
using (var writer = new GZipStream(fileWriter, CompressionLevel.Optimal))
{
serializer.Serialize(writer, data);
}
}
else
{
serializer.Serialize(fileWriter, data);
}
}
}