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<T>(T instance)
{
return JSON.Serialize(instance);
}
public T Deserialize<T>(string input)
{
return JSON.Deserialize<T>(input);
}
}
Configuring DI Container to provide instances:
container.RegisterType<ISerializer, JilSerializer>();
container.RegisterType<ICacheProvider, MyCacheProvider>(new ContainerControlledLifetimeManager());