I'm implementing the repository pattern as part of an ASP.NET MVC site. Most examples I've seen of repositories are fairly simple. For example here's a typical abstract repository interface.
public interface IRepository<TEntity>
{
IQueryable<TEntity> All();
TEntity FindBy(int id);
TEntity FindBy(Expression<Func<TEntity, bool>> expression);
IQueryable<TEntity> FilterBy(Expression<Func<TEntity, bool>> expression);
bool Add(TEntity entity);
bool Update(TEntity entity);
bool Delete(TEntity entity):
}
It's clear to me how you would use a repository like this to add, update, delete, or get entities of a single type. But how do you handle creating and manipulating one-to-many or many-to-many relationships between different types?
Say you have an Item
type where each item is assigned to a Category
. How would you make this assignment through the repository? Should this be up to the Update(Category c)
and/or Update(Item i)
methods to figure out what relationships need to be made to or from the element being updated? Or should there be an explicit AssignCategoryToItem(Item i, Category c)
method?
If it makes any difference I'm using Fluent NHibernate to implement my concrete repositories.