lundi 7 mars 2022

Correct way to implement strategy design Pattern in C#

I have a scenario wherein there are multiple conditions and I have to write a code in such a way that it should be open for extension and not for modification so I went with the strategy design pattern, below is my code, let me know if I'm on the correct path and need some guidance on the code written.

 BaseOrderProcessing baseOrderProcessing = new BaseOrderProcessing(new BaseSubscribe(new SubscribeService(Subscribe)));  
// Is this correct way to call it, how to apply dependency injection ?? You can find this code in the end

 

Here is the actual code:

    public interface IBaseOrderProcessing
        {
            void ProcessOrder();
        }

    public class BaseOrderProcessing : IBaseOrderProcessing
        {
            private IBaseOrderProcessing _baseOrderProcessing = null;
            public BaseOrderProcessing(IBaseOrderProcessing baseOrderProcessing)
            {
                _baseOrderProcessing = baseOrderProcessing;
            }
            public virtual void ProcessOrder()
            {
                _baseOrderProcessing.ProcessOrder();
            }
        }

 public interface ISubscribeService
    {
        SubscribeType SubscribeType { get; set; }
        void ActivateSubscribe();

        void UpgradeSubscribe();
    }
    
    // Strategy Pattern 1  => Subscribe is one of the "if" condition
    public class BaseSubscribe : IBaseOrderProcessing
    {
        private ISubscribeService _SubscribeService = null;
        public BaseSubscribe(ISubscribeService SubscribeService)
        {
            _SubscribeService = SubscribeService;
        }
        
        public void ProcessOrder()
        {
            if (_SubscribeService.SubscribeType == SubscribeType.ACTIVATE)
                _SubscribeService.ActivateSubscription();

            if (_SubscribeService.SubscribeType == SubscribeType.UPGRADE)
                _SubscribeService.UpgradeSubscription();
            
        }
    }

// Writing another class to simplify is correct ?????
 public class SubscribeService : ISubscribeService
    {
        private SubscribeDetails _Subscribedetails = null;

        public SubscribeType SubscribeType { get; set; }

        public SubscribeService(SubscribeDetails Subscribedetails)
        {
            _Subscribedetails = Subscribedetails;
            SubscribeType = Subscribedetails.SubscribeType;
        }
        public void ActivateSubscription()
        {
            // Code to save the Subscribe details in the database
            Console.WriteLine($"\n\nSubscribe {_Subscribedetails.SubscribeId} for {_Subscribedetails.SubscribeName} activated for order Id: {_Subscribedetails.OrderId}" +
                $" from {_Subscribedetails.SubscribeStartDate} to {_Subscribedetails.SubscribeEndDate}");        
        }      

        public void UpgradeSubscription()
        {
            // Code to upgrade the Subscribe details in the database
            Console.WriteLine($"\n\nSubscribe {_Subscribedetails.SubscribeId} for {_Subscribedetails.SubscribeName} upgraded for order Id: {_Subscribedetails.OrderId}" +
                $" from {_Subscribedetails.SubscribeStartDate} to {_Subscribedetails.SubscribeEndDate}");

        }
    }  
    
    
    
    
     // Client code: 
    
    IBaseOrderProcessing baseOrderProcessing = null;
     SubscribeDetails Subscribe = new SubscribeDetails();
                        Subscribe.OrderId = Guid.NewGuid();
                        Subscribe.SubscribeId = Guid.NewGuid();
                        Subscribe.SubscribeName = "Amazon";
                        Subscribe.SubscribeStartDate = DateTime.Now;
                        Subscribe.SubscribeEndDate = DateTime.Now.AddDays(30);
                        if (option == 1)
                            Subscribe.SubscribeType = SubscribeType.ACTIVATE;

                        if (option == 2)
                            Subscribe.SubscribeType = SubscribeType.UPGRADE;
    baseOrderProcessing = new BaseOrderProcessing(new BaseSubscribe(new SubscribeService(Subscribe)));  
// Is this correct way to call it, how to apply dependency injection ??

Aucun commentaire:

Enregistrer un commentaire