1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Making Polls Generic Contents

Making Polls Generic Contents

6 posts, 0 answered
  1. Augusto Radtke
    Augusto Radtke avatar
    24 posts
    Registered:
    16 Apr 2010
    07 May 2010
    Link to this post
    Hello,

    I was try to implement a PollsProvider to make polls a generic content, so I can tag, categorize and extend with some meta content. I want to reuse most of the NolicsPollsProvider code, so I extended it and overrided CreatePoll() and GetPollById() initially, to make only the Poll a generic content, not the answers or questions, like the code above:

    public class QuayPollContentProvider: NolicsPollsProvider
     {
         private ContentManager manager;
     
         public QuayPollContentProvider()
         {
             this.manager = new ContentManager("Polls");
         }
     
         public override IPollItem GetPollById(Guid id)
         {
             var content = manager.GetContent(id);
     
             return new PollContent(content);
         }
     
         public override IPollItem CreatePoll()
         {
             var content = manager.CreateContent("text/html");
     
             return new PollContent(content);
         }

    I also made a wrapper class PollContent, to wrap an IContent and implement the IPollItem:

    public class PollContent: IPollItem
    {
        private IContent content;
     
        public PollContent(IContent content): base()
        {
            this.content = content;
     
            this.content.SetMetaData("Title", String.Empty);
             
        }
     
        #region IPollItem Members
     
        public new Guid ID
        {
            get { return this.content.ID; }
        }
     
        public new string Owner
        {
            get
            {
                return this.content.GetMetaData("Owner") as string;
            }
            set
            {
                this.content.SetMetaData("Owner", value);
            }
        }
     
        public new IList Questions
        {
            get
            {
                var questions = new GetAllQuestions();
     
                questions.ID = this.ID;
                questions.LoadAll();
     
                return questions;
            }
        }
     
        public new string Title
        {
            get
            {
                return this.content.GetMetaData("Title") as string;
            }
            set
            {
                this.content.SetMetaData("Title", value);
            }
        }
     
        public new int VoterNumber
        {
            get
            {
                return Convert.ToInt32(this.content.GetMetaData("VoterNumber"));
            }
            set
            {
                this.content.SetMetaData("VoterNumber", value);
            }
        }
     
        #endregion
    }

    So far so good, the problem is that when the CreateQuestion() of NolicsPollsProvider is running it make a upcast of IPollItem to Poll, assuming that is a Poll, which is not. Even before that it casts as IOdbClass, which I managed to fix someway, but I got stuck with the second problem.

    I would like to know if it´s fine to reuse something like NolicsPollsProvider  or should I implement from scratch the whole polls provider and forget about reusing some part of it. There is any solution to these kind of reuse problems?
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    09 May 2010
    Link to this post
    Hello Augusto Radtke,

    I suggest that you should create a custom module based on Generic_Content and  use a custom data provider. You could use LINQ-SQL or OpenAccess ORM

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Augusto Radtke
    Augusto Radtke avatar
    24 posts
    Registered:
    16 Apr 2010
    10 May 2010
    Link to this post
    I´m using Telerik ORM already. I was just wondering that I could reuse parts of the data provider to spare some work. Do you think that the best advice is to not reuse providers but implement them from scratch to avoid misusing like this?

    Maybe could be a good idea to make these default providers classes sealed for 4.0 release?  
  4. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    11 May 2010
    Link to this post
    Hello Augusto Radtke,

    I think the best idea is to have the provider written from scratch.
    By the way, could you elaborate on the sealed classed? This is exactly the opposite approach of making the classes extensible?

    Regards,
    Georgi
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. Augusto Radtke
    Augusto Radtke avatar
    24 posts
    Registered:
    16 Apr 2010
    11 May 2010
    Link to this post
    Yes, the sealed classes cannot be inherited. Check about here. It would sign very clear that the best approach is to derive from the provider bases and not inherit it. Although it could be useful sometimes to inherit the provider for small changes, like for example sending a notification e-mail every time a comment hit a blog post page, could be easily done inheriting:

    public class TestBlogProvider: Telerik.Blogs.Data.DefaultBlogProvider
    {
        public override void SaveComment(Telerik.Cms.Engine.IComment comment)
        {
            base.SaveComment(comment);
     
            // Do custom e-mailing here.
        }
    }

    I think that for these small "addons" for provider functionality I would make a different approach: events. Could be interesting to have events on the base provider class, non overridable that the extended class can subscribe and execute these kind of functionality:

    public class TestBlogProvider: Telerik.Blogs.IBlogProvider
    {
        public void SaveComment(Telerik.Cms.Engine.IComment comment)
        {
            base.OnPreSaveComment += (sender, e) => {
                // Do custom e-mailing here.
            };
     
            // This base method would trigger the event.
            base.SaveComment(comment);           
        }
    }

    This would imply in making IBlogProvider an abstract class instead of an interface, like CmsProvider, ContentProviderBase and others. This would open space to implement modules that simple "plug-in" into certain providers and extend their functionality a little bit.

    What you think about that?
  6. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    11 May 2010
    Link to this post
    Hello Augusto Radtke,

    Thank you for clarifying. 

    We are trying to make Sitefinity open for developers, and this is the real reason why the provider classes are not sealed. There are some cases in which this openness really helped - for example we can send fixes to people who experience problems with the providers.

    As for the events, we expose such, but on Manager classes level (one level up). I agree that such events are really interesting and helpful. Since every piece of content is created through a manager class (which is the relation with the provider), it's logical that we implement the events there, and keep the provider clean. Please check this forum thread - http://www.sitefinity.com/support/forums/sitefinity-3-x/developing-with-sitefinity/adding-event-handlers.aspx - it will get you started with the events :)

    I hope these architecture decisions sound reasonable to you. 

    Kind regards,
    Georgi
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
6 posts, 0 answered