Manually dispose OpenAccessContext

4 posts, 1 answers
  1. SI
    SI avatar
    9 posts
    Member since:
    Aug 2014

    Posted 20 Jul 2015 Link to this post

    Hello all, I would like to know if the dispose method below is correct. 

    I'm using Simple Injector, and this is my container configuration:

    _container.RegisterSingle<IGroupRepository>(new GroupRepository(new OpenAccessContext(MyContextConfig.ConnectionStringName, MyContextConfig.GetBackendConfiguration(), new GroupDataSource())));



    public MyRepository(OpenAccessContext context)
    ​{
           _context = context;
     
            public IQueryable<Group> ListGroups()
            {
                return _context.GetAll<Group>();
            }
     
            public void Dispose()
            {
                _context.DisposeDatabase();
                _context.Dispose();
            }
    }

     

  2. SI
    SI avatar
    9 posts
    Member since:
    Aug 2014

    Posted 21 Jul 2015 in reply to SI Link to this post

    This disposable was causing some troubles (Could Not implement my repository in sequence), I had to remove it. So, this implementation can cause problem if not dispose OpenAccessContext ?
  3. DevCraft banner
  4. Answer
    Simeon Simeonov
    Admin
    Simeon Simeonov avatar
    24 posts

    Posted 22 Jul 2015 Link to this post

    Hi SI,

    Thank you for contacting us.

    Regarding your question of the correct way to dispose a DataAccess context - in your case most probably only disposing the context will be the best solution. Like this:

    public void Dispose()
    {
        _context.Dispose();
    }

    Calling 'DisposeDatabase' disposes the underlying static Database instance. This means that the next time you try to perform any OpenAccess operation the entire metadata will be recalculated. This is an expensive operation and ideally should be performed only once, on the first operation.


    Also from your example it seams that you are registering in the IoC container a specific instance of your repository that will always be returned when resolving IGroupRepository. This specific instance holds a specific instance of the OpenAccessContext. This means that if somewhere in your code you dispose your repository, its OpenAccessContext instance will also be disposed and every next time you resolve from the IoC container the repository it will be already disposed and will be working inside with a disposed instance of the OpenAccessContext.

    For that reason I would recommend that you register in the IoC container just the repository type and all its dependencies and always resolve a new instance of your repository and the context it is using. You can do this by registering your own OpenAccessContext class (named for example DataContext) like it is shown here.

    After that you can make you your repository depend on your custom context like this:
    public GroupRepository(DataContext context)
    ​{
    ...
    }

    And you can register in the Simple Injector container both types like this:
    _container.Register<DataContext>();
    _container.Register<IGroupRepository, GroupRepository>();

    I hope you find this information helpful.

    Regards,
    Simeon Simeonov
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  5. SI
    SI avatar
    9 posts
    Member since:
    Aug 2014

    Posted 22 Jul 2015 in reply to Simeon Simeonov Link to this post

    Thank you, very helpful.
Back to Top