<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gd="http://schemas.google.com/g/2005"><id>tag:blogger.com,1999:blog-4500363753981919783.post110375373242031311..comments</id><updated>2023-10-03T22:06:50.708+08:00</updated><title type='text'>Comments on Huan-Lin 學習筆記: Repository,我可能不會用你 (2) - 範例</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://www.huanlintalk.com/feeds/comments/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html'/><link rel="hub" href="http://pubsubhubbub.appspot.com/"/><author><name>Michael Tsai</name><uri>http://www.blogger.com/profile/00364693770445538641</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>8</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-100038713854667008</id><published>2012-12-25T08:07:02.939+08:00</published><updated>2012-12-25T08:07:02.939+08:00</updated><title type='text'>是的,依應用程式的類型與架構來選擇合適的生命週期模型。也謝謝你的分享 :)</title><content type='html'>是的,依應用程式的類型與架構來選擇合適的生命週期模型。也謝謝你的分享 :)</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/100038713854667008'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/100038713854667008'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1356394022939#c100038713854667008' title=''/><author><name>Michael Tsai</name><uri>https://www.blogger.com/profile/00364693770445538641</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-1825873726"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/25 上午8:07"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-1709225867495272729</id><published>2012-12-25T01:12:57.659+08:00</published><updated>2012-12-25T01:12:57.659+08:00</updated><title type='text'>那我知道了,要依專案的架構來選擇&#xa;&#xa;沒有一定那種做法比較好&#xa;&#xa;我之前有開發一個瀑布流的分享圖文網站,...</title><content type='html'>那我知道了,要依專案的架構來選擇&lt;br /&gt;&lt;br /&gt;沒有一定那種做法比較好&lt;br /&gt;&lt;br /&gt;我之前有開發一個瀑布流的分享圖文網站,就是交給 EF 去自動 Dispose,它的每天的發文量有一定的數量,也沒有出現過 SQL Server Connection 爆掉的問題,看起來它還是值得信賴的 XD&lt;br /&gt;&lt;br /&gt;謝謝囉</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/1709225867495272729'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/1709225867495272729'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1356369177659#c1709225867495272729' title=''/><author><name>David</name><uri>https://www.blogger.com/profile/09534159092204216971</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-560032840"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/25 凌晨1:12"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-6724079365204666653</id><published>2012-12-24T23:24:24.362+08:00</published><updated>2012-12-24T23:24:24.362+08:00</updated><title type='text'>是的,我先前看過一篇老外的文章,他的實驗觀察是 EF 的 DbContext 已經會盡快把連線關閉。...</title><content type='html'>是的,我先前看過一篇老外的文章,他的實驗觀察是 EF 的 DbContext 已經會盡快把連線關閉。因此,DbContext 的壽命該多長,就不單單只是考量連線是否盡快釋放的而已了。</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/6724079365204666653'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/6724079365204666653'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1356362664362#c6724079365204666653' title=''/><author><name>Michael Tsai</name><uri>https://www.blogger.com/profile/00364693770445538641</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-1825873726"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/24 晚上11:24"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-2749480429202640676</id><published>2012-12-24T23:17:39.897+08:00</published><updated>2012-12-24T23:17:39.897+08:00</updated><title type='text'>Hi, &#xa;&#xa;要不要手動 Dispose 的問題也困擾我很久,之前有看過一些文章中說明為了要使用 EF...</title><content type='html'>Hi, &lt;br /&gt;&lt;br /&gt;要不要手動 Dispose 的問題也困擾我很久,之前有看過一些文章中說明為了要使用 EF 的延遲加載,所以不要手動 Dispose,而讓 EF 自動去控制 Dispose。&lt;br /&gt;&lt;br /&gt;我自己也有實際測試過,先將 Connection Pool 關掉&lt;br /&gt;private NorthwindEntities _context = new NorthwindEntities();&lt;br /&gt;不去寫 context.Dispose();   &lt;br /&gt;EF 也會自動把 connection close </content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/2749480429202640676'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/2749480429202640676'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1356362259897#c2749480429202640676' title=''/><author><name>David</name><uri>https://www.blogger.com/profile/09534159092204216971</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-560032840"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/24 晚上11:17"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-4695470959146632306</id><published>2012-12-10T02:06:10.365+08:00</published><updated>2012-12-10T02:06:10.365+08:00</updated><title type='text'>不好意思,晚了點回覆。&#xa;我把範例程式整理在另一篇文章了:&#xa;http://goo.gl/bSNnu</title><content type='html'>不好意思,晚了點回覆。&lt;br /&gt;我把範例程式整理在另一篇文章了:&lt;br /&gt;http://goo.gl/bSNnu</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/4695470959146632306'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/4695470959146632306'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1355076370365#c4695470959146632306' title=''/><author><name>Michael Tsai</name><uri>https://www.blogger.com/profile/00364693770445538641</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-1825873726"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/10 凌晨2:06"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-7700029293128409453</id><published>2012-12-07T15:06:52.409+08:00</published><updated>2012-12-07T15:06:52.409+08:00</updated><title type='text'>您好,感謝您的回覆,大致上了解了。&#xa;不過不太懂您提到的建議採用 by request 的方式來使用D...</title><content type='html'>您好,感謝您的回覆,大致上了解了。&lt;br /&gt;不過不太懂您提到的建議採用 by request 的方式來使用DbContext,這個的具體做法大概是怎樣呢?我好像沒看到相關的技術文章,不知道要怎麼實做,因您有提到,所以蠻感興趣想要試試這樣的方式,不知可否提點一下呢?謝謝您。</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/7700029293128409453'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/7700029293128409453'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1354864012409#c7700029293128409453' title=''/><author><name>HeartMoon</name><uri>https://www.blogger.com/profile/00513718660004813902</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-838257904"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/7 下午3:06"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-1491071941297329493</id><published>2012-12-06T19:57:19.539+08:00</published><updated>2012-12-06T19:57:19.539+08:00</updated><title type='text'>Hi HeartMoon,&#xa;你的顧慮是正確的。不好意思,我在貼範例程式碼的時候,沒有很仔細,漏掉了一...</title><content type='html'>Hi HeartMoon,&lt;br /&gt;你的顧慮是正確的。不好意思,我在貼範例程式碼的時候,沒有很仔細,漏掉了一些東西。你從範例程式中可以看到,CustomerService 是繼承自 ServiceBase。其實 ServiceBase 類別會負責建立 context 物件,然後有實作 Dispose 方法,並在其中 dispose context 物件。只是我在貼程式碼時,為了省版面,不把 ServiceBase 貼上來,於是在 CustomerService 中補了一行建立 context 的程式碼,卻漏掉 Dispose 方法了。&lt;br /&gt;&lt;br /&gt;至於是否要使用 using,其實有實作 IDisposable 介面就是安全的,只要用戶端記得呼叫 CustomerService 的 Dispose 方法。以 ASP.NET 程式來說,我會以 by request 的方式來建立 CustomerService(或者 DbContet 物件),亦即在 request 開始時建立,request 結束時釋放。這種做法,當然就不會在各個 methond 中使用 using。其好處是可以在整個 request 生命週期中享受 DbContext 的各種好處,如 caching、變更追蹤、交易管理等等。如果是 by method(使用 using),那麼 context 的生命週期就非常短--不是不能用,而是我通常會建議採用 by request 的方式來使用 DbContext。 &lt;br /&gt;&lt;br /&gt;最後,&amp;quot;是否會釋放資源?&amp;quot;,這個問題,若要精確的說,其實呼叫 context 物件的 Dispose 只是確保該物件本身的 unmanaged resource 獲得釋放(你可以視為 connection 會關閉),至於真正的資源釋放,還是得等到 GC 發生時才會執行回收。&lt;br /&gt;以上,希望有回答道你的問題。我稍晚也會把範例程式碼補完整,以免引起誤會。Thanks!</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/1491071941297329493'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/1491071941297329493'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1354795039539#c1491071941297329493' title=''/><author><name>Michael Tsai</name><uri>https://www.blogger.com/profile/00364693770445538641</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-1825873726"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/6 晚上7:57"/></entry><entry><id>tag:blogger.com,1999:blog-4500363753981919783.post-522822587911784261</id><published>2012-12-05T23:58:14.803+08:00</published><updated>2012-12-05T23:58:14.803+08:00</updated><title type='text'>您好,想請教一下,&#xa;我在 EF 跟 Repository模式相關國外文章看到不少關於Dispose的...</title><content type='html'>您好,想請教一下,&lt;br /&gt;我在 EF 跟 Repository模式相關國外文章看到不少關於Dispose的文章,想請問EF的Context直接使用,不用Using包住真的沒有問題嗎?如您的例子也是直接&lt;br /&gt;private NorthwindEntities _context = new NorthwindEntities();&lt;br /&gt;對這感到困惑,不知它是否真的會完全釋放資源,謝謝您。</content><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/522822587911784261'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/4500363753981919783/110375373242031311/comments/default/522822587911784261'/><link rel='alternate' type='text/html' href='https://www.huanlintalk.com/2012/12/repository-2_5.html?showComment=1354723094803#c522822587911784261' title=''/><author><name>HeartMoon</name><uri>https://www.blogger.com/profile/00513718660004813902</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='https://www.huanlintalk.com/2012/12/repository-2_5.html' ref='tag:blogger.com,1999:blog-4500363753981919783.post-110375373242031311' source='http://www.blogger.com/feeds/4500363753981919783/posts/default/110375373242031311' type='text/html'/><gd:extendedProperty name="blogger.itemClass" value="pid-838257904"/><gd:extendedProperty name="blogger.displayTime" value="2012/12/5 晚上11:58"/></entry></feed>