我正在构建某种存储库的原型,我想知道仅对序列进行操作的方法是否足够。
例如:
public void AddEntities<T>(IQueryable<T> sequence);
现在,这个方法原型看起来不错,而且确实通用,但是当有人想做这样的事情时,它就很糟糕:
var t = new T();
// Add just a single entity.
repository.AddEntities(???);
这种情况的解决方案是什么?
我应该做我的界面更大并添加诸如 AddSingleEntity(T t)
和 RemoveSingleEntity(T t)
之类的方法,或者我应该保持原样并使用类似的方法:
repository.AddEntities(new List { new T() }.AsQueryable());
两种选择显然都有缺点:第一个使界面更丑陋且不太紧凑,第二个看起来有点奇怪。
你会做什么以及为什么?
I'm prototyping some sort of repository and I want to know if it's enough to have methods, that operate on sequences only.
For example:
public void AddEntities<T>(IQueryable<T> sequence);
Now, this method prototype seems fine and really generic, but it sucks when somebody wants to do something like:
var t = new T();
// Add just a single entity.
repository.AddEntities(???);
What are the solutions for this case?
Should I make my interface larger and add methods like AddSingleEntity<T>(T t)
and RemoveSingleEntity<T>(T t)
or should I leave it as it is and use something like:
repository.AddEntities(new List { new T() }.AsQueryable());
Both choices obviously have drawbacks: the first one makes the interface uglier and less compact, the second simply looks a bit weird.
What would you do and why?
发布评论
评论(3)
我会创建额外的方法来处理单个实体操作,因为它看起来更干净,并且很多通用接口都可以做到这一点(列表)
编辑:如果我只想有一个方法组,我会做什么:
你现在可以调用:
I would create additional methods to handle single entities operations, because it will look cleaner and a lot of common interfaces do it (List)
EDIT: what I do if I want to have only one method group:
you can now call:
为什么不添加
repository.AddEntity(T t)
?简单又美好。看起来有点过度设计。why not to add
repository.AddEntity(T t)
? Simple and nice. Looks like overengeneering a bit.在这种特殊情况下,我不会使用 IQueryable 进行操作。这是一个询问。
阅读 这篇有用的文章。发送数据时使用IEnumerable。
I would not operate with IQueryable in this particular case. It's a query.
Read this useful article. Use IEnumerable when sending data.