As someone who is always trying to work with the DRY principle I try to write code that can do most of what is required without being repetitive.
Note Code is just for example
Normally most software uses the standard pattern as follows:
public interface IRepository
{
public Entity GetById(string id);
public List<Entity> GetAll();
public List<Entity> GetBySpecificCriteria{Criteria criteria};
}
This above is pretty standard and nothing special.
What i prefer as most of the above can contain repetative code is something like the following:
public interface IRepository
{
public List<Entity> GetEntity(EntityRequest request);
}
public class EntityRequest
{
public int Id{get;set;}
public Criteria Criteria{get;set;}
}
Using this I can then check if I want an Entity by Id or by criteria and either way just return a List of Entities from a single method.
When requesting a single Entity I will pass in the Id value and get the Entity as GetEntity(request)[0];
I am just wondering if there is any downside to this as from my point of view maintenance is easier and it seems a lot cleaner than having different methods for everything.
Or is it better to seperate each method to return nearly the same result.
Aucun commentaire:
Enregistrer un commentaire