您最经常重复使用的课程是什么? [英] What's your most reused class?

查看:69
本文介绍了您最经常重复使用的课程是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间后,每个程序员最终都会得到一组实用程序类.其中有些是真正的编程明珠,并且可以在您的几个项目中重复使用.例如,在Java中:

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 java:

 class Separator {

        private String separator;
        private boolean called;

        public Separator(String aSeparator) {
            separator = aSeparator;
            called = false;
        }

        @Override
        public String toString() {
            if (!called) {
                called = true;
                return "";
            } else {
                return separator;
            }
        }
    }

和:

public class JoinHelper {

    public static <T> String join(T... elements) {
        return joinArray(" ", elements);
    }

    public static <T> String join(String separator, T... elements) {
        return joinArray(separator, elements);
    }

    private static <T> String joinArray(String sep, T[] elements) {
        StringBuilder stringBuilder = new StringBuilder();
        Separator separator = new Separator(sep);

        for (T element : elements) {
           stringBuilder.append(separator).append(element);
        }

        return stringBuilder.toString();
    }
}

您最常使用的类是什么?

推荐答案

具有日志记录和电子邮件功能的实用程序类.包含扩展方法的扩展类.基本上利用报告服务Web服务的报告类,并使其易于以excel,pdf等格式流式传输报告.

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.

示例...
1.)实用程序类(静态)

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.)扩展类

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

这篇关于您最经常重复使用的课程是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆