2

Is there a way to specify the maximum number of related records allowed for an entity? For example, for each Order entity I want to specify a constraint that it has a maximum of five orderItems.

Would I have to use sql or is there something in the fluent api or ef attributes that can help?

atreeon
  • 21,799
  • 13
  • 85
  • 104

2 Answers2

3

I think that "a maximum of five orderItems per order" is a business requirement. Such requirements should not be implemented by infrastructure (mapping) or sql (although I'm not sure what you mean by that, I read it as database logic). An attribute that causes validation might be OK, but I don't think there is any attribute for it.

You should implement it in a way that validation and feedback occur similar to all other business rules. Rules implemented in mapping (if it were possible) or database constraints would require a second validation mechanism, probably catching exceptions, which is ugly.

Besides that, it is a rule that could change one day, maybe even temporarily (Christmas?). Then you don't want the implementation of this rule to be scattered over various application layers.

I would implement the rule in some AddItem method in a service class or repository or in the Order class itself and make the maximum configurable.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • Hi Gert, good points and yes I think ItemOrder should be more configurable so the validation logic should not be defined in sql or the mapping. What are your opinions around implementing the interface IValidatableObject and using self validating models? I'm thinking of doing a simple count and add a new ValidationResult if that count is greater than the maximum allowed. – atreeon Jan 31 '13 at 14:06
  • If by _self validating models_ you mean DTOs or view models that can be validated I think this is an excellent idea. I would welcome everything that helps centralizing and isolating validation logic. – Gert Arnold Jan 31 '13 at 14:31
  • Yup, that's exactly what I mean, I think that is a good strategy. Thanks for your input – atreeon Jan 31 '13 at 17:43
1

I would approach this something like what was done here:

Limit size of Queue<T> in .NET?

Also: How do I override List<T>'s Add method in C#?

Handle this by overriding whatever is handling you list of returned entities with an extended type which implements your business logic requirements. This will also make it easy to control the property from a settings file if you want to change it in the future.

I know of no way to do this in either EF / Fluent or SQL and it seems counter intuitive as this is relevant business logic and not relevant to how you persist the data. (*Not to say there isn't a way I don't know of)

Something like this should work:

public class LimitedList<T> : List<T> {
  private int limit = -1;

  public int Limit {
    get { return limit; }
    set { limit = value; }
  }

  private List<T> list= new List<T>();
  public LimitedList(int Limit) {
     this.Limit=Limit;
  }

  public void Add(T entry) {
     if (this.Limit != list.Count) {
       list.Add(entry);
     } else {
         //error
     }
  }
}
Community
  • 1
  • 1
Matthew
  • 9,851
  • 4
  • 46
  • 77