Answer to Question #322689 in C# for madhu

Question #322689

Designing module for managing collection of product type data

This solution code should contain definition for product model along with it’s properties. The program should also provide impelementations of CRUD operations for the Product model

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

  1. Product model class
  • This class should provide common properties for the Product model.
  • More on properties is provided in boilerplate code
  1. ProductRepository class
  • The primary responsibility of this class to manage CRUD operations for collection of Products
  • The boilerplate contains outline of methods for different CRUD operatoins required to be managed for products
  1. Product Storage
  • The repository class should declare field as a collection object for allowing only the Product items to be added to the collection
  • Choose the collection type basis the fact that product collection should not allow any other type of items.
1
Expert's answer
2022-04-04T16:12:42-0400
class ProductModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    class ProductReposity
    {
        public async Task FileRead()
        {
            using (StreamReader reader = new StreamReader(@"YourFile.txt"))
            {
                string text = await reader.ReadToEndAsync();
                Console.WriteLine(text);
            }
        }
        public void Create(ProductModel productModel)
        {
            using (ProductStorage database = new ProductStorage())
            {
                database.ProductModel.Add(productModel);
                database.SaveChanges();
            }
        }
        public void Delete(int ID)
        {
            using (ProductStorage database = new ProductStorage())
            {
                var data = database.ProductModel.Where(p => p.ID == ID).FirstOrDefault();
                if (data != null)
                {
                    database.ProductModel.Remove(data);
                    database.SaveChanges();
                }
            }
        }
        public void Update(ProductModel productmodel)
        {
            using (ProductStorage database = new ProductStorage())
            {
                    var updateProduct = database.ProductModel.Where(p => p.ID == productmodel.ID).FirstOrDefault();
                    updateProduct.Name = productmodel.Name;
                    database.SaveChanges();
            }
        }
        public List<ProductModel> Read()
        {
            using (ProductStorage database = new ProductStorage())
            {
                try
                {
                    return database.ProductModel.ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
    }


    class ProductStorage : DbContext
    {
        public ProductStorage() : base("YourDataBaseConnection")
        {


        }


        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