What's your most reused class?

前端 未结 2 635
盖世英雄少女心
盖世英雄少女心 2021-02-12 19:11

Every programmer ends up with a set of utility classes after a while. Some of them are true programming pearls and they are reused in several of your projects. For example, in j

2条回答
  •  无人及你
    2021-02-12 19:46

    A utility class that has logging and email functionality. An extensions class that contains extension methods. A reporting class that basically harness the reporting services web service and makes it easy to stream reports as excel, pdf, etc.

    Examples...
    1.) Utility Class (static)

       public static void LogError(Exception ex)
        {
            EventLog log = new EventLog();
            if (ex != null)
            {
                log.Source = ConfigurationManager.AppSettings["EventLog"].ToString();
                StringBuilder sErrorMessage = new StringBuilder();
                if (HttpContext.Current.Request != null && HttpContext.Current.Request.Url != null)
                {
                    sErrorMessage.Append(HttpContext.Current.Request.Url.ToString() + System.Environment.NewLine);
                }
                sErrorMessage.Append(ex.ToString());
                log.WriteEntry(sErrorMessage.ToString(), EventLogEntryType.Error);
            }
        }
    

    2.) Extensions Class

       public static IEnumerable WhereIf(this IEnumerable source, bool condition, Func predicate)
        {
            if (condition)
                return source.Where(predicate);
            else
                return source;
        }
    

提交回复
热议问题