I'm trying to understand and learn about the SOLID principle and especially Dependency Injection (DI) and Factory pattern.
Some background:
I have a production application that uses some "helpers" classes but those are all-in-one classes that I just copy into each new project. One of the (most used) functions is serialization. Our customer has strict demands about history/revision tracing so all important requests are serialized to XML and inserted in Sql server.
With SOLID, DI and Factory in mind I decided to rewrite ye old helpers into a fresh C# project and do things properly.
Below is sample code for Serializers (I have three, XML, JSON and Binary), shown is only the Json class but they are all pretty much the same.
My questions are:
Have I correctly implemented the Factory pattern?
Is the Dependency Injection the way it is supposed to be?
I'm not exactly sure if I even covered both patterns, a mix or perhaps just one?
I hope I made things clear, if not let me know, and thanks for helping me understand DI/Factory better.
Interface
namespace nUtility.Serializers
{
public interface ISerializer
{
string InputString { get; set; }
object InputObject { get; set; }
string SerializeToString<T>(); // Need <T> because of the Xml serializer.
object DeserializeFromString<T>();
}
}
Factory
namespace nUtility.Serializers
{
public class SerializersFactory
{
readonly ISerializer _iserializer;
public SerializersFactory(ISerializer iserializer)
{
_iserializer = iserializer;
}
public string Serialize<T>() => _iserializer.SerializeToString<T>();
public object Deserialize<T>() => _iserializer.DeserializeFromString<T>();
}
}
Serializers (Json in this example)
using Newtonsoft.Json;
using System;
namespace nUtility.Serializers
{
public class nSerializerJson : ISerializer
{
public string InputString { get; set; }
public object InputObject { get; set; }
public nSerializerJson(string inputString, object inputObject)
{
if (inputString == null)
throw new ArgumentNullException(nameof(inputString));
if (inputObject == null)
throw new ArgumentNullException(nameof(inputObject));
InputObject = inputObject;
InputString = inputString;
}
public string SerializeToString<T>()
{
return JsonConvert.SerializeObject(InputObject);
}
public object DeserializeFromString<T>()
{
return JsonConvert.DeserializeObject<T>(InputString);
}
}
}
Program.cs
(SampleValues.InputObjectList
is a simple test class which populates a List with 3 items)
var serializersFactory = new SerializersFactory(new nSerializerJson(expectedString, SampleValues.InputObjectList));
expectedString = serializersFactory.Serialize<List<SerializeTestObject>>();
Two more things:
Why not static class for serializers - I kinda agree with this answer on SO, however on a second thought changing my class (the parameter creep/constructor point in SO answer) would violate SOLID (the SRP) so I'm not sure if this is correct decision?
I know I'm breaking the Type and Namespace naming convetion by putting "nSerializers" etc. but this "n" prefix has been in the company for 9 years now and we're not prepared to let go yet :).