Register | Login

Stacking Code

public interface IBlog { string Dump(Stream consciousness); }

Comments

Tuesday, 14 December, 2010 @ 9:03 PM < Adam Boddington
Tags: ASP.NET MVC, Building Neno

This is post #21 in the Building Neno series. Please click here for a description of the Building Neno project and instructions on how to access the source code for this post.

I debated whether to build comments myself or just sign up to Disqus instead. Their feature list is impressive, they're "free", and they require just a little JavaScript on the page to get it all working.

Tempting, but is it really free? What is Disqus doing with all that data? Their license says they can do whatever they like with non-personal information, so does that mean they take the names out and do marketing analysis on the rest? Where people comment, what they respond to, what links they include, what they like, what they don't like, when they comment, etc. The mountain of data they're collecting is a potential goldmine if it isn't already. It's the same issue I have with all the social networking sites. Maybe I'm just paranoid about privacy, maybe the aggregation makes it all harmless. Perhaps. But wouldn't it be nice if social networking was distributed instead?

I digress. The short version is I decided to build comments myself.

View Model

So, time to put the view models through their paces and get some coding done. I've added a text property to my single post view model for the text of the new comment.

public class PostCommentNew : PostView
{
    public PostCommentNew(Post post, Comment comment)
        : base(post)
    {
        Comment = comment;
    }

    public Comment Comment { get; private set; }

    [DisplayName("comment")]
    [Required]
    [StringLength(500)]
    public string Text
    {
        get { return Comment.Text; }
        set { Comment.Text = value; }
    }
}

This makes UI validation mostly automatic -- although I'm not thrilled about the validation rule duplication.

Invalid

Controller

My actions could still use a little work -- they seem a bit bloated to me. One solution may be to accept the view model back in as a parameter on my postback actions, but I will have to look into that later.

It's funny how we get stuck in our own little way of doing things. We can be aware of a better way, but our current way works, so we don't investigate and make the effort to switch. Passing parameters into action methods is one of those for me. Another is my limited use of IOC. I should really be getting my IOC container to do more of the heavy lifting for me, like automatically injecting services into my controllers.

[HttpPost]
[UserIsRegistered]
public ActionResult PostByPublishDateAndSlug(int year, int month, int day, string slug, FormCollection collection)
{
    var publishDate = new DateTime(year, month, day);
    Post post = Container.Get<IPostService>().GetPostByPublishDateAndSlug(publishDate, slug);

    if (post == null)
    {
        Messages.Add(MessageType.Warning, "No posts for " + publishDate.ToString("dddd, d MMMM, yyyy") + ", with a slug of " + slug + ".");

        return RedirectToAction("postsbypublishdate", "blog", new PublishDate(publishDate));
    }

    var comment = new Comment(post, DomainModel.User.Current);
    var model = new PostCommentNew(post, comment);

    try
    {
        TryUpdateModel(model, new[] { "Text" });

        if (!ModelState.IsValid)
            return View(model);

        Container.Get<IPostService>().CreateComment(comment);
        Messages.Add("Comment created successfully.");

        return RedirectToAction("postbypublishdateandslug", "blog", new PublishDateAndSlug(post));
    }
    catch (Exception exception)
    {
        Messages.Add(exception);

        return View(model);
    }
}

View

The rest is just HTML and CSS. My screens already have access to the comments collection, so I just have to iterate and format nicely.

List

Conclusion

My comments are very basic. I don't have threading, email notification, or an RSS feed. More importantly, I don't have any spam protection apart from OpenID and anonymity. I could implement a CAPTCHA, or I could go for something like Akismet. But if I don't like all my comments going to Disqus, how is sending all my comments through Akismet any different? I have to read up on spam protection a bit more.

Anyway, buiding simple comment functionality was much easier with a view model than it would have been if I had stuck to the domain model. Next up, tags.

There are 0 comments.


Comments

Leave a Comment

Please register or login to leave a comment.


Older
View Model Refactor

Newer
Tags

Older
View Model Refactor

Newer
Tags

browse with Pivot


About


Projects

Building Neno


RSS
Recent Posts

Codility Nitrogenium Challenge
OS X Lock
HACT '13
Codility Challenges
Priority Queue


Tags

Architecture (13)
ASP.NET (2)
ASP.NET MVC (13)
Brisbane Flood (1)
Building Neno (38)
C# (4)
Challenges (3)
Collections (1)
Communicator (1)
Concurrency Control (2)
Configuration (1)
CSS (5)
DataAnnotations (2)
Database (1)
DotNetOpenAuth (2)
Entity Framework (1)
FluentNHibernate (2)
Inversion of Control (5)
JavaScript (1)
jQuery (4)
Kata (2)
Linq (7)
Markdown (4)
Mercurial (5)
NHibernate (20)
Ninject (2)
OpenID (3)
OS X (1)
Pivot (6)
PowerShell (8)
Prettify (2)
RSS (1)
Spring (3)
SQL Server (5)
T-SQL (2)
Validation (2)
Vim (1)
Visual Studio (2)
Windows Forms (3)
Windows Service (1)


Archives


Powered by Neno, ASP.NET MVC, NHibernate, and small furry mammals. Copyright 2010 - 2011 Adam Boddington.
Version 1.0 Alpha (d9e7e4b68c07), Build Date Sunday, 30 January, 2011 @ 11:37 AM