tags:

views:

45

answers:

2

I am going to store a comma delimited list of courseIDs available with a product, there could be any number of them and many times only ONE. I am using MVC and I want to be able to extract out the list into a collection of some sort prior to it being in the model. Basically I don't want to parse a list into an array inside of the view or controller... How/where is the best way to do this, here is my product object that is used by my repository. What would I use for the type of the courseIDs collection and where do I convert it from a comma delimited list in the database into an array or something?

namespace DomainModel.Entities
{
    public class Product
    {
        public int ProductID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set; }
        public ??? CourseIDs { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Entities;

namespace DomainModel.Abstract
{
    public interface IProductsRepository
    {
        IQueryable<Product> Products { get; }
    }
}
A: 

How about System.Collections.Generic.List<string> or IList<string> ? You could do the splitting in your DAO (if you're using that pattern) or you could store the string in your Product class an lazy load the splitted list:

public class Product {
  private string _courseIdsString;
  private IList<string> _courseIds;
  public IList<string> CourseIds {
    get {
      if(_courseIds == null)
        _courseIds = new List<string>(_courseIds.Split(',' StringSplitOptions.RemoveEmptyEntries));
      return _courseIds;
   }
   // ...
}
Patrick M.
I am using LINQ to SQL
shogun
A: 

If it provides you will all the functionality you need, IEnumerable<string> may be the most appropriate method to store the set of course ids. It is the least restrictive "collection" interface. If you need other functionality provided by IList (Add, Remove, Count, etc), then IList<string> is an option as well.

Leave any parsing of the comma separate string in your data access layer (same spot you read from the database). There's no sense in leaking those kinds of details up to your model.

Nader Shirazie