How to avoid “too many parameters” problem in API design?

前端 未结 13 1701
别那么骄傲
别那么骄傲 2020-11-27 09:01

I have this API function:

public ResultEnum DoSomeAction(string a, string b, DateTime c, OtherEnum d, 
     string e, string f, out Guid code)
13条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 09:35

    Use the structure, but instead of public fields, have public properties:

    •Everybody (including FXCop & Jon Skeet) agree that exposing public fields are bad.

    Jon and FXCop will be satisified because you are exposing properites not fields.

    •Eric Lippert et al say relying on readonly fields for immutability is a lie.

    Eric will be satisifed because using properties, you can ensure that the value is only set once.

        private bool propC_set=false;
        private date pC;
        public date C {
            get{
                return pC;
            }
            set{
                if (!propC_set) {
                   pC = value;
                }
                propC_set = true;
            }
        }
    

    One semi-immutable object (value can be set but not changed). Works for value and Reference types.

提交回复
热议问题