Imagine you have an entity that has some relations with other entities and you want to load only some of these entities to display them in diferent views.
For example, given this entity:
public class Category
{
public int id;
public Category child;
public Category parent;
}
In the view "ShowChild" you don't want to load the "parent" property since you are not showing it.
So, given this scenario I implemented a very nice "system" in my repository to load entities from DB filling only the properties I want. It works this way:
Category category = repo.FindCategory(id, (int)(LoadLevel.basic | LoadLevel.Child))
So now I have a category instance with only the id and Child properties loaded.
The dilemma I'm facing here is that if I define the LoadLevel in my serviceLayer (where it should be) I'll have to write two methods, "LoadCategoryWithChild" and "LoadCategoryWithParent" in my service class, one for each view (DRY violation?).
public class CategoryService
{
public Category LoadCategoryWithChild(int id)
{
int loadlevel = (int) (LoadLevel.Basic | LoadLevel.Child);
return repo.FindCategory(id, loadlevel);
}
}
OR, the other option I see is to define the loadlevel in the controller (MVC violation?) and implement just one method in my service class:
public class CategoryService
{
public Category LoadCategory(int id, int loadlevel)
{
return repo.FindCategory(id, loadlevel);
}
}
Which option would be better? I think that the DRY violation is much worse since it implies writing a lot of redundant code.