Skip to content

Commit e84bae9

Browse files
committed
Update the service locator example
1 parent 98b79fc commit e84bae9

File tree

6 files changed

+14
-19
lines changed

6 files changed

+14
-19
lines changed

src/AdditionalPatterns/ServiceLocator/OrderProcessing/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ public override void Execute()
2121
var orderManager = new OrderManager(serviceLocator);
2222
orderManager.ProcessOrder(order);
2323
}
24-
}
24+
}

src/AdditionalPatterns/ServiceLocator/OrderProcessing/IServiceLocator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
public interface IServiceLocator
44
{
55
void AddService<T>(T service);
6-
76
void AddService<T>(string serviceName, T service);
8-
97
T GetService<T>();
10-
118
object GetService<T>(string serviceName);
12-
}
9+
}

src/AdditionalPatterns/ServiceLocator/OrderProcessing/Order.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
public class Order
44
{
5-
public Guid ProductId { get; set; }
6-
7-
public decimal UnitPrice { get; set; }
8-
9-
public int Quantity { get; set; }
10-
}
5+
public Guid ProductId { get; init; }
6+
public decimal UnitPrice { get; init; }
7+
public int Quantity { get; init; }
8+
}

src/AdditionalPatterns/ServiceLocator/OrderProcessing/OrderManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public void ProcessOrder(Order order)
1515
{
1616
_logger.Log("Processing new order...");
1717

18-
decimal totalPrice = order.UnitPrice * order.Quantity;
18+
var totalPrice = order.UnitPrice * order.Quantity;
1919

2020
var paymentProcessor = _serviceLocator.GetService<PaymentProcessor>();
21-
bool isPaymentSuccessful = paymentProcessor.ProcessPayment(totalPrice);
21+
var isPaymentSuccessful = paymentProcessor.ProcessPayment(totalPrice);
2222

2323
NotifyCustomer(isPaymentSuccessful);
2424
}
@@ -35,4 +35,4 @@ private void NotifyCustomer(bool isPaymentSuccessful)
3535
notifier.NotifyCustomer("Payment failed");
3636
_logger.Log("Payment failed");
3737
}
38-
}
38+
}

src/AdditionalPatterns/ServiceLocator/OrderProcessing/PaymentProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class PaymentProcessor
44
{
55
public bool ProcessPayment(decimal amount)
66
{
7-
Console.WriteLine($"Payment processor: Processing payment...");
7+
Console.WriteLine("Payment processor: Processing payment...");
88
return amount <= 100;
99
}
10-
}
10+
}

src/AdditionalPatterns/ServiceLocator/OrderProcessing/ServiceLocator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/// The service locator plays the middleman and allow consumers
55
/// to find and connect with various services.
66
/// Possible improvements:
7-
/// 1) The service locator itself might be a singleton. There usually is no need to have two instances of a service locator.
7+
/// 1) The service locator itself might be a singleton. Usually, there is no need to have two instances of a service locator.
88
/// 2) Lazy initialization of services might be considered.
99
/// In the example above, the constructor creates new instances for all possible services.
1010
/// Initialization might be deferred until some client actually requests a particular service.
@@ -20,8 +20,8 @@ public ServiceLocator()
2020
// Services can be pre-populated, but users could add them dynamically too.
2121
_services = new Dictionary<string, object>()
2222
{
23-
{ typeof(PaymentProcessor).Name, new PaymentProcessor() },
24-
{ typeof(NotificationManager).Name, new NotificationManager() },
23+
{ nameof(PaymentProcessor), new PaymentProcessor() },
24+
{ nameof(NotificationManager), new NotificationManager() },
2525
};
2626
}
2727

0 commit comments

Comments
 (0)