早上的帖子:http://topic.csdn.net/u/20120104/10/a1b106af-1d39-4fdd-89df-735ddca0de90.html
http://topic.csdn.net/u/20111214/18/9c3ec9fc-6da1-4873-8e3e-5f07cfb2d980.html修改后的代码,继续请教大牛,是否还需要完善?//根据不同等级,读取不同等级会员的模版,
    public class TemplateRule
    {
        public TemplateRule(int LevelID, string Page)
        {
            this.Page = Page;
            this.LevelID = LevelID;
            this.Path = string.Format("/Template/{0}/{1}.html", this.levelID, this.Page);
        }
        /// <summary>
        /// 是否需要缓存
        /// </summary>
        private bool IsCache { get; set; }
        /// <summary>
        /// 缓存键值
        /// </summary>
        private string CacheKey { get; set; }
        /// <summary>
        /// 路径
        /// </summary>
        private string Path { get; set; }
        /// <summary>
        /// 页面
        /// </summary>
        private string Page { get; set; }
        /// <summary>
        /// 会员等级
        /// </summary>
        private int levelID;
        private int LevelID
        {
            get { return levelID; }
            set
            {
                levelID = value;
                string Rule = ConfigurationManager.AppSettings[levelID.ToString()];
                IsCache = Rule.Length > 0 ? Convert.ToBoolean(Rule) : false;
                if (IsCache)
                    CacheKey = ConfigurationManager.AppSettings[levelID + "CacheKey"] + this.Page;
            }
        }
        /// <summary>
        /// 获取模版
        /// </summary>
        /// <returns></returns>
        public string GetTemplate()
        {
            string template = string.Empty;
            if (!File.Exists(HttpContext.Current.Server.MapPath(this.Path)))
                return template;
            if (IsCache)
            {
                if (HttpContext.Current.Cache[CacheKey] == null)
                    HttpContext.Current.Cache.Insert(CacheKey, ReadTemplate(), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20));
                template = HttpContext.Current.Cache[CacheKey].ToString();
            }
            else
                template = ReadTemplate();
            return template;
        }
        /// <summary>
        /// 读取模版
        /// </summary>
        /// <returns></returns>
        protected string ReadTemplate()
        {
            string result = string.Empty;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(HttpContext.Current.Server.MapPath(this.Path), Encoding.GetEncoding("gb2312"));
                result = sr.ReadToEnd();
            }
            catch
            {
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                    sr.Dispose();
                }
            }
            return result;
        }
    }配置文件配置是否需要开启缓存,以及设置缓存键值  <add key="10003" value="true" />
  <add key="10002" value="false" />
  <add key="10001" value="false" />
  <add key="10000" value="false" />
  <add key="10003CacheKey" value="Diamond"/>
//页面工厂,针对不同页面进行处理
    public class PageFactory
    {
        private string page;
        private string template;
        private Model.CompanyTbl model;
        public PageFactory(string page, string template,Model.CompanyTbl model)
        {
            this.page = page;
            this.template = template;
            this.model = model;
        }
        public string GetPage()
        {
            if (template.Length == 0)
                return template;
            switch (this.page)
            {
                case "Index":
                    return new PageHandler(template, model).GetIndex();
                case "News":
                    return new PageHandler(template, model).GetNews();
                //....
                default:
                    return template;
            }
        }
    }
//页面调用
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Common.RegexCommon.IsNumber(Request.QueryString["ID"]))
            {
                int CID = Convert.ToInt32(Request.QueryString["ID"]);
                string Page = string.IsNullOrEmpty(Request.QueryString["PageName"]) ? "Index" : Request.QueryString["PageName"];
                Model.CompanyTbl model = new GetCompanyModel(CID);
                if (model != null)
                {
                    TemplateRule tr;
                    string template = string.Empty;
                    if (model.CompPassed > 0)
                        tr = new TemplateRule(model.LevelID, Page);
                    else
                        tr = new TemplateRule(10000, Page);
                    template = tr.GetTemplate();
                    PageFactory pf = new PageFactory(Page, template, model);
                    Response.Write(pf.GetPage());
                }
            }
        }

解决方案 »

  1.   

    csdn个人空间怎么用不了了。
    想去留言下都不行了。
      

  2.   

    string Rule = ConfigurationManager.AppSettings[levelID.ToString()];
    -----------------
    switch (this.page)
                {
                    case "Index":
                        return new PageHandler(template, model).GetIndex();
                    case "News":
                        return new PageHandler(template, model).GetNews();
                    //....
                    default:
                        return template;
                }
    --------------------
    问题依然存在啊
      

  3.   

    有修改了这个地方,换成从xml获取
     switch (levelID)
                {
                    case 1:
                        baseClass = new Diamond(levelID);
                        break;
                    case 2:
                        baseClass = new Gold(levelID);
                        break;
                    case 3:
                        baseClass = new Silver(levelID);
                        break;
                    case 4:
                        baseClass = new General(levelID);
                        break;
                    default:
                        baseClass = new General(10000);
                        break;
                }
      

  4.   

    换成从xml读取相应配置,然后根据参数直接返回相应会员的模版去进行替换。
    上面那个switch(this.page)这个不知道该怎么改。
    因为要根据页面参数,然后根据这个参数去读取这个页面的模版进行替换。
    麻烦非哥指点下/
      

  5.   

    TemplateRule 里不应该有类似 ConfigurationManager.AppSettings 的东东出现它只是根据参数进行对应的逻辑处理,相应的信息都应该是存储在已知的参数(属性,字段)中的可能你需要一个类似 TemplateRuleBuilder or TemplateRuleFactory 的东东它主要的工作就是根据配置文件构建对应的 TemplateRule 关于 
    switch (this.page)
                {
                    case "Index":
                        return new PageHandler(template, model).GetIndex();
                    case "News":
                        return new PageHandler(template, model).GetNews();
                    //....
                    default:
                        return template;
                }如果你的环境支持的话,可以用表达式树来完成此工作思想是一样,根据不同的参数返回类型一样的东东 可能是类也可能是方法句柄
      

  6.   


    如果是单参数的话,像index news这类的,用一个Directory<string,delegate>可以实现类似的功能,就是有点慢。但是放在TemplateRule里确实不应该。
      

  7.   

    不过是个模版嘛...就算你要对象化它也只是个模版对象,跟会员有个蛋关系...至于什么金牌银牌扑克牌会员,只不过是个会员等级,就算你要对象化它也只是个会员对象的属性,跟模版有个蛋关系...那模版跟谁有关系呢,不是页面而是模版管理器...页面套用某个模版,或者某个等级的会员应该套用哪个模版,都只是模版管理器的action而已...读取会员实例等级属性->读取等级预设模版名->加载模版,模版管理器的几个动作而已,搞那么复杂干什么...
      

  8.   

    Directory<string,Func<T>> 不太适用于这里 
      

  9.   

    谢谢非哥指点。你这里说的
    可能你需要一个类似 TemplateRuleBuilder or TemplateRuleFactory 的东东
    是建一个这种类,然后把本来的属性,page,path等提取出来放到里面,然后只保留一个TemplateRuleBuilder这个类
    是这个意思吗?
      

  10.   

    有关后面那个swich,我的确没想明白为啥会有这种要求如果你需要调用方法去填充数据,那么用委托是可以滴。当然这里的选择可以有很多种方式可以选择工厂方法可以选择AOP类似特性注入如果是我个人,在net4下,我会使用工厂方法+MEF的ImportMany方式完成1.使用工厂方法,将你的getIndex,GetNews()全拆成独立类(而非lz提供的整体抽象类,抽象工厂的麻烦就是你必须固定方法,或者不固定而使用反射)2.在net4下利用MEF的ImportMany 自动引入所有方法集合,至于后面调用只是利用表查询直接查询出方法并调用
      

  11.   

    这个需求是这样的,
    比如www.xxxx.com/CompanyID/Index.html------这个对应的是首页
    比如www.xxxx.com/CompanyID/News.html------这个对应的是新闻页
    而我根据Index,News这个参数,来加载相对应的首页,新闻页模版。
    刚开始有想到用表驱动法和反射,可是表驱动法,一开始要全部加载进来,怕影响效率。
    而如果用反射去实例化相应摸块,也是怕对效率有影响。
    我只是个初学者,只是想设计的面向对象点,封装好点,方便以后扩展。以上您,非哥,螃蟹哥提到的很多都没接触过,非常感谢你们的指点。
    至于您说的那个用委托,我写了个,可是还是要switch一个个枚举出来额,可能我不了解您的想法,麻烦在指点下。工厂方法我也想了下,好象也是要switch出来!        public delegate string PageDelegate(string Template, int CID);
            public string GetPage()
            {
                PageDelegate pd = null;
                if (template.Length == 0)
                    return template;
                switch (this.page)
                {
                    case "Index":
                        pd = new PageDelegate(GetIndex);
                        break;
                    case "News":
                        pd = new PageDelegate(GetNews);
                        break;
                    default:
                        break;
                }
                if (pd == null)
                    return string.Empty;
                return pd(template, 7);
            }
      

  12.   

    用的是3.5,支持linq,不过表达式树还不是很理解,等下去熟悉下。至于上面上的,非哥的意思是这样吗?我改了下
        public class TemplateRuleBuilder
        {
            public TemplateRuleBuilder(int LevelID, string Page)
            {
                this.page = Page;
                this.path = string.Format("/Template/{0}/{1}.html", LevelID, Page);
                isCache = GetSetting(LevelID.ToString()) == "true" ? true : false;
                cacheKey = isCache ? GetSetting(LevelID + "CacheKey") + Page : "";
            }
            public string GetSetting(string key)
            {
                return ConfigurationManager.AppSettings[key];
            }
            private bool isCache;
            /// <summary>
            /// 是否需要缓存
            /// </summary>
            public bool IsCache
            {
                get { return isCache; }
                set { isCache = value; }
            }
            private string cacheKey;
            /// <summary>
            /// 缓存键值
            /// </summary>
            public string CacheKey
            {
                get { return cacheKey; }
                set { cacheKey = value; }
            }
            private string path;
            /// <summary>
            /// 模版路径
            /// </summary>
            public string Path
            {
                get { return path; }
                set { path = value; }
            }
            private string page;
            /// <summary>
            /// 模版文件名
            /// </summary>
            public string Page
            {
                get { return page; }
                set { page = value; }
            }
    而TemplateRule的只用TemplateRuleBuilder这个属性        public TemplateRule(int LevelID, string Page)
            {
                trb = new TemplateRuleBuilder(LevelID, Page);
            }
            /// <summary>
            /// 模板相关属性
            /// </summary>
            private TemplateRuleBuilder trb;
      

  13.   

    不是这个意思,是保留你的  TemplateRule 。但是  TemplateRule 的构建需要
     TemplateRuleBuilder or TemplateRuleFactory 
    TemplateRule 不需要关心  内部属性的取值
    只关心它要做的事就可以了
      

  14.   

    其实我白了,你这是被博客园的给带坏了不需要对象的时候你对象,不需要考虑效率的时候你效率其实博客园那拨人你不需要去管,他们一方面这效率那效率的,另一方面有大力推mvc,linq,MEF,自相矛盾啊,mvc,linq,mef里面用反射用的还少???送你一句话,《孙子兵法》:“故兵闻拙速,未睹巧之久也。”
    译文:所以在用兵上,只听说有指挥拙笨但却要求战争速决的,从未见有指挥灵巧而要求战争旷日持久的
      

  15.   

    其实你拿微软的这个mvc来说1个核心是路由规则表------这个规则表就是表驱动
    另1个核心是view 和control分离,而这个访问匹配规则很明显是用的反射
    那么就微软这个mvc的表现效率到底有多“差”,也没多差把,博客园那拨还在大力鼓吹mvc就是好,效率就是高
      

  16.   

    放哥,这是换成反射方法后的代码        //public string GetPage()
            //{
            //    if (template.Length == 0)
            //        return template;
            //    switch (this.page)
            //    {
            //        case "Index":
            //            return new PageHandler(template, model).GetIndex();
            //        case "Contact":
            //            return new PageHandler(template, model).GetContact();
            //        case "Aboutus":
            //            return new PageHandler(template, model).GetAboutus();
            //        case "News":
            //            return new PageHandler(template, model).GetNews();
            //        case "Business":
            //            return new PageHandler(template, model).GetBusiness();
            //        case "Product":
            //            return new PageHandler(template, model).GetProduct();
            //        case "Join":
            //            return new PageHandler(template, model).GetJoin();
            //        case "Video":
            //            return new PageHandler(template, model).GetVideo();
            //        case "VDetail":
            //            return new PageHandler(template, model).GetVDetail();
            //        case "Certificate":
            //            return new PageHandler(template, model).GetCertificate();
            //        case "ShopFace":
            //            return new PageHandler(template, model).GetShopFace();
            //        default:
            //            return template;
            //    }
            //}
            public string GetPage()
            {
                if (template.Length == 0)
                    return template;
                Type type = typeof(PageHandler);
                object o = Activator.CreateInstance(type, new object[] { template, model });
                System.Reflection.MethodInfo mi = type.GetMethod("Get" + page);
                if (mi == null)
                    return string.Empty;
                return mi.Invoke(o, null).ToString();
            }这样确实比较方便,而且也容易扩展,我在本机测试,运算多次取平均值,差别不大。
    不知道放到服务器上后,一些带宽,服务器配置会不会影响这个差别,影响大吗!因为服务器上不止一个网站。您是建议用反射还是用上面那个注释掉的方法,还是有什么更好的方法呢?