0

私は効果的な追加、更新、削除などのためのリポジトリの種類を書き込もうとしています。しかし、私は2つのクラス(ErpEntitiesとDataRepository)を処分する方法がもっと助言されています。私は戻り値の後に処分したい。 Shorthlyとeffectivelly :( 敬具...EF 3.5 -4.0に有効なDispose()を持つリポジトリを書くにはどうすればいいですか?

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(); 
     } 

    } 
} 
+0

この記事をご覧ください。それは助けるかもしれません:http://bit.ly/bF7jL3。 – Steven

+0

なぜ人生は困難になるのですか? DIコンテナを使用し、コンテキストを破棄します。 – RPM1984

+0

DIコンテナとは何ですか? – Penguen

答えて

2

私は、これはあなたが欲しいものだと思う:

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();  
    } 
} 

クラス解放する管理対象リソースがないため、デストラクタ(~DataRepository())とGC.SupressFinalizer()は不要です。多くの人はそれがIDisposableパターンの一部だと主張しますが、IMOは不要です。

はまた、この:

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

は完全に冗長で不要です。あなたがここでやっているのは、このオブジェクトを作成することです。しか破壊しません。それは他の機能を持たず、メモリ/ CPUサイクルの無駄にすぎません。

+0

public void Dispose() {GC.SurprizeFinalize(this); }はOKですか? ErpEntitiesのために? – Penguen

+0

'ErpEntities'には' Dispose() 'メソッドは全く必要ありません。データベース接続やファイルハンドルのように、処理する必要のあるものがあれば 'Dispose()'だけが必要です。 'GC.SurpriseFinalize()'はあなたのクラスにファイナライザがある場合にのみ使う必要があります。あなたが正しく処分することを保証しなければならない管理されていないリソースがない限り、ファイナライザは本当に必要ありません。 – CodingGorilla

+0

//あなたは何を意味するのかを処理する必要があるErpEntitiesクラス? – Penguen

1

あなたが投稿するものから、ErpEntitiesはIDisposableインターを実装する必要はありません。それは、自身の "何もしません。

それがなかった場合は、 new ErpEntities().SaveCustomer(...)は、それを使用するために間違った方法だろう。

DataRepositoryクラスはGC.SuppressFinalize(this)にデストラクタ(~DataRepository())とコール(複数可)を必要としない、あまりにも削除することができます。

呼び出し元のコードが使用するクラスが含まれている場合

  • (所有)IDisposableをクラスそれは同様IDisposableインターを実装し、(処分するコールを転送すべきである)

  • :何を残すべき

    ブロックusing(...) { }のIDisposableクラス。あなたがunmamagedリソースを持っている(その場合でも、より良いオプションがある)場合を除き

  • はデストラクタと混乱しない

+0

私はpublic void Dispose()を呼び出すことができます { if(_context!= null) _context.Dispose(); } – Penguen

+0

@Phsika:他の方法と同じように呼び出すことができますが、using-blockが優れています。 –

+0

コーディングゴリラを見てください。どうすればpublic void Dispose()を呼び出すことができますか? { if(_context!= null) _context.Dispose(); } – Penguen

関連する問題