Question

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;
Était-ce utile?

La solution 2

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>();
}

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top