Data Layer Best Practices

后端 未结 8 1383
甜味超标
甜味超标 2021-02-04 12:43

I am in the middle of a \"discussion\" with a colleague about the best way to implement the data layer in a new application.

One viewpoint is that the data layer should

8条回答
  •  面向向阳花
    2021-02-04 13:27

    One trick I've found handy is to have my data layer be "collection agnostic". That is, whenever I want to return a list of objects from my data layer, I get the caller to pass in the list. So instead of this:

    public IList GetFoosById(int id) { ... }
    

    I do this:

    public void GetFoosById(IList foos, int id) { ... }
    

    This lets me pass in a plain old List if that's all I need, or a more intelligent implementation of IList (like ObservableCollection) if I plan to bind to it from the UI. This technique also lets me return stuff from the method like a ValidationResult containing an error message if one occurred.

    This still means that my data layer knows about my object definitions, but it gives me one extra degree of flexibility.

提交回复
热议问题