开发者

How to I write a repository with effective Dispose() in EF 3.5 -4.0?

i try to write a kind of repositor开发者_如何学JAVAy for effective Add,Update, Delete etc. But i am so confused how can i dispose my 2 class ( ErpEntities and DataRepository) there are more advise but also more conflicts on google. i want to make disposing after return value. Shorthly and effectivelly :( Best regards...

namespace WinApp.EF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (ErpEntities erp = new ErpEntities())
            {
                erp.SaveCustomer(textBox1.Text, textBox2.Text);
            }
        }
    }

    public class ErpEntities : IDisposable
    {

        public int SaveCustomer(string Name, string SurName)
        {
            using (DataRepository<Customer> repository = new DataRepository<Customer>(new TestErpEntities()))
            {
                return repository.Add(new Customer() { Name = Name, SurName = SurName });
            }
        }

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

    }


    public interface IRepository<T> : IDisposable where T : class
    {
         int Add(T entity);
    }

    public class DataRepository<T> : IRepository<T> where T : class
    {

        private TestErpEntities _context;

        public DataRepository()
        {
        }   

       public DataRepository(TestErpEntities context)
        {
            _context = context;
        }

        public int Add(T entity)
        {
            _context.AddObject(typeof(T).Name, entity);
            int saveValue = _context.SaveChanges();
            return saveValue;
        }

        public void Dispose()
        {
            if (_context != null)
                _context.Dispose();
        }

    }
}


I think this is what you want:

public class ErpEntities : IDisposable 
{ 

    public int SaveCustomer(string Name, string SurName) 
    {
        using(DataRepository repository = new DataRepository<Customer>(new TestErpEntities()))
        { 
           return repository.Add(new Customer() { Name = Name, SurName = SurName });  
        } // This using statment ensures that the DataRepository is Dispose()'d when the method exits
    } 


    #region IDisposable Members 

    public void Dispose() 
    { 
       // You could eliminate this as there's nothing in your 
       // ErpEntities class that needs disposing
    } 

    #endregion 
} 

public class DataRepository<T> : IRepository<T> where T : class     
{     

    private TestErpEntities _context;     

    public DataRepository()     
    {     
    }     

    public DataRepository(TestErpEntities context)     
    {     
        _context = context;     
    }     

    public int Add(T entity)     
    {     
        _context.AddObject(typeof(T).Name, entity);     
        int saveValue = _context.SaveChanges();     
        return saveValue;     
    }     


    public void Dispose()     
    {     
        if (_context != null)     
            _context.Dispose();     
    }
}  

The class destructor (~DataRepository()) and the GC.SupressFinalizer() are not necessary because you don't have any unmanaged resources to release. Many would argue that it's part of the IDisposable pattern, but IMO it's unnecessary.

Also, this:

new DataRepository<Customer>().Dispose(); 

is completely redundant and unnecessary. What you're doing here is creating this object only to destroy it. It has no other function and is just waste of memory/cpu cycles.


From what you posted, ErpEntities doesn't need to implement IDisposable. It doesn't 'own' anything.

If it did, new ErpEntities().SaveCustomer(...) would be the wrong way to use it.

The DataRepository class doesn't need the destructor (~DataRepository()) and the call(s) to GC.SuppressFinalize(this) can all be removed too.

What you should be left with:

  • if a class contains (owns) an IDisposable class it should implement IDisposable as well and forward the calls to Dispose()

  • the calling code should use IDisposable classes in a using(...) { } block.

  • don't mess with destructors unless you have an unmamaged resource (and even then there are better options)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜