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

How to dispose OpenAccessContext object

3 Answers 90 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
john
Top achievements
Rank 1
john asked on 25 Sep 2013, 07:22 AM
I check your help document about dispose OpenAccessContext object with three ways:

Now, my application will use ASP.NET WEB API to get/put all data with OpenAccess ORM. My question is that do u have good suggestion to dispose the OpenAccessContext object in WEB API scenario.

3 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 25 Sep 2013, 01:37 PM
Hi Jin,

You could have a look at the integration sample between OpenAccess ORM and WebAPI, shipped with the Samples Kit what the best approach to handle OpenAccessContext instance is.

Hope you will find it useful.

Regards,
Damyan Bogoev
Telerik
OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
0
john
Top achievements
Rank 1
answered on 27 Sep 2013, 06:29 AM
I checked your sample. But I don't see any way to dispose the OpenAccessContext  object. Could u help to suggest?
0
Damyan Bogoev
Telerik team
answered on 01 Oct 2013, 02:25 PM
Hi Jin,

You need to slightly modify the sample in order to dispose the OpenAccessContext instance:
  • Change the declaration of the IOpenAccessBaseRepository interface, so that it implements the IDisposable interface;
public interface IOpenAccessBaseRepository<TEntity, TContext> : IDisposable
        where TContext : OpenAccessContext, new()
{
    IQueryable<TEntity> GetAll();
    TEntity GetBy(Expression<Func<TEntity, bool>> filter);
    TEntity AddNew(TEntity entity);
    TEntity Update(TEntity entity);
    void Delete(TEntity entity);
}

  • In the  OpenAccessBaseRepository class implement the Dispose method and dispose the OpenAccessContext there;
public abstract partial class OpenAccessBaseRepository<TEntity, TContext> : IOpenAccessBaseRepository<TEntity, TContext>
        where TContext : OpenAccessContext, new()
{
    protected TContext dataContext = new TContext();
    protected FetchStrategy fetchStrategy = new FetchStrategy();
 
    ...
 
    public void Dispose()
    {
        this.dataContext.Dispose();
    }
}

  • Override the OpenAccessBaseApiController.Dispose(bool) method:
public abstract partial class OpenAccessBaseApiController<TEntity, TContext> : ApiController
        where TContext : OpenAccessContext, new()
{
    protected IOpenAccessBaseRepository<TEntity, TContext> repository;
 
    public virtual IQueryable<TEntity> Get()
    {
        var allEntities = repository.GetAll();
 
        return allEntities;
    }
 
    /// <summary>
    /// Creates a new entity based on the provided data
    /// </summary>
    /// <param name="entity">The new entity to be created</param>
    /// <returns>HTTP Status:
    /// - Accepted when operation is successful or
    /// - MethodNotAllowed if the operation is disabled for this entity or
    /// - BadRequest if the provided entity is NULL</returns>
    public virtual HttpResponseMessage Post(TEntity entity)
    {
        if (entity == null)
            throw new HttpResponseException(HttpStatusCode.BadRequest);
 
        //TODO: should we check if the incomming entity is not an existing one?
        TEntity newEntity = repository.AddNew(entity);
 
        var response = CreateResponse(HttpStatusCode.Accepted, newEntity);
        return response;
    }
 
    protected abstract HttpResponseMessage CreateResponse(HttpStatusCode httpStatusCode, TEntity entityToEmbed);
 
    protected override void Dispose(bool disposing)
    {
        this.repository.Dispose();
    }
}

 
Hope you will find it useful.

Should you have further questions, do not hesitate to contact us back.

Regards,
Damyan Bogoev
Telerik
OpenAccess ORM Q3 2013 Beta is available for immediate download in your account. Get it now and play with the latest bits. See what's new >>
Tags
General Discussions
Asked by
john
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
john
Top achievements
Rank 1
Share this question
or