views:

43

answers:

1

I have a pretty standard Repository pattern going, where repositories are injected into my MVC controller at construction. Repositories are initialized once per AppDomain and are shared by other controllers. Repositories access the database using NHibernate ISessions and ICriteria, but access is exposed using ListXYZ methods instead of accepting ICriteria directly.

I now need a neat way to restrict the results returned in certian cases, one such may for example be listing all products in a given market, known only when an action is called.

I don't want to pass this restriction in at each call to a ListXYZ method since it would lead to a lot of overloaded methods. Ideally I would like to be able to access the repository like before but with the restrictions applied. Something like:

var rep = new ProductRepository().ApplyRestriction(new MarketRestriction("xy"));
var list = rep.ListInCategory("shirts");

Any suggestions How I should do this? Or am I making it too complicated for myself?

A: 

You can try the specification pattern for this. It's a bit strange that you use 2 restrictions (specifications) in a different way. I would do

var rep = new Repository<Product>();
var specification = new MarketSpecification("xy") && new CategorySpecification("shirts");
var list = rep.Find(specification);
Paco
Thanks, I long avoided this because I thought it would be too complicated to implement. But once I got around to it it didnt seem too bad and it works very well.
kaa