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 choic开发者_StackOverflow社区es 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?
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:
public void Add<T>(IEnumerable<T> entities) { ... }
public void Add<T>(params T[] entities) { this.Add(entities.AsEnumerable()); }
you can now call:
repo.Add(entity);
repo.Add(entity1, entity2);
repo.Add(entityList);
why not to add repository.AddEntity(T t)
? Simple and nice. Looks like overengeneering a bit.
I would not operate with IQueryable in this particular case. It's a query. Read this useful article. Use IEnumerable when sending data.
精彩评论