I have the following class at my business layer level (simplified code):
public class StatusUpdater : IStatusUpdater
{
private readonly IStatusRepo _statusRepo;
public class StatusUpdater(IStatusRepo statusRepo)
{
_statusRepo = statusRepo;
}
public voic UpdateStatus(int id, string newStatus)
{
_statusRepo.UpdateStatus(id,newStatus);
}
}
So currently in the MVC side I'm using PerRequestLifetimeManager to control the lifetime of the DbContext.
But now in the windows service I can't use that, so I was going to do the following, but it doesn't feel right because it looks a lot like ServiceLocator :
using(var container = ConfiguredContainer.CreateChildContainer())
{
var statusUpdater = container.Resolve<IStatusUpdater>();
statusUpdater.UpdateStatus("test");
}
Are there any other options? and is there a way to use the same code in the MVC app and the windows service without having 2 types of registrations:
MVC:
container.RegisterType<IStatusRepo, StatusRepo>(new PerRequestLifetimeManager());
WindowsService:
container.RegisterType<IStatusRepo, StatusRepo>(new HierarchicalLifetimeManager());