如何以及何时处置我的物品? [英] How and when to dispose my objects?

查看:56
本文介绍了如何以及何时处置我的物品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在后端服务中,我使用工作单元模式.我想知道我是否缺少与处置对象有关的东西.

In my backend service I use unit of work pattern. I was wondering if I'm missing something related to disposing objects.

首先,这是我到目前为止可用的代码.该服务仅调用请求处理程序:

First, this is the code I have so far, which works. The service just calls a request handler:

 [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上下文:

The EfTrackerDbContext is the actual EF context:

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就像:

And the DatabaseFactory is like:

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:

The 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; }
    }
} 

如您所见,一切都已注入.注册如下:

As you see, everything is injected. Registration looks like:

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

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

Now, the question is about disposing the objects. The only place where IDisposable is implemented is DatabaseFactory, where _datacontext is disposed. I guess that is not enough, so I have following questions:

  1. 添加了AddCustomerRequestHandler,因此,如果AddCustomer()服务操作结束并且开始垃圾回收,则将其处置.可以吗?还是我应该在AddCustomer()操作结束时在AddCustomerRequestHandler上显式调用Dispose,以便它实现IDisposable?
  2. UnitOfWork是否也应实现IDisposable,我是否必须明确地调用它?
  3. 如何处理EfTrackerDbContext?
  4. 其他言论?

简而言之,我正在寻找一种正确的方法,以便在服务操作结束后立即处理所有内容.

In short, I'm looking for the right way to have everything disposed as soon as the service operation ends.

感谢您的建议,L

推荐答案

只要您使用实现IDisposable的东西,就应该在不再需要它时立即将其处置.如果您将其用作类中的字段,请在该类中实现IDisposable并重复此过程.

Whenever you use something that implements IDisposable you should Dispose it as soon as you don't need it any more. If you use it as a field in a class, implement IDisposable in that class and iterate this process.

PS:如果您像这样打开代码分析:

PS: If you turn on Code Analysis like this:

您将免费获得一些警告,提示您:D

you will get some warnings pointing you to this for free :D

这篇关于如何以及何时处置我的物品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆