How to dispose OpenAccessContext object

4 posts, 0 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 25 Sep 2013 Link to this post

    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.
  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 25 Sep 2013 Link to this post

    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 >>
  3. DevCraft banner
  4. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 27 Sep 2013 Link to this post

    I checked your sample. But I don't see any way to dispose the OpenAccessContext  object. Could u help to suggest?
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 01 Oct 2013 Link to this post

    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 >>
Back to Top