获取ninject处理资源InRequestScope

时间:2016-11-23 18:19:01

标签: c# generics ninject inversion-of-control idisposable

我有一个与我合作的项目,我正在尝试将ninject集成到。我设置它的方式是我有几个服务类继承自两个相似的基类AbstractService& AbstractReadableService,通过我的工作单元,可以从中访问数据层。我想要处理的主要依赖是由UnitOfWork持有的DataContext,然后由ninject注入。

我设置所有这些组件的方式是,我将ServiceFactory放入控制器,它从ninject获取所需的依赖项。然后,每当我需要服务时,我都使用GetService方法,该方法为从ServiceFactory副本中提取的服务设置所需的依赖关系,并返回该服务的副本以供使用。但是,当我导航到新页面或执行任何其他操作时,从不调用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();
    }

1 个答案:

答案 0 :(得分:0)

一切都是正确的,我从nuget安装了Ninject MVC4解决方案之后能够让它工作