public virtual void BindModel(TModel model, IEnumerable <string> args) { var unnamedArgs = args.TakeWhile(s => IsSwitch(s) == false); EnsureCorrectNumberOfOrdinalArguments(unnamedArgs); for (var i = 0; i < OrdinalArguments.Count; i++) { MemberBindingDefinition <TModel> member = (MemberBindingDefinition <TModel>)GetMemberBindingDefinition(OrdinalArguments[i]); if (member == null) { throw new BindingDefinitionException(String.Format("Member {0} in {1} does not have a BindingDefinition defined.", OrdinalArguments[i].Name, typeof(TModel).FullName)); } if (OrdinalArguments.Count != i + 1) { HandleInputs(new[] { unnamedArgs.Skip(i).First() }, model, member); } else { //last ordinal, send remaining ordinal arguments HandleInputs(unnamedArgs.Skip(i), model, member); } } var switchedArguments = args.Skip(unnamedArgs.Count()); var namedMembers = Members.Where(m => OrdinalArguments.Contains(m.Key) == false); foreach (var member in namedMembers) { //find a suitable switch var argument = switchedArguments.SkipWhile(a => a.StartsWith(SwitchDelimiter) == false || a.Length <= SwitchDelimiter.Length || member.Value.CanHandleSwitch(a.Substring(SwitchDelimiter.Length)) == false); if (argument.Any()) { //take all arguments until the next switch is found, skipping the first, which is the switch itself var argumentValue = argument.Skip(1).TakeWhile(a => a.StartsWith(SwitchDelimiter) == false); HandleInputs(argumentValue, model, member.Value); } else { if (member.Value.Required == true) { throw new InvalidOperationException(String.Format(Properties.Resources.RequiredParameterNotProvidedMessage, member.Value.MemberInfo.Name)); } //switch not provided if (member.Value.DefaultValue != null) { member.Key.SetValue(model, member.Value.DefaultValue); } } } }
public virtual IMemberBindingDefinition <TModel> GetOrCreateMemberBindingDefinition(MemberInfo member) { var returnValue = GetMemberBindingDefinition(member) as MemberBindingDefinition <TModel>; if (returnValue == null) { returnValue = new MemberBindingDefinition <TModel>(member, this); Members.Add(member, returnValue); } return(returnValue); }
protected virtual void HandleInputs(IEnumerable <string> arguments, TModel model, MemberBindingDefinition <TModel> memberBindingDefinition) { var result = memberBindingDefinition.CoerceValue(arguments, model); memberBindingDefinition.SetMemberValue(result, model); }