Question

Good Day guys!

I've just started using MVC3, I've 2 models in my application i.e. "Page" and "PageHistory" both has same properties. except that "PageHistory" has one extra property called "PageId" which references the "Page" model.

My question is am doing it in correct way? or should I use inheritance for this. If inheritance is a option, How can I handle this, any examples will help me a lot.

My Model looks like as follows:

public class Page
    {
        private readonly IndianTime _g = new IndianTime();

        public Page()
        {
            CreatedOn = _g.DateTime;
            Properties = "Published";
            Tags = "Page";
            RelativeUrl = string.Empty;
        }

        public string Path
        {
            get { return (ParentPage != null) ? ParentPage.Heading + " >> " + Heading : Heading; }
        }

        [Key]
        public int Id { get; set; }

        [StringLength(200), Required, DataType(DataType.Text)]
        public string Title { get; set; }

        [StringLength(200), Required, DataType(DataType.Text)]
        public string Heading { get; set; }

        [MaxLength, Required, DataType(DataType.Html)]
        public string Content { get; set; }

        [Display(Name = "Reference Code"), ScaffoldColumn(false)]
        public string ReferenceCode { get; set; }

        [Required]
        [Remote("CheckDuplicate", "Page", ErrorMessage = "Url has already taken", AdditionalFields = "initialUrl")]
        public string Url { get; set; }


        [Display(Name = "Created On"), ScaffoldColumn(false)]
        public DateTime CreatedOn { get; set; }

        //Parent Page Object (Self Reference: ParentId = > Id)
        [Display(Name = "Parent Page")]
        public int? ParentId { get; set; }

        [DisplayFormat(NullDisplayText = "Root")]
        public virtual Page ParentPage { get; set; }

        public virtual ICollection<Page> ChildPages { get; set; }
    }
Was it helpful?

Solution

I don't think inheritance will be the right way, the current structure looks ok to me.

See if you choose to make the Page as parent, and Pagehistory as being inherited from Page which actually it is not its just the pagehistory quite simply put.

Your idea of inheritance should always come from real world implementations, for ex. there could be diff. kinds of pages all inheriting from a Super Page type, while Page history is just a property to the page rather a complex property with properties inside it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top