using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace ECommerceMicroservices
public string Name { get; set; }
public decimal Price { get; set; }
public override string ToString()
return $"Name: {Name}, Price: {Price}";
public string Name { get; set; }
public string Email { get; set; }
public override string ToString()
return $"Name: {Name}, Email: {Email}";
public interface IProductService
void AddProduct(Product product);
IEnumerable<Product> GetAllProducts();
public interface IOrderService
void PlaceOrder(Customer customer, Product product);
void 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>();
public void AddProduct(Product product)
Console.WriteLine($"Product added: {product}");
public IEnumerable<Product> GetAllProducts()
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;
public OrderService(IProductService productService, ICustomerService customerService)
_productService = productService;
_customerService = customerService;
public void PlaceOrder(Customer customer, Product product)
if (_productService.GetAllProducts().Any(p => p.Name == product.Name && p.Price == product.Price))
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 void GetOrderDetails(int orderId)
if (_orders.ContainsKey(orderId))
Console.WriteLine($"Order Details: {_orders[orderId]}");
Console.WriteLine("Order not found.");
public class CustomerService : ICustomerService
private readonly List<Customer> _customers = new List<Customer>();
public void AddCustomer(Customer customer)
_customers.Add(customer);
Console.WriteLine($"Customer added: {customer}");
public Customer GetCustomer(string email)
var customer = _customers.Find(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;
_orderService = orderService;
_customerService = 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);
_orderService.GetOrderDetails(1);
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();