Strategy Design

r3plica

Member
Joined
Dec 22, 2011
Messages
11
Programming Experience
5-10
Hi,
I have used the Strategy Design Pattern for part of my application. But I have come across a problem.
Here goes:

I have a BasePage class set up which looks like this:

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


    }
}

AS you can see, this uses composition to include my IPageBehavior Interface, which looks like this:

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();
    }
}

Now, from the interface I have a couple of classes implementing it, but for the purposes of simplicity I will only show you the one I am having issues with:

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


    }
}

The highlighted part is where the issue is. As you can see from the constructor of this class, I pass in the Id, CompanyId and Name from the calling class (in this case Page).
The problem is my foreach loop. It states:

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

The problem with that, is that the Pages() method expects the Id and CompanyId as parameters. When I create a new Page() I have to pass in these parameters (or retrieve them from the database), but the foreach does not create instances of the class. So my question is simple.
What is the best way to pass the parameters? Do I pass them through the methods (note: there are 2 methods, both expect the same parameters which is why I included it in the Constructor in the first place) or is there another way?

Just so you can see my Page() class, I will include it here:

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


    }
}

cheers
/r3plica
 
Back
Top Bottom