I wrote a program that does work with files like delete and update, store, and search And all customers But I have a problem with the method is LoadAll Once the data are read from the file and then Deserialize the object becomes But when I want to save the list of objects in the list are repeated. How can I prevent the duplication in this code?
var customerStr = File.ReadAllLines (address);
The code is written in CustomerDataAccess class DataAccess Layer.
The main problem with the method LoadAll Code:
public ICollection<Customer> LoadAll()
{
var alldata = File.ReadAllLines(address);
List<Customer> lst = new List<Customer>();
foreach (var s in alldata)
{
var objCustomer = customerSerializer.Deserialize(s);
lst.Add(objCustomer);
}
return lst;
}
CustomerSerialize class defined in a Field you simply use the Deserialize method you! Each series is a series Deserialize the value added items previously got into the game! (Because there is reference Type).
If you change your code to:
public Customer Deserialize(string str)
{
**Customer customer = new Customer();**
var strCustomer = str.Split(',');
customer.Id = int.Parse(strCustomer[0]);
customer.FirstName = strCustomer[1];
customer.LastName = strCustomer[2];
customer.Age = int.Parse(strCustomer[3]);
customer.Country = strCustomer[4];
return customer;
}