I m using JSON.NET to serialize and deserialize object for different purposes. I m a big fan of DI but the code below gives me the chills. Is smells like bad code:
public class Foo : Baz
{
private readonly IBar bar;
public Foo()
: this(ObjectFactory.GetInstance<IBar>())
{ }
public Foo(IBar bar)
{
if (bar == null)
throw new ArgumentNullException("bar");
this.bar = bar;
}
... rest of class ...
}
The default constructor is the thing that gives me the chills. I ve added this to support the deserialization caused by JSON.NET:
string jsonString = ...;
string concreteBazType = ...;
Baz baz = (Baz)JsonConvert.DeserializeObject(jsonString, Type.GetType(concreteBazType);
Notice the class Foo inherrits from the abstract base class Baz!
My question to all you DI and JSON.NET geeks out there: how can I change to code to avoid the code-smell the default constructor gives me in class Foo?