I have two generic save methods in a repository class:
public void Save<T>(T entity)
{
_session.Save(entity);
}
public void Save<T>(IEnumerable<T> entities)
{
foreach (var item in entities)
{
_session.Save(item);
}
}
However, when I use Save(collection) (which infers the type automatically), it recognizes it as a T rather than IEnumerable<T> and tries to save it using the first method.
How do I write this save method(s) so that it can handle either case, without me having to explicitly provide the type?
Put the type parameter at the class level on your repository instead of at the method level, this way the overload will not be ambiguous.
You’d use it like this: