locked
DBContext connection Dispose RRS feed

  • Question

  • Hello All,

    I am learning the DI with MEF and below is the code i am using to learn

    [Export(typeof(ITweetService))]
        public class TweetService : ITweetService
        {
            private ILogger _logger;
            [Import]
            public IBlogDBContext _dbContext { get; set; }
            [ImportingConstructor]
            public TweetService(ILogger Logger)
            {
                _logger = Logger;
            }


            public List<Blog> GetAllBlogs()
            {
                List<Blog> _blogs = null;
                try
                {
                    _blogs = _dbContext.Blogs;
                    _logger.LogMessage(String.Concat("Blog Count", _blogs.Count));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                }
                return _blogs;
            }
        }

    As i don't want my EF library dependency in the service layer i just abstracted the EF DBContext with Interface("IBlogDBContext ") and accessing the interface to reach EF DBContext. The DI injection is happening normally. But when we use "_blogs = _dbContext.Blogs;" will the connection get dispose automatically. Normally i use using block with DBContext.

    Then i found the following blog

    http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html#.U6WdzrGEeTw

    which mentioned that the connection will get automatically disposed. So is the above way is correct??

    Wednesday, March 2, 2016 6:46 AM

Answers

  • Hi Mrityunjay,

    >>which mentioned that the connection will get automatically disposed. So is the above way is correct??

    You could inherit an interface named IDispose, then implement the method named Dispose. Like below.

    public class TweetService : ITweetService, IDisposable
    {
    //your code
     public void Dispose()
     {
    //dispose your dbcontext;
     }
    }
    

    Best regards,

    Cole Wu

    Thursday, March 3, 2016 9:41 AM

All replies

  • The below is how I do by just using a "Using' statement.

    using System.Linq;
    using DAL.Model;
    using System.Collections.Generic;
    using Entities;
    
    
    namespace DAL
    {
        public class DaoArticle :IDaoArticle
        {
            public DTOArticle GetArticleTop1()
            {
                var dto = new DTOArticle();
    
                using (var db = new PublishingCompanyEntities())
                {
                    var article = (from a in db.Articles select a).Take(1).First();
                 
                    dto.ArticleId = article.ArticleID;
                    dto.AuthorId =  article.AuthorID;
                    dto.Body = article.Body;
                    dto.Title = article.Title;
                }
    
                return dto;  
            }
    
            public DTOArticle GetArticle(int id)
            {
                var dto = new DTOArticle();
    
                using (var db = new PublishingCompanyEntities())
                {
                    var article = (from a in db.Articles.Where(a => 
                            a.ArticleID == id)select a).First();
    
                    dto.ArticleId = article.ArticleID;
                    dto.AuthorId =  article.AuthorID;
                    dto.Body = article.Body;
                    dto.Title = article.Title;
                }
    
                return dto;  
            }
    
            public List<DTOArticle> GetArticles()
            {
                var dtos = new List<DTOArticle>();
    
                using (var db = new PublishingCompanyEntities())
                {
                    var articles = (from a in db.Articles select a).ToList();
    
                    dtos.AddRange(articles.Select(article => new DTOArticle
                    {
                        ArticleId = article.ArticleID,
                        AuthorId = (int)article.AuthorID,
                        Body = article.Body,
                        Title = article.Title
                    }));
                }
    
                return dtos;
            }
    
            public void UpdateArticles(List<DTOArticle> articles)
            {
                using (var db = new PublishingCompanyEntities())
                {
                    foreach (var article in articles)
                    {
                        var art = db.Articles.Single(x => x.ArticleID == article.ArticleId);
    
                        art.AuthorID = article.AuthorId;
                        art.Title = article.Title;
                        art.Body = article.Body;
                        db.SaveChanges();
                    }
                }
            }
    
            public void AddArticles(List<DTOArticle> articles)
            {
                using (var db = new PublishingCompanyEntities())
                {
                  foreach (var article in articles)
                    {
                        var artc = new Article
                        {
                            AuthorID = article.AuthorId,
                            Title = article.Title,
                            Body = article.Body
                        };
    
                        db.Articles.Add(artc);
                        db.SaveChanges();
                    }
                }
            }
         
    
            public void DeleteArticle(DTOArticle article)
            {
              
                using (var db = new PublishingCompanyEntities())
                {
                    var art = db.Articles.Single(x => x.ArticleID == article.ArticleId);
                    db.Articles.Remove(art);
                    db.SaveChanges();
                }
            }
        }
    }
    

    Wednesday, March 2, 2016 2:27 PM
  • yes that how normally we do. If we want to have layer separation of using not using EF DB context all places we will go with repository which will encapsulate the DBContext as i was just trying with single interface to abstract the DBContext with MEF wanted to know on this.
    Thursday, March 3, 2016 4:51 AM
  • yes that how normally we do. If we want to have layer separation of using not using EF DB context all places we will go with repository which will encapsulate the DBContext as i was just trying with single interface to abstract the DBContext with MEF wanted to know on this.

    You mention the Repository pattern I avoid it and use the Data Access Object pattern in the DAL that is closer to the underlying datastor.     
    Thursday, March 3, 2016 5:41 AM
  • Hi Mrityunjay,

    >>which mentioned that the connection will get automatically disposed. So is the above way is correct??

    You could inherit an interface named IDispose, then implement the method named Dispose. Like below.

    public class TweetService : ITweetService, IDisposable
    {
    //your code
     public void Dispose()
     {
    //dispose your dbcontext;
     }
    }
    

    Best regards,

    Cole Wu

    Thursday, March 3, 2016 9:41 AM