1

I've got a class that has a factory method, which when it finds a serialized version of itself should return it; otherwise it should return a new instance of itself:

class ClassToDeserialize : List<SomeClass>
{
    public static event Func<ClassToDeserialize> onNoDeserializationFile;

    public ClassToDeserialize(SomeClass firstInList)
    {
        this.Add(firstInList);
    }

    public static ClassToDeserialize DeserializeIfAny(string jsonPath)
    {
        if (File.Exists(jsonPath))
            return JsonConvert.DeserializeObject<ClassToDeserialize>(File.ReadAllText(jsonPath));

        return onNoDeserializationFile();
    }
}

I'm trying to refactor my application to use DI, the problem though is that I have to do a double binding for ClassToDeserialize, like so:

static void Main(string[] args)
    {
        string json = @"C:\obj_serialized.txt";
        IKernel ninjectKernel = new StandardKernel();
        ClassToDeserialize.onNoDeserializationFile += (() => ninjectKernel.Get<ClassToDeserialize>("NoSerialization"));

        ninjectKernel.Bind<ClassToDeserialize>().ToSelf().Named("NoSerialization").WithConstructorArgument("jsonPath", json);
        ninjectKernel.Bind<ClassToDeserialize>().ToConstant<ClassToDeserialize>(ClassToDeserialize.DeserializeIfAny(json));
    }

I added the onNoDeserializationFile event to let ninject handle all instantiations and decouple my business logic from my IoC, and then I intend to Get a Service which has a dependency upon ClassToDeserialize, and to be able to resolve this request I need to find a way to tell ninject that when a serialization file is found it should call the corresponding binding (even though the context is the same).

ninjectKernel.Get<DependantClass>().DoSomething();

I'm aware this resembles the Service-Locator Antipattern, but is not the only way I'm using the container and this behavior is tied only to the entry point of my application.

What's the proper way for solving this?

2
  • Your ClassToDeserialize seems to be a DTO. DTOs, entities, messages, and other classes that hold data should not be registered in your container. Containers are for resolving components; classes that have behavior. Any time you try to mix data and behavior and register it in your container, things will go wrong. So data classes should not have dependencies. They should simply be 'pushed' through the object graph that you resolved using your container. You should refactor your code accordingly and a lot of problems will disapear when doing that.
    – Steven
    Dec 24, 2014 at 20:24
  • @Steven maybe you're right but I would argue that the DTO is my - SomeClass class. I use the ClassToDeserialize as a custom list to do things whenever I add or remove SomeClasses from the list.
    – Jassel
    Dec 24, 2014 at 21:13

1 Answer 1

1

You could put that decision logic into an IProvider.

Alternatively there's the When binding syntax for conditions. See Contextual Bindings.

How about using:

kernel.Bind<ClassToDeserialize>().ToSelf()
      .WithConstructorArgument(...); // default binding

kernel.Bind<ClassToDeserialize>()
      .ToMethod(ctx => JsonConvert.DeserializeObject<ClassToDeserialize>(...))
      .InSingletonScope()
      .When(ctx => File.Exists(...));

(hint: i didn't compile it so the method sequence might be slightly off).

1
  • Perfect! Thanks, that's just what I needed, I did see the Contextual Bindings documentation but I overlooked that, I did not know the contextual binding could be so specific. I tried to give a point to your answer but I apparently I'm new and can't do it yet :/
    – Jassel
    Dec 26, 2014 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.