public string HandleRequest(ILoanRequest request) { //A clerk can approve amount upto Rs 10,000 if (request.Amount <= 1000) return "Amount Approved by Clerk"; else Console.WriteLine("Loan request cannot be approved by "); //Else Clerk cannot approve,so pass it to the next successor return Successor.HandleRequest(request); }
//There is no successor to the Super Manager public string HandleRequest(ILoanRequest request) { if (request.Amount <=100000) { return "Request approved by Super Manager"; } else { throw new Exception("Bank can approve loan only uptill one lakh. Loan cannot be granted for amounts greater than 1 lac"); } }
public static string TrySuccessor(this IRequestHandler current, ILoanRequest request) { if (current.Successor == null) { return "There is no next successor"; } else { Console.WriteLine("Cannot process the request. Passing it onto the next successor"); return current.Successor.HandleRequest(request); } }
/// <summary> /// Calculates the response. /// </summary> /// <param name="request">Loan request object</param> /// <returns>Loan response object</returns> public ILoanResponse Calculate(ILoanRequest request) { Engine engine = new Engine(); List <Offer> workingOffers = engine.SelectOffers(request.Offers, request.LoanAmount); LoanResponse response = new LoanResponse(); response.RequestedAmount = request.LoanAmount; response.Rate = engine.GetAnnualRate(workingOffers, request.LoanAmount); response.TotalRepayment = engine.GetTotalRepayment(workingOffers); response.MounthlyRepayment = engine.GetMonthlyRepayment(workingOffers, response.TotalRepayment); return(response); }
public string HandleRequest(ILoanRequest request) { //A Manager can approve amount upto Rs 50,000 if (request.Amount <= 5000) return "Request approved by Manager"; else { //Else Clerk cannot approve,so pass it to the next successor which is Super Manager //return Successor.HandleRequest(request); //Other than the above line we can also use return this.TrySuccessor(request); } }
//This method will be called by Managed Extensibility Framework public bool HandleRequestMEF(ILoanRequest request) { //A clerk can approve amount upto Rs 10,000 if (request.Amount <= 1000) { Console.WriteLine("Your loan request for amount {0} has been approved by {1} ", request.Amount, this.GetType().ToString()); return true; } else { return false; } }
/// <summary> /// Validates a given request according to existing rules and limitations. /// </summary> /// <param name="request">Loan request object</param> public void ValidateRequest(ILoanRequest request) { if (request.LoanAmount < configProvider.AmountMin || request.LoanAmount > configProvider.AmountMax) { throw new ArgumentOutOfRangeException("Amount"); } if (request.LoanAmount % 100 != 0) { throw new ArgumentException("The requested amount should be divisible by 100"); } if (request.Offers.Sum(o => o.Amount) < request.LoanAmount) { throw new AmountNotAvailableException("The requested amount is not available"); } }
//Handle the request as it comes. Pass the request object and the name of the handler that will first process the request private bool TryHandle(IRequestHandler requestHandler, ILoanRequest request) { //Find if the curent request handler has got any Successor. If it has than assign the successor var firstOrDefault = Handlers.FirstOrDefault(handle => handle.Metadata.SuccessorOf == requestHandler.GetType()); //Handle the request first. If it gets handled, nothing to do if (requestHandler.HandleRequestMEF(request)) { return true; } //Reques did not get handled, try passing it on to the Successor else if (firstOrDefault != null) { requestHandler.Successor = firstOrDefault.Value; //Call the method again to process the request and set the successor, if there is any return TryHandle(requestHandler.Successor, request); } else { return false; } }
//This method exposes the public interface for handling the request public bool HandleRequest(ILoanRequest request) { return TryHandle(first, request); }