你好,
我已经在部分应用程序中使用了策略设计模式。但是我遇到了一个问题。
开始:
我有一个设置的BasePage类,如下所示:
如您所见,它使用合成来包含我的IPageBehavior接口,如下所示:
现在,从该接口我有几个实现它的类,但是为了简单起见,我仅向您展示我遇到的问题:
突出显示的部分是问题所在。从此类的构造函数中可以看到,我从调用类(在本例中为Page)中传递了Id,CompanyId和Name。
问题是我的 前言 环形。它指出:
问题在于 页数() 方法期望将Id和CompanyId作为参数。当我创建一个新的 Page() 我必须传递这些参数(或从数据库中检索它们),但是 前言 不创建类的实例。所以我的问题很简单。
传递参数的最佳方法是什么?我是否将它们传递给方法(请注意:有2种方法,两者都期望使用相同的参数,这就是为什么我首先将其包含在Constructor中的原因)还是还有另一种方法?
只是为了您可以看到我的 页() 课,我将在这里包括:
干杯
/ r3plica
我已经在部分应用程序中使用了策略设计模式。但是我遇到了一个问题。
开始:
我有一个设置的BasePage类,如下所示:
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
}
}
突出显示的部分是问题所在。从此类的构造函数中可以看到,我从调用类(在本例中为Page)中传递了Id,CompanyId和Name。
问题是我的 前言 环形。它指出:
C#:
foreach (Page oPage in oChildren)
{
Collection<BasePage> oPages = oPage.Pages();
}
问题在于 页数() 方法期望将Id和CompanyId作为参数。当我创建一个新的 Page() 我必须传递这些参数(或从数据库中检索它们),但是 前言 不创建类的实例。所以我的问题很简单。
传递参数的最佳方法是什么?我是否将它们传递给方法(请注意:有2种方法,两者都期望使用相同的参数,这就是为什么我首先将其包含在Constructor中的原因)还是还有另一种方法?
只是为了您可以看到我的 页() 课,我将在这里包括:
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