- Service Locator use K‡iI Avgiv Inversion of Control (IoC) implement Ki‡Z cvwi|
Ø
Dc‡iv³ diagram
Abyhvqx OrderService Class OrderDatabase Class m¤ú‡K© AÁvZ _v‡K wKš‘ ServiceLocator Class m¤ú‡K©
Rv‡b | Avevi ServiceLocator Class
OrderDatabase m¤ú‡K© ÁvZ _v‡K|
Ø
hLb AcceptOrder
invoke Kiv nq ZLb Bnv IOrderSaver Gi instance Gi
Rb¨ ServiceLocator ‡K ask Ki‡e|
Ø
ServiceLocator ZLb
IOrderSaver Gi D³ instance Gi Rb¨ configured class Gi instance supply Ki‡e|
Ø
ServiceLocator use K‡i OrderService Gi implementation wb¤œiƒc n‡e-
public class OrderService
{
public void AcceptOrder(Order order)
{
//Domain logic such as validation
OrderSaverFactory.GetOrderSaver().SaveOrder(order);
}
}
Ø
ServiceLocator Gi
implementation wb¤œiƒc n‡e-
public class ServiceLocator
{
public static IOrderSaver OrderSaver { get; set; }
public static IOrderSaver GetOrderSaver()
{
if (OrderSaver == null)
OrderSaver = new OrderDatabase();
return OrderSaver;
}
}
Ø GLv‡b
cÖ_‡g IOrderSaver Gi GKwU static instance
OrderSaver define Kiv
n‡q‡Q
Ø Then GetOrderSaver method Gi g‡a¨ check K‡i OrderSaver null wK
bv|
Ø hw` null nq Z‡e OrderDatabase class Gi GKwU new instance OrderSaver G
initialize
Kiv nq|
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন