战略设计

R3Plica

成员
加入
2011年12月22日
消息
11
编程经验
5-10
你好,
我使用了策略设计模式,以便是我的应用程序。但我遇到了一个问题。
开始:

我有一个如下所示的基本类设置:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using CMS.Data;


namespace CMS
{
    public class BasePage
    {


        #region Encapsulation


        public IPageBehavior OurBehavior;


        #endregion


        #region Properties


        public int Id { get; set; }


        [Required]
        public int ParentId { get; set; }


        public string UserId { get; set; }
        public string CompanyId { get; set; }
        public string Author { get; set; }


        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }


        public string ModifiedById { get; set; }
        public string ModifiedBy { get; set; }


        [Required]
        public string Name { get; set; }
        public string Description { get; set; }
        public string Path { get; set; }
        public string ActualPath { get; set; }
        public string Link { get; set; }
        public string Controller { get; set; }
        public string Area { get; set; }
        public string ViewTitle { get; set; }
        public byte[] ViewData { get; set; }


        [Required]
        public int StateId { get; set; }
        public string State { get; set; }


        #endregion


        #region Public methods


        public void Save()
        {
            if (this.Id > 0)
            {
                PageData.edit(this);
            }
            else
            {
                this.Id = PageData.create(this);
            }
        }


        public void Remove()
        {
            PageData.delete(this.Id);
        }


        #endregion


    }
}

正如您所看到的,这将使用组合来包含我的ipagebehavior接口,它看起来像这样:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;


namespace CMS
{
    public interface IPageBehavior
    {
        int Id { get; set; }
        string CompanyId { get; set; }


        Collection<BasePage> Pages();
        string BuildTree();
    }
}

现在,从界面上,我有几个课程实现它,但是为了简单起见,我只会向你展示我有问题的人:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using CMS.Data;


namespace CMS
{
    class PageMethods : IPageBehavior
    {


        #region Properties


        public int Id { get; set; }
        public string CompanyId { get; set; }
        public string Name { get; set; }


        #endregion


        #region Constructors


        public PageMethods(int Id, string CompanyId, string Name)
        {
            this.Id = Id;
            this.CompanyId = CompanyId;
            this.Name = Name;
        }


        #endregion


        #region Public methods


        public Collection<BasePage> Pages()
        {
            return PageData.getPages(this.Id, this.CompanyId);
        }


        public string BuildTree()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<ul class='tree sortable' id='" + this.Id + "'>");
            Collection<BasePage> oPages = this.Pages();
            if (oPages.Count > 0)
            {
                sb.Append(BuildChildren(oPages));
            }
            sb.Append("</li>");
            sb.Append("</ul>");
            return sb.ToString();
        }


        #endregion


        #region Private methods


        private static string BuildChildren(Collection<BasePage> oChildren)
        {
            StringBuilder sb = new StringBuilder();


            sb.Append("<ul>");
[COLOR=#ff0000]            foreach (Page oPage in oChildren)[/COLOR]
            {              
                sb.Append("<li><span class='tick'></span><div class='item' rel='" + oPage.Path + "' id='" + oPage.Id + "' title='" + oPage.Name + "'><span class='arr expanded'></span><span class='title'>" + oPage.Name + "</span></div>");
                Collection<BasePage> oPages = oPage.Pages();
                if (oPages.Count > 0)
                {
                    sb.Append(BuildChildren(oPages));
                }
                sb.Append("</li>");
            }
            sb.Append("</ul>");


            return sb.ToString();
        }


        #endregion


    }
}

突出显示的部分是问题所在。您可以从此类的构造函数中看到,我通过调用类(在本例页中的名称中传递ID,CompanyID和名称)。
问题是我的 Foreach. 环形。它指出:

C#:
            foreach (Page oPage in oChildren)
            {              
                Collection<BasePage> oPages = oPage.Pages();
            }

这是问题的,就是这样 页面() 方法期望ID和CompanID为参数。当我创造一个新的时候 Page() 我必须通过这些参数(或从数据库中检索它们),但是 Foreach. 没有创建类的实例。所以我的问题很简单。
通过参数的最佳方法是什么?我是否通过这些方法(注意:有2种方法,两者都期望相同的参数,这就是我首先在构造函数中包含它的原因)或者是另一种方式?

所以你可以看到我的 页() 课程,我将在此处包含:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.Collections.ObjectModel;
using CMS.Data;
using System.ComponentModel.DataAnnotations;


namespace CMS
{
    public class Page : BasePage
    {


        #region Constructors


        public Page()
        {
            OurBehavior = new PageMethods(this.Id, this.CompanyId, this.Name);
        }


        public Page(string CompanyId)
        {
            BasePage oPage = PageData.getRoot(CompanyId);


            this.Id = oPage.Id;
            this.ParentId = oPage.ParentId;
            this.UserId = oPage.UserId;
            this.CompanyId = oPage.CompanyId;
            this.Author = oPage.Author;
            this.DateCreated = oPage.DateCreated;
            this.DateModified = oPage.DateModified;
            this.ModifiedById = oPage.ModifiedById;
            this.ModifiedBy = oPage.ModifiedBy;
            this.Name = oPage.Name;
            this.Description = oPage.Description;
            this.Path = oPage.Path;
            this.ActualPath = oPage.ActualPath;
            this.Link = oPage.Link;
            this.Controller = oPage.Controller;
            this.Area = oPage.Area;
            this.ViewTitle = oPage.ViewTitle;
            this.ViewData = oPage.ViewData;
            this.StateId = oPage.StateId;


            OurBehavior = new PageMethods(this.Id, this.CompanyId, this.Name);
        }


        public Page(int Id, string CompanyId)
        {
            BasePage oPage = PageData.getPage(Id, CompanyId);
            
            this.Id = oPage.Id;
            this.ParentId = oPage.ParentId;
            this.UserId = oPage.UserId;
            this.CompanyId = oPage.CompanyId;
            this.Author = oPage.Author;
            this.DateCreated = oPage.DateCreated;
            this.DateModified = oPage.DateModified;
            this.ModifiedById = oPage.ModifiedById;
            this.ModifiedBy = oPage.ModifiedBy;
            this.Name = oPage.Name;
            this.Description = oPage.Description;
            this.Path = oPage.Path;
            this.ActualPath = oPage.ActualPath;
            this.Link = oPage.Link;
            this.Controller = oPage.Controller;
            this.Area = oPage.Area;
            this.ViewTitle = oPage.ViewTitle;
            this.ViewData = oPage.ViewData;
            this.StateId = oPage.StateId;


            OurBehavior = new PageMethods(this.Id, this.CompanyId, this.Name);
        }


        #endregion


        #region Public methods


        public Collection<BasePage> Pages()
        {
            return OurBehavior.Pages();
        }


        public string BuildTree()
        {
            return OurBehavior.BuildTree();
        }


        #endregion


    }
}

干杯
/ R3Plica
 
最佳 底部