This is a migrated thread and some comments may be shown as answers.

Manually dispose OpenAccessContext

3 Answers 68 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
SI
Top achievements
Rank 1
SI asked on 21 Jul 2015, 02:20 AM
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();
        }
}

 

3 Answers, 1 is accepted

Sort by
0
SI
Top achievements
Rank 1
answered on 22 Jul 2015, 12:53 AM
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 ?
0
Accepted
Simeon Simeonov
Telerik team
answered on 22 Jul 2015, 03:22 PM
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.
0
SI
Top achievements
Rank 1
answered on 22 Jul 2015, 03:44 PM
Thank you, very helpful.
Tags
Data Access Free Edition
Asked by
SI
Top achievements
Rank 1
Answers by
SI
Top achievements
Rank 1
Simeon Simeonov
Telerik team
Share this question
or