locked
Using interfaces to define behavior and specific properties RRS feed

  • Question

  • User1100692814 posted

    Hi all

    At work we have been discussing the use of interfaces, various patterns etc...

    The below code is for a ficticious CMS but at present we do the following within some of our applications:

    // Model
    
    public class ContentItem
    {
        public int ContentItemID { get; set; }
        public string Title { get; set; }
        public string Owner { get; set; }
        public bool IsPublishable { get; set; }
        public DateTime? PublishedOn { get; set; }
        public DateTime CreatedOn{ get; set; }
        public DateTime ModifiedOn { get; set; }
    }
    
    public interface IPublishable
    {
        bool CanPublish { get; }
    }
    
    public interface IHasBody
    {
        string Body { get; set; }
    }
    
    public class BlogPost : ContentItem, IHasBody, IPublishable
    {
        public int BlogPostID { get; set; }
        public string Excerpt { get; set; }
        public bool IsMemberOnly { get; set; }
        public string Body { get; set; } // IHasBody
        public bool CanPublish { get { return true; } } // IPublishable
    }
    
    public class MenuItem : ContentItem, IPublishable
    {
        public int MenuItemID { get; set; }
        public bool CanPublish { get { return false; } } // IPublishable
    }
    
    
    
    // UI
    var post = BlogService.GetPost(1);
    txtBody.Text = post.Body;
    pnlPublish.Visible = post.CanPublish;
    
    
    
    
    


    Would you consider the above good practice? If not, how would you improve it?

    Just looking to improve coding standards etc... So be honest.

    Thanks in advance.

    /D

    Monday, November 26, 2012 11:32 AM

Answers

  • User614805505 posted

    Hi,

    I see no issue if do like this. But I have a comment on the Interface class naming. It shouldn't name it like something that will return true or false. IPublishable, IHasBody....

    I suggest to place the "CanPublish" property into "ContentItem" class so the IPublishable interface can be remove. Then rename IHasBody to IContentBody.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2012 6:14 AM

All replies

  • User1100692814 posted

    No one?

    Thursday, November 29, 2012 6:06 AM
  • User614805505 posted

    Hi,

    I see no issue if do like this. But I have a comment on the Interface class naming. It shouldn't name it like something that will return true or false. IPublishable, IHasBody....

    I suggest to place the "CanPublish" property into "ContentItem" class so the IPublishable interface can be remove. Then rename IHasBody to IContentBody.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 29, 2012 6:14 AM
  • User1100692814 posted

    Many thanks.

    Good pointers.

    /D

    Tuesday, December 11, 2012 6:57 AM