Answer to Question #322605 in C# for Madhu

Question #322605

Implementing File I/O for Product data

This program should define properties for Product model class. The program should also provide impelementations of CRUD operations for the Product model.

To achieve persistence, the data should be stored and read from file

This exercise contains project for solution code for Product entity, Product Repository and DataContext classes

  1. Product model class This class should provide common properties for the Product model.
  2. More on properties is provided in boilerplate code
  3. ProductRepository class The primary responsibility of this class to manage CRUD operations for collection of Products through DataContext object
  4. The boilerplate contains outline of methods for different CRUD operatoins required to be managed for products
  5. Product Storage
  6. The repository class should declare field as a DataContext object which contains the methods to read and write data of product list to file
  7. Product DataContext
1
Expert's answer
2022-04-03T13:30:53-0400
namespace CRUD
{
    class ProductModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    class ProductReposity
    {
        public async Task FileRead()
        {
            using (StreamReader reader = new StreamReader(@"TextFile.txt"))
            {
                string text = await reader.ReadToEndAsync();
                Console.WriteLine(text);
            }
        }
        public void Create(ProductModel model)
        {
            using (ProductSorage database = new ProductSorage())
            {
                database.ProductModel.Add(model);
                database.SaveChanges();
            }
        }
        public void Delete(int ID)
        {
            using (ProductSorage database = new ProductSorage())
            {
                var data = database.ProductModel.Where(x => x.ID == ID).FirstOrDefault();
                if (data != null)
                {
                    database.ProductModel.Remove(data);
                    database.SaveChanges();
                }
            }
        }
        public void Update(ProductModel productmodel)
        {
            using (ProductSorage database = new ProductSorage())
            {
                    var updateProduct = database.ProductModel.Where(u => u.ID == productmodel.ID).FirstOrDefault();
                    updateProduct.Name = productmodel.Name;
                    database.SaveChanges();
            }
        }
        public List<ProductModel> Read()
        {
            using (ProductSorage database = new ProductSorage())
            {
                try
                {
                    return database.ProductModel.ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
    }


    class ProductSorage : DbContext
    {
        public ProductSorage() : base("DbConnection")
        {


        }


        public DbSet<ProductModel> ProductModel { get; set; }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            ProductReposity productReposity = new ProductReposity();
            productReposity.FileRead();
            Console.ReadKey();
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS