views:

72

answers:

2

In my model I have these entities:

public interface IOrder
{
   string Name {get;set;}
   List<IProduct> OrderedProducts {get;set;}
}

public interface IProduct {}

In partial class generated by linq-to-sql I map these properties on my entity properties:

public partial class Order : IOrder
{
   List<IProduct> OrderedProducts
   {
      get { return this.L2SQLProducts.Cast<IProduct>.ToList(); }
      set { this.L2SQLProducts = ??? }
   }
}

How should setter look like?

EDIT: Purpose of this is to have interface that I could MOCK for unit testing or change linq-to-sql to another DAL.

+1  A: 

Hey,

An EntitySet implements IList so rather than dealing with a derived List instance, why not use the interface IList and simply assign it directly, as in:

public interface IOrder
{
   string Name {get;set;}
   IList<IProduct> OrderedProducts {get;set;}
}

Alternatively, you could instantiate an entity set, and copy over the objects in the list to that entity set. There may be alternative consequences to this.

   List<IProduct> OrderedProducts
   {
      get { return this.L2SQLProducts.Cast<IProduct>.ToList(); }
      set { 
           this.L2SQLProducts = new EntitySet<IProduct>();
           foreach (var entry in value)
              this.L2SQLProducts.Add(entry);
      }
   }

HTH.

Brian
Could you please provide code sample?
jlp
Updated the post.
Brian
A: 

汗了。。。 这样不怕性能问题吗? 如果我这样编码呢?

var a = xx.OrderedProducts[0];
var b = xx.OrderedProducts[1];

这样不是要该问了两次数据库吗?

translate to english: what? this can be slowly when run.

if I code like this:

var a = xx.OrderedProducts[0];
var b = xx.OrderedProducts[1];

Is that you will access to the database twice?

Tim Li