0

I have the following method in DAL which accepts the model OrderList and returns a List<OrderList>

public List<OrderList> GetOrderList(OrderList orderList)
{
...
return new List<OrderList>();
}

When I analyze using FXCop, it's saying DoNotExposeGenericLists Error

FXCop is saying this as resolution

"Change 'List<OrderList>' in 'OrderRelatedDataAccess.GetOrderList(OrderList)' to use 
Collection<T>, ReadOnlyCollection<T> or KeyedCollection<K,V>"

How can I correct this?

My OrderList model is shown below

public int serialNumber { get; set; }
public int orderID { get; set; }
public int orderListID { get; set; }
public int productID { get; set; }
public int quantity { get; set; }
public long price { get; set; }
public string productName { get; set; }

Thanks.

Edit: Code inside the method

List<OrderList> listOfOrderList = new List<OrderList>();
  using (SqlConnection connection = new SqlConnection(connectionString))
  using (SqlCommand command = new SqlCommand("sGetOrderListByOrderID", connection))
  {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@orderID", orderList.orderID);
    connection.Open();
    reader = command.ExecuteReader();
    int sno = 0;
    while (reader.Read())
    {
      long price = long.Parse(reader[4].ToString());
      listOfOrderList.Add(new OrderList()
      {
        serialNumber = ++sno,
        orderID = (int)reader[0],
        productID = (int)reader[2],
        quantity = (int)reader[3],
        price = price,
        productName = reader[5].ToString(),
       });
     }
   connection.Close();
 }
return listOfOrderList;
RandomUser
  • 1,843
  • 8
  • 33
  • 65

2 Answers2

1

In your case its best to change the method signature to use an IList instead of List

public IList<OrderList> GetOrderList(OrderList orderList)
{
 //get your data and build your list here
    return new List<OrderList>();
}

See the differences between ICollection and IList in this answer

Community
  • 1
  • 1
user3373870
  • 1,406
  • 2
  • 13
  • 17
0

The answer is right there in the FxCop message. Just change the return type to be ICollection<OrderList>, like so:

public ICollection<OrderList> GetOrderList(OrderList orderList)
{
    ...
    return new List<OrderList>();
}
metacubed
  • 7,031
  • 6
  • 36
  • 65
  • I updated my question with my method definition. I am returning a collection! – RandomUser May 10 '14 at 06:08
  • @RandomUser I don't see a `Collection` anywhere in your edit - can you clarify? – metacubed May 10 '14 at 06:12
  • The tool FXCop is suggesting me to use `Collection` instead of directly using `List` as a **Design Rule**. I am wonder how can I change my code to satisfy this! – RandomUser May 10 '14 at 06:14
  • @RandomUser No that is not so.. it just recommends you to not return a `List` from a public method, instead using `Collection` or `ReadOnlyCollection`, etc. You can read more here: [link](http://msdn.microsoft.com/en-us/library/ms182142.aspx). – metacubed May 10 '14 at 06:19
  • 1
    @metacubed, your code won't event compile since you cannot perform an implicitly cast a List to a Collection – user3373870 May 10 '14 at 06:20
  • @metacubed How can I achieve what you said? that was my first question or least that was what I wanted to ask.. – RandomUser May 10 '14 at 06:20
  • Oops.. change that to be an `ICollection` and you should be fine. See my answer now. – metacubed May 10 '14 at 06:22