简体   繁体   中英

Is warning CA1001 implement IDisposable negligible

I am using Repository pattern and having my general repository class

public class GeneralRepository<T> where T:class
{
    private Context con = new Context();
    protected DbSet<T> Dbset { get; set; }

    public GeneralRepository()
    {
        Dbset = con.Set<T>();
    }
    public IEnumerable<T> GetAll()
    {
        return Dbset;
    }
    public T SelectByID(int? id)
    {
        var rec = Dbset.Find(id);
        return rec;
    }
    public void AddRecord(T entity)
    {
        Dbset.Add(entity);
    }
    public void EditRecord(T entity)
    {
        con.Entry(entity).State = EntityState.Modified;
    }

    public void Detach(T entity)
    {
       con.Entry(entity).State = EntityState.Detached;
    }
    public void Save()
    {
        con.SaveChanges();
    }
}

There are multiple native repositories which are inheriting my General repository like:

public class EmailAssignRepository:GeneralRepository<EmailAssign>
public class DepartmentRepository:GeneralRepository<Department>

Everything is running fine in my code , but it gave me warning when I run code analysis. What does it mean? is it negligible? If not how to overcome this warning?

Based on MSDN :

A class implements the IDisposable interface to dispose of unmanaged resources that it owns. An instance field that is an IDisposable type indicates that the field owns an unmanaged resource. A class that declares an IDisposable field indirectly owns an unmanaged resource and should implement the IDisposable interface. If the class does not directly own any unmanaged resources, it should not implement a finalizer.

Cause:

A class declares and implements an instance field that is a System.IDisposable type and the class does not implement IDisposable .

How to Fix Violations

To fix a violation of this rule, implement IDisposable and from the System.IDisposable.Dispose method call the Dispose method of the field.

And see this: How to implement IDisposable inheritance in a repository?


When to Suppress Warnings (and your question is it negligible ? No)

Do not suppress a warning from this rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM