GXPT(五)——Partial加深你对面向对象思想的理解


******含义:

C#2.0引入了局部类型的概念。局部类型允许我们将一个类、结构或接口分成几个部分,分别实现在不同几个不同的.CS文件中。


******适用情况:

1)类特别大,不适宜放在一个文件中实现

2)一个类中的代码的一部分为自动化工具生成的代码,不宜与我们自己编写的代码混合在一起。

3)需要多人合作编写一个类。


******实例DeMO

采用T4模板生成的公用工厂代码类:由工具生成的公用的类。

namespace ITOO.Exam.BLLFactory
{
    public partial class BLLAbstractFactory
    {
	    private static object GetInstance(string assembly, string className)
        {
            var  obj = Assembly.Load(assembly).CreateInstance(className, true);
            return obj;
        }

	    public static IExamAnswerRecordEntityBLL GetExamAnswerRecordEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamAnswerRecordEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamAnswerRecordEntityBLL;
        }

	    public static IExamApplyExamEntityBLL GetExamApplyExamEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamApplyExamEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamApplyExamEntityBLL;
        }

	    public static IExamCheatControlEntityBLL GetExamCheatControlEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamCheatControlEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamCheatControlEntityBLL;
        }

	    public static IExamCourseQuestionTypeBLL GetExamCourseQuestionTypeBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamCourseQuestionTypeBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamCourseQuestionTypeBLL;
        }

	    public static IExamDetailsPaperEntityBLL GetExamDetailsPaperEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamDetailsPaperEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamDetailsPaperEntityBLL;
        }

	    public static IExamDictionaryEntityBLL GetExamDictionaryEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamDictionaryEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamDictionaryEntityBLL;
        }

	    public static IExamDictionaryOperationRecordEntityBLL GetExamDictionaryOperationRecordEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamDictionaryOperationRecordEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamDictionaryOperationRecordEntityBLL;
        }

	    public static IExamDictionaryTypeEntityBLL GetExamDictionaryTypeEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamDictionaryTypeEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamDictionaryTypeEntityBLL;
        }

	    public static IExamDictionaryTypeOperatingRecordEntityBLL GetExamDictionaryTypeOperatingRecordEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamDictionaryTypeOperatingRecordEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamDictionaryTypeOperatingRecordEntityBLL;
        }

	    public static IExamEndTestSettingEntityBLL GetExamEndTestSettingEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamEndTestSettingEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamEndTestSettingEntityBLL;
        }

	    public static IExamErrorRecordEntityBLL GetExamErrorRecordEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamErrorRecordEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamErrorRecordEntityBLL;
        }

	    public static IExamExamEntityBLL GetExamExamEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamExamEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamExamEntityBLL;
        }

	    public static IExamExamineeExamEntityBLL GetExamExamineeExamEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamExamineeExamEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamExamineeExamEntityBLL;
        }

	    public static IExamExamRoomPlanEntityBLL GetExamExamRoomPlanEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamExamRoomPlanEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamExamRoomPlanEntityBLL;
        }

	    public static IExamFeedBackRecordEntityBLL GetExamFeedBackRecordEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamFeedBackRecordEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamFeedBackRecordEntityBLL;
        }

	    public static IExamHardwareEntityBLL GetExamHardwareEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamHardwareEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamHardwareEntityBLL;
        }

	    public static IExamPaperEntityBLL GetExamPaperEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamPaperEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamPaperEntityBLL;
        }

	    public static IExamPapeReportCardEntityBLL GetExamPapeReportCardEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamPapeReportCardEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamPapeReportCardEntityBLL;
        }

	    public static IExampEmpowerEntityBLL GetExampEmpowerEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExampEmpowerEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExampEmpowerEntityBLL;
        }

	    public static IExamPerformanceScoreEntityBLL GetExamPerformanceScoreEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamPerformanceScoreEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamPerformanceScoreEntityBLL;
        }

	    public static IExamQuestionBankEntityBLL GetExamQuestionBankEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamQuestionBankEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamQuestionBankEntityBLL;
        }

	    public static IExamQuestionTypeDetailsEntityBLL GetExamQuestionTypeDetailsEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamQuestionTypeDetailsEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamQuestionTypeDetailsEntityBLL;
        }

	    public static IExamQuestionTypeEntityBLL GetExamQuestionTypeEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamQuestionTypeEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamQuestionTypeEntityBLL;
        }

	    public static IExamScoreLogEntityBLL GetExamScoreLogEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamScoreLogEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamScoreLogEntityBLL;
        }

	    public static IExamTemplateDetailsEntityBLL GetExamTemplateDetailsEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamTemplateDetailsEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamTemplateDetailsEntityBLL;
        }

	    public static IExamTemplateEntityBLL GetExamTemplateEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamTemplateEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamTemplateEntityBLL;
        }

	    public static IExamTempQuestionBankEntityBLL GetExamTempQuestionBankEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamTempQuestionBankEntityBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamTempQuestionBankEntityBLL;
        }

	    public static IExamVirtualExamineeBLL GetExamVirtualExamineeBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamVirtualExamineeBLL";
            var obj  = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamVirtualExamineeBLL;
        }

	}
}
 

自己扩展编写的工厂类:在上面的基础上,扩展我们自己的工厂方法

namespace ITOO.Exam.BLLFactory
{
    /**********************************************************************************
     * 开发人:SYQ
     * 开发组:开发小组(九期十期考试组:)
     * 接口说明: B层工厂类,当添加“ServicesBLL T4模板生成类”以外的B层类的时候,请在此类下添加工厂反射。
     * 可以直接调用“AbstractFactory T4”模板中生成的方法“GetInstance”
     * 开发时间:2014年12月15日23:50:41
     * 开发版本:1.0
    * *********************************************************************************/
    public partial class BLLAbstractFactory
    {
        /// <summary>
        /// 获得在线添加试题的接口IExamOnlineAddQuestionBLL——WHY
        /// </summary>
        /// <returns></returns>
        public IExamOnlineAddQuestionBLL GetExamAddQuestionBLL() 
        {
            string FullClassName = ConfigurationManager.AppSettings["BLLNameSpace"] + "ExamOnlineAddQuestionBLL";
            var obj = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], FullClassName);
            return obj as IExamOnlineAddQuestionBLL;
        }

        /// <summary>
        ///  题型管理的 B层外观类    ——ZC
        /// </summary>
        /// <returns></returns>
        public static IExamFaQuestionTypeEntityBLL GetExamFaQuestionTypeEntityBLL()
        {
            string classFullName = ConfigurationManager.AppSettings["BLLNameSpace"] + ".ExamQuestionTypeEntityBLL";
            var obj = GetInstance(ConfigurationManager.AppSettings["BLLAssembly"], classFullName);
            return obj as IExamFaQuestionTypeEntityBLL;
        }
    }
}

******总结:

1)将公用的代码抽象出来,提高代码的复用性

2)与接口、抽象类的基础上,还提供了多人合作的方式。

3)代码扩展。

常常在考虑,面向对象的三个基本特征:抽象,继承,封装是如何在代码中体现的呢?去年的这个时候对这三个词的体会还只是在停留在字面意思,如今在理解了泛型,委托,集合等概念的基础上再理解这三个词,已经具象了许多。对于部分的体验,个人理解也应该基于这三个字去理解。其实微软的任何一个细微之处的设计都值得我们好好的去学习与体会的。 

你可能感兴趣的:(GXPT(五)——Partial加深你对面向对象思想的理解)