File tree Expand file tree Collapse file tree 6 files changed +14
-19
lines changed
src/AdditionalPatterns/ServiceLocator/OrderProcessing Expand file tree Collapse file tree 6 files changed +14
-19
lines changed Original file line number Diff line number Diff line change @@ -21,4 +21,4 @@ public override void Execute()
2121 var orderManager = new OrderManager ( serviceLocator ) ;
2222 orderManager . ProcessOrder ( order ) ;
2323 }
24- }
24+ }
Original file line number Diff line number Diff line change 33public 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+ }
Original file line number Diff line number Diff line change 22
33public 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+ }
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments