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()
21
21
var orderManager = new OrderManager ( serviceLocator ) ;
22
22
orderManager . ProcessOrder ( order ) ;
23
23
}
24
- }
24
+ }
Original file line number Diff line number Diff line change 3
3
public interface IServiceLocator
4
4
{
5
5
void AddService < T > ( T service ) ;
6
-
7
6
void AddService < T > ( string serviceName , T service ) ;
8
-
9
7
T GetService < T > ( ) ;
10
-
11
8
object GetService < T > ( string serviceName ) ;
12
- }
9
+ }
Original file line number Diff line number Diff line change 2
2
3
3
public class Order
4
4
{
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)
15
15
{
16
16
_logger . Log ( "Processing new order..." ) ;
17
17
18
- decimal totalPrice = order . UnitPrice * order . Quantity ;
18
+ var totalPrice = order . UnitPrice * order . Quantity ;
19
19
20
20
var paymentProcessor = _serviceLocator . GetService < PaymentProcessor > ( ) ;
21
- bool isPaymentSuccessful = paymentProcessor . ProcessPayment ( totalPrice ) ;
21
+ var isPaymentSuccessful = paymentProcessor . ProcessPayment ( totalPrice ) ;
22
22
23
23
NotifyCustomer ( isPaymentSuccessful ) ;
24
24
}
@@ -35,4 +35,4 @@ private void NotifyCustomer(bool isPaymentSuccessful)
35
35
notifier . NotifyCustomer ( "Payment failed" ) ;
36
36
_logger . Log ( "Payment failed" ) ;
37
37
}
38
- }
38
+ }
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ public class PaymentProcessor
4
4
{
5
5
public bool ProcessPayment ( decimal amount )
6
6
{
7
- Console . WriteLine ( $ "Payment processor: Processing payment...") ;
7
+ Console . WriteLine ( "Payment processor: Processing payment..." ) ;
8
8
return amount <= 100 ;
9
9
}
10
- }
10
+ }
Original file line number Diff line number Diff line change 4
4
/// The service locator plays the middleman and allow consumers
5
5
/// to find and connect with various services.
6
6
/// 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.
8
8
/// 2) Lazy initialization of services might be considered.
9
9
/// In the example above, the constructor creates new instances for all possible services.
10
10
/// Initialization might be deferred until some client actually requests a particular service.
@@ -20,8 +20,8 @@ public ServiceLocator()
20
20
// Services can be pre-populated, but users could add them dynamically too.
21
21
_services = new Dictionary < string , object > ( )
22
22
{
23
- { typeof ( PaymentProcessor ) . Name , new PaymentProcessor ( ) } ,
24
- { typeof ( NotificationManager ) . Name , new NotificationManager ( ) } ,
23
+ { nameof ( PaymentProcessor ) , new PaymentProcessor ( ) } ,
24
+ { nameof ( NotificationManager ) , new NotificationManager ( ) } ,
25
25
} ;
26
26
}
27
27
You can’t perform that action at this time.
0 commit comments