나는 함께 작업하는 프로젝트를 가지고 있으며, ninject를 통합하려고합니다. 내가 설정 한 방식은 두 개의 유사한 기본 클래스에서 상속받은 여러 서비스 클래스가 있다는 것입니다. AbstractService & AbstractReadableService.이 클래스에서 내 작업 단위를 통해 데이터 레이어에 액세스 할 수 있습니다. 내가 처리하고자하는 주요 종속성은 UnitOfWork에 의해 유지되는 DataContext이며, 이것은 UnitObject에 의해 차례로 주입됩니다.리소스를 처분하기위한 인젝터 얻기 InRequestScope
이 모든 구성 요소 설정 방법은 ServiceFactory를 ninject에서 필요한 종속성을 가져 오는 컨트롤러에 넣는 것입니다. 그런 다음 서비스가 필요할 때마다 ServiceFactory의 복사본에서 가져온 해당 서비스에 필요한 종속성을 설정하고 사용할 준비가 된 해당 서비스의 복사본을 반환하는 GetService 메서드를 사용합니다. 그러나 새 페이지로 이동하거나 다른 작업을 수행하면 UnitOfWork의 Dispose 메서드가 호출되지 않습니다.
내 생각에 ServiceFactory와 그 의존성은 각 요청마다 만들어졌고 새로운 요청을 설정할 때마다 UnitOfWork는 데이터베이스 컨텍스트를 처리 할 것이라고 생각했습니다. 그러나 나는 처분 방법을 때리지 않고 누군가가 올바른 방향으로 나를 가리킬 수 있기를 희망했다.
AbstractService :
AbstractReadableService 작업 단위를 보유하고 있습니다. 작업 클래스의
public abstract class AbstractService : AbstractReadableService
{
/// <summary>
/// Gets the current user id all lowercase at run time
/// </summary>
protected string UserId; //TODO: Change, to a more complete object, ex: We can resolve the employee number at startup time and get all that from an object returned by ICustomPrincipalService
public AbstractService() { }
/// <summary>
/// Constructor for abstract service that we can use to create a new service inside of other services
/// </summary>
/// <param name="user"></param>
/// <param name="unitOfWork"></param>
public AbstractService(ICustomPrincipalService user, IUnitOfWork unitOfWork)
{
if (base._unitOfWork == null || this.UserId == null)
{
this.UserId = user.GetUser();
base._unitOfWork = unitOfWork;
}
}
public void SetDependencies(ICustomPrincipalService user, IUnitOfWork unitOfWork)
{
if (base._unitOfWork == null || this.UserId == null)
{
this.UserId = user.GetUser();
base._unitOfWork = unitOfWork;
}
else
{
// Throw some really nasty error
}
}
}
단위
public class UnitOfWork : IUnitOfWork
{
private DataContext _dataContext;
public UnitOfWork(DataContext dataContext)
{
//Misc
_dataContext = dataContext;
ISomeRepo SomeRepo { get; private set; }
//Repositories
SomeRepo = new SomeRepo(dataContext);
}
public void SaveChanges(string userId)
{
RecordAuditProperties(userId);
_epmsContext.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
_dataContext.Dispose();
}
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
서비스 공장
public class ServiceFactory : IServiceFactory
{
private IUnitOfWork _unitOfWork;
private ICustomPrincipalService _userInfo;
private IEmployeeCredentialService employeeCredentials;
public ServiceFactory(IUnitOfWork unitOfWork, ICustomPrincipalService userInfo, IEmployeeCredentialService employeeCredentials)
{
_unitOfWork = unitOfWork;
_userInfo = userInfo;
}
public T GetService<T>() where T : AbstractService, new()
{
T svc = new T();
svc.SetDependencies(_userInfo, _unitOfWork);
return svc;
}
public T GetReadOnlyService<T>() where T : AbstractReadableService, new()
{
T svc = new T();
svc.SetDependencies(_unitOfWork);
return svc;
}
}
Ninject에 바인딩 :
private void AddBindings()
{
// Binding context to ensure only one context is used in the lifetime of the request
kernel.Bind<DataContext>().ToSelf().InRequestScope();
kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope();
//Deals with pulling the current HTTP context user id into the services
kernel.Bind<ICustomPrincipalService>().To<CustomPrincipalService>().InRequestScope();
kernel.Bind<IHttpContextFactory>().To<HttpContextFactory>().InRequestScope();
kernel.Bind<IEmployeeCredentialService>().To<EmployeeCredentialService>().InRequestScope();
//Disposible dependencies are passed here
kernel.Bind<IServiceFactory>().To<ServiceFactory>().InRequestScope();
}
IDisposable에서 IUnitOfWork가 파생됩니까? –
죄송합니다. 예. – adc90