public Policy CreatePolicy(string masterContractNumber, decimal coverAmount) { //normally we would get much of the parameters below from a database lookup, factory or service. //we simplify it here for the sake of brevity and simply hardcode most values. try { var newPolicy = new Policy { CaptureDate = DateTime.Now.Date, DateOfCommencement = GetFirstDayOfNextMonth(), IsActive = true, MasterContractNumber = masterContractNumber, SumAssured = coverAmount, PlanCode = "PL", PlanDescription = "Premium Life", PolicyNumber = CreatePolicyNumber(), Premium = coverAmount * (PremiumPercentageOfCover / 100) }; unitOfWork.Repository.Add(newPolicy); ValidatePolicy(newPolicy); unitOfWork.Commit(); return newPolicy; } catch (Exception) { unitOfWork.Rollback(); throw; } }
public void TestInit() { policyService = new PolicyApplicationService(new UnitOfWork(new InMemoryRepository())); //add default policy for testing defaultTestPolicy = policyService.CreatePolicy(defaultMasterContract, defaultCoverAmount); }
static void PrintPolicyDetails(Policy policy) { Console.WriteLine("{0} policy {1} is currently {2} and provides {3} cover with a montly premium of {4}", policy.PlanDescription, policy.PolicyNumber, policy.IsActive ? "active" : "inactive", policy.SumAssured, policy.Premium); Console.WriteLine(); }
/// <summary> /// In transaction script architectures we often tend to see our validation concentrated in a sigle location such as this. /// An alternate form uses Data Annotation validations on our domain model class combined with more complex validation being done in a /// method such as this one. In the end, we are mainly focussed on cecking that the state of the domain object to be persisted /// is valid just before doing the actual persistence. /// </summary> private void ValidatePolicy(Policy policy) { if (policy == null) throw new ArgumentNullException("policy"); if (String.IsNullOrWhiteSpace(policy.PlanCode)) throw new Exception("The policy plan code must contain a valid string."); if (policy.PlanCode.Length != 2) throw new Exception("The policy plan code must contain exactly two characters"); if (String.IsNullOrWhiteSpace(policy.MasterContractNumber)) throw new Exception("The policy Master Contract Number code must contain a valid string."); if (policy.MasterContractNumber.Length != 4 && policy.MasterContractNumber.ToCharArray().Any(c => !Char.IsDigit(c))) throw new Exception("The policy Master Contract Number must contain exactly four digits."); if (!policy.IsActive && policy.Premium > 0) throw new Exception("An inactive policy may not have a premium that is greater than 0."); if(!policy.IsActive && policy.SumAssured > 0) throw new Exception("An inactive policy may not provide a valid cover amount"); }