Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CA1001 Visual Studio 2012 Code Analysis warning. What does it mean?

It is not that important but I am trying to figure out what it is telling me and is it a legitimate warning ? Can someone explain this error in simple terms for me ?

CA1001 Types that own disposable fields should be disposable

Implement IDisposable on 'MemVoteManager' because it creates members of the following IDisposable types: 'CongressDBEntities'. If 'MemVoteManager' has previously shipped, adding new members that implement IDisposable to this type is considered a breaking change to existing consumers.

    public class MemVoteManager : AbstractDataManager, IMemVoteManager
{
    private CongressDBEntities context = new CongressDBEntities();

    public int AddMemVote(tMemVoteScore mvs)
    {
        //Insert Model
        context.tMemVoteScores.Add(mvs);
        context.SaveChanges();

        int newPK = mvs.MemVoteScoresID;

        //Update funky column ID with PK as well
        var memVoteItem = (from m in context.tMemVoteScores
                           where m.MemVoteScoresID == newPK
                           select m).SingleOrDefault();

        memVoteItem.ID = memVoteItem.MemVoteScoresID;
        context.SaveChanges();
        return newPK;
    }
like image 259
punkouter Avatar asked Mar 01 '13 18:03

punkouter


2 Answers

You could implement IDisposable so the context will be disposed of when consumers are done with your class, but you may be better off NOT having the context be a member of the class. Just create it when you need it and dispose of it when you're done:

public int AddMemVote(tMemVoteScore mvs)
{
    //Insert Model
    using(CongressDBEntities context = new CongressDBEntities())
    {
        context.tMemVoteScores.Add(mvs);
        context.SaveChanges();

        int newPK = mvs.MemVoteScoresID;

        //Update funky column ID with PK as well
        var memVoteItem = (from m in context.tMemVoteScores
                           where m.MemVoteScoresID == newPK
                           select m).SingleOrDefault();

        memVoteItem.ID = memVoteItem.MemVoteScoresID;
        context.SaveChanges();
    }
    return newPK;
}

Contexts are lightweight so there's not a huge penalty for creating them each time. Plus you then don't have to worry about consumers notifying you to dispose of the context, and you don't have a lot of built-up changes in memory if one instance of the class is used many times.

like image 174
D Stanley Avatar answered Nov 04 '22 14:11

D Stanley


It's letting you know that field context contains disposable members. That means those members need to have Dispose() called on them so that Garbage Collection can occur. Therefore, it wants you to implement the interface IDisposable on MemVoteManager so that you can call Dispose() on the context and/or its members that are disposable.

So modify you code as such:

public class MemVoteManager : AbstractDataManager, IMemVoteManager, IDisposable

and then implement the members of the IDisposable interface like this:

public void Dispose()
{
    // call dispose on the context and any of its members here
}
like image 34
Mike Perrenoud Avatar answered Nov 04 '22 15:11

Mike Perrenoud