Dependency injection thread safety Is it OK to mix different lifetime of ISerializer and ICacheProvider in this case? I am using Unity DI. In this case, whenever ICacheProvider is needed, Unity will always give the same instance (singleton) but ISerializer will always be a new instance. I am not sure if it would be OK to have ISerializer as a singleton as well in an ASP.NET Web API environment.

public class MyCacheProvider : ICacheProvider
{
   private readonly ISerializer _serializer;

   public MyCacheProvider(ISerializer serializer)
   {
       _serializer = serializer;
   }
}


Serializer implementation:

public class JilSerializer : ISerializer
    {
        public string Serialize&lt;T&gt;(T instance)
        {
            return JSON.Serialize(instance);
        }

        public T Deserialize&lt;T&gt;(string input)
        {
            return JSON.Deserialize&lt;T&gt;(input);
        }
    }


Configuring DI Container to provide instances:

container.RegisterType&lt;ISerializer, JilSerializer&gt;();
            container.RegisterType&lt;ICacheProvider, MyCacheProvider&gt;(new ContainerControlledLifetimeManager());
Your MyCacheProvider will hold an instance of JilSerializer and since your MyCacheProvider is a singleton - there's going to be an instance of JilSerializer around forever as well.

As it's apparently fine for your ICacheProvider to use the same serializer throughout the lifetime of the app, and the implementation looks completely fine for sharing an instance, I don't see any reason why you can't just make ISerializer a singleton too.

I think that this isn't quite a captive dependency as the default lifetime of ISerializer is transient but it's certainly a related topic that you can read about on Mark Seemann's blog.