private static AuditLogEntry CreateCountChangeEntry(SrmDocument document, MessageType singular, MessageType plural, int count, MessageArgs singularArgs, MessageArgs pluralArgs) { switch (count) { case 1: return(CreateSimpleEntry(document, singular, singularArgs.Args)); default: return(CreateSimpleEntry(document, plural, pluralArgs != null ? pluralArgs.Args : MessageArgs.Create(count).Args)); } }
/// <summary> /// Creates an entry that depends on whether there are 1 or multiple elements /// in a collection. /// </summary> /// <param name="document">Document change was made to</param> /// <param name="singular">Message to show if there's 1 element in the collection. Only element gets passed as argument to the message</param> /// <param name="plural">Message to show if there are multiple elements. The count gets passed to the message</param> /// <param name="items">Items to consider</param> /// <param name="count">Number of elements in IEnumerable. If null, all items are enumerated</param> /// <param name="singularArgsFunc">Converts the element to MessageArgs that get passed to the singular message</param> /// <param name="pluralArgs">Args to be passed to plural. If null, count is passed as single arg</param> public static AuditLogEntry CreateCountChangeEntry <T>(SrmDocument document, MessageType singular, MessageType plural, IEnumerable <T> items, int?count, Func <T, MessageArgs> singularArgsFunc, MessageArgs pluralArgs) { if (!count.HasValue) { var collection = items as ICollection <T> ?? items.ToArray(); return(CreateCountChangeEntry(document, singular, plural, collection, singularArgsFunc, pluralArgs)); } var singularArgs = count.Value == 1 ? singularArgsFunc(items.FirstOrDefault()) : null; return(CreateCountChangeEntry(document, singular, plural, count.Value, singularArgs, pluralArgs)); }
/// <summary> /// Creates an entry that depends on whether there are 1 or multiple elements /// in a collection. /// </summary> /// <param name="document">Document change was made to</param> /// <param name="singular">Message to show if there's 1 element in the collection. Only element gets passed as argument to the message</param> /// <param name="plural">Message to show if there are multiple elements. The count gets passed to the message</param> /// <param name="items">Items to consider</param> /// <param name="singularArgsFunc">Converts the element to MessageArgs that get passed to the singular message</param> /// <param name="pluralArgs">Args to be passed to plural. If null, count is passed as single arg</param> public static AuditLogEntry CreateCountChangeEntry <T>(SrmDocument document, MessageType singular, MessageType plural, ICollection <T> items, Func <T, MessageArgs> singularArgsFunc, MessageArgs pluralArgs) { var singularArgs = items.Count == 1 ? singularArgsFunc(items.FirstOrDefault()) : null; return(CreateCountChangeEntry(document, singular, plural, items.Count, singularArgs, pluralArgs)); }