How to perform add/update of a model object that contains EntitySet

Posted by David Liddle on Stack Overflow See other posts from Stack Overflow or by David Liddle
Published on 2010-05-01T14:31:04Z Indexed on 2010/05/01 14:37 UTC
Read the original article Hit count: 266

I have a similar concept to the SO questions/tags scenario however am trying to decide the best way of implementation.

Tables Questions, QuestionTags and Tags

Questions        QuestionTags       Tags
---------        ------------       ----
QID              QID                TID
QName            TID                TName

When adding/updating a question I have 2 textboxes. The important part is a single textbox that allows users to enter in multiple Tags separated by spaces.

I am using Linq2Sql so the Questions model has an EntitySet of QuestionTags with then link to Tags.

My question is regarding the adding/updating of Questions (part 1), and also how to best show QuestionTags for a Question (part 2).


Part 1

Before performing an add/update, my service layer needs to deal with 3 scenarios before passing to their respective repositories.

  1. Insert Tags that do not already exist
  2. Insert/Update Question
  3. Insert QuestionTags - when updating need to remove existing QuestionTags

Here is my code below however started to get into a bit of a muddle. I've created extension methods on my repositories to get Tags WithNames etc.

public void Add(Question q, string tags)
{
    var tagList = tags.Split(new string[] { " " },
        StringSplitOptions.RemoveEmptyEntries).ToList();

    using (DB.TransactionScope ts = new DB.TransactionScope())
    {
        var existingTags = TagsRepository.Get()
            .WithName(tagList)
            .ToList();

        var newTags = (from t in tagList
            select new Tag
            {
                TName = t
            }).Except(existingTags, new TagsComparer()).ToList();

        TagsRepository.Add(newTags);
        //need to insert QuestionTags
        QuestionsRepository.Add(q);

        ts.Complete();
    }
}

Part 2

My second question is, when displaying a list of Questions how is it best to show their QuestionTags?

For example, I have an Index view that shows a list of Questions in a table. One of the columns shows an image and when the user hovers over it shows the list of Tags.

My current implementation is to create a custom ViewModel and show a List of QuestionIndexViewModel in the View.

QuestionIndexViewModel
{
    Question Question { get; set; }
    string Tags { get; set; }
}

However, this seems a bit clumsy and quite a few DB calls.

public ViewResult Index()
{
    var model= new List<QuestionIndexViewModel>();

    //make a call to get a list of questions
    //foreach question make a call to get their QuestionTags, 
    //to be able to get their Tag names and then join them
    //to form a single string.
    return View(model);
}

Also, just for test purposes using SQL Profiler, I decided to iterate through the QuestionTags entity set of a Question in my ViewModel however nothing was picked up in Profiler? What would be the reason for this?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about linq-to-sql