简体   繁体   中英

Should I Dispose instances that are not Owned?

I'm using Autofac in a project, and I'm trying to do it right. So I've been reading the documentation and found Owned<T> . It seems like it's the right relationship type to use when i want to dispose something by myself - eg a DbContext which is disposable.

So I changed all my injected factories Func<DbContext> to Func<Owned<DbContext>> .

The only thing is it feels a little dirty the Lazy<T> like behaviour and to put the Owned in the using and depending on a non framework type...

Is it wrong to not use Owned at all?

Is there an issue in disposing instances that are not owned by my class like this?

public class MyClass
{
    private readonly Func<DbContext> _dbcFactory;

    private MyClass(Func<DbContext> dbcFactory)
    {
        _dbcFactory = dbcFactory; // nullcheck etc;
    }
    private void TheMethodWhoUpdate(String newName) 
    {
        using(var dbc  = _dbcFactory())
        {
            var ent = dbc.Table.Single(x => x.id == 3);

            end.Name = newName;
            dbc.SaveChanges();
        }
    }
}

What I was able to imagine (because I can't find a clue on the docs) is that this could lead to some performance issue, because maybe autofac will track that created DbContext instance and try to dispose it again, losing some amount of time (probably very small)... But maybe I'm wrong, and I should stick to the guide .

Why not just use Func<DbContext> dbcFactory like shown in your code sample?

You shouldn't do that, for two reasons.

  1. It is the container's job to dispose of it - so let it do its job.
  2. Secondly, if you register that DbContext as InstancePerLifetimeScope then multiple objects (involved in the same lifetime scope) will get the same instance of the DbContext - which is problematic if one of them disposes it out from under the other one.

When having only one instance of dbcontext (or just any other class) is just not an option (there is so many reason that i won't discuss it here) it is perfectly fine to create new ones.

And have a factory injected instead of just one instance is the way to obtain it while sticking to the dependency injection pattern.

About the Func<Owned<T>> vs the Func<T> i just found out the hard way that let the scope do it's job it is just a bad idea.

I used the Func<Owned<T>> approach but even if I disposed all the owned scopes I end up with a HUGE memory leak.

The way that really worked for me is to inject a factory that create the dbContext (or whatever you need for just a limited amount of time, or need to control the lifecycle).

That factory should be registered as a single instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM