![](/img/trans.png)
[英]How do you create a unit-testing stub for an interface containing a read-only member?
[英]What should you do if you have an interface with a collection of objects and you want both a read-only and a read-write version of the interface?
我目前正在為用戶控件創建一些界面,這些界面將包含對象的集合。 我想為一個控件提供一個接口,該控件可以從外部添加對象,而另一個控件為不能從外部添加對象的接口。 到目前為止,我基本上有以下內容(簡化此問題;如果可以幫助您查看到目前為止我擁有的實際代碼,請告訴我):
public interface IReadableFoo
{
IReadOnlyCollection<Foo> Foos { get; }
}
public interface IReadWriteFoo : IReadOnlyFoo
{
new ICollection<Foo> Foos { get; }
}
在這種情況下,如果您具有一個帶有對象集合的接口並且想要同時擁有只讀版本和讀寫版本,那么被認為是“最佳實踐”? 像這樣IReadOnlyCollection<Foo>
的ICollection<Foo>
是否被認為可以接受?
還有其他我應該使用的模式嗎? IReadOnlyFoo
和IReadWriteFoo
是否IReadWriteFoo
從通用基本接口IFoo
繼承?
編輯:為了澄清我要做什么,我在這里添加正在使用的實際代碼:
public interface IOptionSelector<T>
{
IReadOnlyCollection<T> Options { get; }
T SelectedOption { get; }
event EventHandler SelectedOptionChanged;
}
public interface IWriteableOptionSelector<T> : IOptionSelector<T>
{
new ICollection<T> Options { get; }
new T SelectedOption { get; set; }
}
這是您應該做的:
public interface IOptionSelector<T>
{
event EventHandler SelectedOptionChanged;
}
public interface IReadOnlyOptionSelector<T> : IOptionSelector<T>
{
IReadOnlyCollection<T> Options { get; }
}
public interface IWritableOptionsSelector<T> : IOptionSelector<T>
{
ICollection<T> Options { get; }
}
ReadOnly和Writeable是兩個單獨的問題。 當您必須在編寫的方法上使用new關鍵字時,這清楚地表明您正在處理單獨的問題,需要重新考慮設計。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.