[英]CA1001 Visual Studio 2012 Code Analysis warning. What does it mean?
它並不重要,但我想弄清楚它告訴我什么,這是一個合理的警告嗎? 有人可以用簡單的術語解釋這個錯誤嗎?
CA1001擁有一次性領域的類型應該是一次性的
在'MemVoteManager'上實現IDisposable,因為它創建了以下IDisposable類型的成員:'CongressDBEntities'。 如果先前發布了“MemVoteManager”,則將實現IDisposable的新成員添加到此類型將被視為對現有使用者的重大更改。
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;
}
您可以實現IDisposable
以便在消費者完成您的課程時處理上下文,但您可能最好不要讓上下文成為該類的成員。 只需在需要時創建它並在完成后將其丟棄:
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;
}
上下文是輕量級的,因此每次創建它們都沒有太大的代價。 此外,您不必擔心消費者會通知您處理上下文,並且如果多次使用該類的一個實例,則內存中沒有很多內置更改。
它讓你知道現場context
包含一次性成員。 這意味着那些成員需要調用Dispose()
,以便可以進行垃圾收集。 因此,它希望您在MemVoteManager
上實現接口IDisposable
,以便您可以在上下文和/或其一次性成員上調用Dispose()
。
所以修改你的代碼:
public class MemVoteManager : AbstractDataManager, IMemVoteManager, IDisposable
然后實現IDisposable
接口的成員,如下所示:
public void Dispose()
{
// call dispose on the context and any of its members here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.