如何以及何时处理我的物品?

时间:2012-09-18 12:05:56

标签: c# wcf service dependency-injection dispose

在我的后端服务中,我使用工作单元模式。我想知道我是否遗漏了与处理物品有关的事情。

首先,这是我到目前为止的代码,它有效。该服务只调用请求处理程序:

 [GlobalExceptionHandlerBehaviour(typeof(GlobalExceptionHandler))]
public class CustomerService : ICustomerService
{                
    public void AddCustomer(AddCustomerRequest request)
    {
        ObjectFactory.GetInstance<AddCustomerRequestHandler>().Execute(request);            
    }       
}

请求处理程序如下所示:

 public class AddCustomerRequestHandler
{
    private readonly IUnitOfWork _unitOfWork;
    private readonly ICustomerRepository _customerRepository;

    public AddCustomerRequestHandler(IUnitOfWork unitOfWork, ICustomerRepository customerRepository)
    {
        _unitOfWork = unitOfWork;
        _customerRepository = customerRepository;
    }

    public void Execute(AddCustomerRequest request)
    {
        var customer = new Customer(request.Id, request.CompanyName);
        _customerRepository.Add(customer);
        _unitOfWork.Commit();               
    } 
}

工作单位定义如下:

public interface IUnitOfWork
{
    void Commit();
}

public class UnitOfWork : IUnitOfWork
{
    private readonly IDatabaseFactory<EfTrackerDbContext> _databaseFactory;
    private EfTrackerDbContext _dataContext;

    public UnitOfWork(IDatabaseFactory<EfTrackerDbContext> databaseFactory)
    {            
        _databaseFactory = databaseFactory;            
    }

    public EfTrackerDbContext DataContext
    {
        get { return _dataContext ?? (_dataContext = _databaseFactory.Get()); }
    }

    public void Commit()
    {
        DataContext.Commit();
    }       
}

EfTrackerDbContext是实际的EF上下文:

public class EfTrackerDbContext : DbContext, IUnitOfWork
{
    public DbSet<Customer> Customers { get; set; }

    public virtual void Commit()
    {
        base.SaveChanges();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Customer>().ToTable("Customer");
    }
}

DatabaseFactory就像:

public class DatabaseFactory<TContext> : DisposableObject, IDatabaseFactory<TContext> where TContext : DbContext, new()
{
    private TContext _dataContext;
    public TContext Get()
    {
        return _dataContext ?? (_dataContext = new TContext());
    }

    protected override void DisposeManagedResources()
    {
        if (_dataContext != null)
        {
            _dataContext.Dispose();
            _dataContext = null;
        }
    }
}

CustomerRepository:

public interface ICustomerRepository : IRepository<Customer>
{
    IQueryable<Customer> Customers { get; }
}

public class CustomerRepository : RepositoryBase<EfTrackerDbContext, Customer>, ICustomerRepository
{
    public CustomerRepository(IDatabaseFactory<EfTrackerDbContext> databaseFactory)
        : base(databaseFactory)
    {
    }

    public IQueryable<Customer> Customers
    {
        get { return DataContext.Customers; }
    }
} 

如你所见,一切都被注入了。注册看起来像:

 For<IDatabaseFactory<EfTrackerDbContext>>().HybridHttpOrThreadLocalScoped().Use<DatabaseFactory<EfTrackerDbContext>>();
 For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();
 For<ICustomerRepository>().HybridHttpOrThreadLocalScoped().Use<CustomerRepository>();

现在,问题是关于处理对象。实现IDisposable的唯一地方是DatabaseFactory,其中放置了_datacontext。我想这还不够,所以我有以下问题:

  1. 注入了AddCustomerRequestHandler,因此如果AddCustomer()服务操作结束并且垃圾收集开始,它将被释放。这是好的,还是我应该在AddCustomer()操作结束时明确地调用AddCustomerRequestHandler上的Dispose,那么它是否实现了IDisposable?
  2. UnitOfWork是否也应该实现IDisposable,我是否必须明确地调用它?
  3. 如何处置EfTrackerDbContext?
  4. 其他评论?
  5. 简而言之,我正在寻找一种正确的方法,在服务操作结束后立即处理所有事情。

    感谢您的建议, →

1 个答案:

答案 0 :(得分:0)

每当你使用实现IDisposable的东西时,你应该在不再需要它时立即处理它。 如果将它用作类中的字段,请在该类中实现IDisposable并迭代此过程。

PS: 如果您打开代码分析,如下所示: enter image description here

你会得到一些警告,指向你免费:D