using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace ECommerceMicroservices
public string Name { get; set; }
public decimal Price { get; set; }
public override bool Equals(object obj)
if (obj is Product product)
return Name == product.Name && Price == product.Price;
public override int GetHashCode()
return HashCode.Combine(Name, Price);
public string Name { get; set; }
public string Email { get; set; }
public interface IProductService
void AddProduct(Product product);
IEnumerable<Product> GetAllProducts();
public interface IOrderService
void PlaceOrder(Customer customer, Product product);
string GetOrderDetails(int orderId);
public interface ICustomerService
void AddCustomer(Customer customer);
Customer GetCustomer(string email);
public class ProductService : IProductService
private readonly List<Product> _products = new List<Product>();
private readonly object _lock = new object();
public void AddProduct(Product product)
Console.WriteLine($"Product added: {product.Name}, Price: {product.Price}");
public IEnumerable<Product> GetAllProducts()
return _products.ToList();
public class OrderService : IOrderService
private readonly IProductService _productService;
private readonly ICustomerService _customerService;
private readonly Dictionary<int, string> _orders = new Dictionary<int, string>();
private int _orderIdCounter = 1;
private readonly object _lock = new object();
public OrderService(IProductService productService, ICustomerService customerService)
_productService = productService ?? throw new ArgumentNullException(nameof(productService));
_customerService = customerService ?? throw new ArgumentNullException(nameof(customerService));
public void PlaceOrder(Customer customer, Product product)
if (_productService.GetAllProducts().Contains(product))
var orderDetails = $"Order ID: {_orderIdCounter}, Customer: {customer.Name}, Product: {product.Name}, Price: {product.Price}";
_orders.Add(_orderIdCounter++, orderDetails);
Console.WriteLine($"Order placed: {orderDetails}");
Console.WriteLine("Product not available in inventory.");
public string GetOrderDetails(int orderId)
if (_orders.TryGetValue(orderId, out var orderDetails))
Console.WriteLine("Order not found.");
public class CustomerService : ICustomerService
private readonly List<Customer> _customers = new List<Customer>();
private readonly object _lock = new object();
public void AddCustomer(Customer customer)
_customers.Add(customer);
Console.WriteLine($"Customer added: {customer.Name}, Email: {customer.Email}");
public Customer GetCustomer(string email)
return _customers.FirstOrDefault(c => c.Email == email);
public class ECommerceMicroservice
private readonly IProductService _productService;
private readonly IOrderService _orderService;
private readonly ICustomerService _customerService;
public ECommerceMicroservice(IProductService productService, IOrderService orderService, ICustomerService customerService)
_productService = productService ?? throw new ArgumentNullException(nameof(productService));
_orderService = orderService ?? throw new ArgumentNullException(nameof(orderService));
_customerService = customerService ?? throw new ArgumentNullException(nameof(customerService));
_productService.AddProduct(new Product { Name = "Laptop", Price = 1000 });
_productService.AddProduct(new Product { Name = "Smartphone", Price = 600 });
_productService.AddProduct(new Product { Name = "Headphones", Price = 150 });
var customer = new Customer { Name = "John Doe", Email = "john.doe@example.com" };
_customerService.AddCustomer(customer);
var product = new Product { Name = "Laptop", Price = 1000 };
_orderService.PlaceOrder(customer, product);
var orderDetails = _orderService.GetOrderDetails(1);
Console.WriteLine($"Order Details: {orderDetails}");
Console.WriteLine($"An error occurred: {ex.Message}");
static void Main(string[] args)
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<IProductService, ProductService>();
serviceCollection.AddScoped<IOrderService, OrderService>();
serviceCollection.AddScoped<ICustomerService, CustomerService>();
serviceCollection.AddScoped<ECommerceMicroservice>();
var serviceProvider = serviceCollection.BuildServiceProvider();
var ecommerceMicroservice = serviceProvider.GetRequiredService<ECommerceMicroservice>();
ecommerceMicroservice.Run();