// Perfect use case for polymorphism that I was too lazy to implement (see switch). internal void Execute(PolicyData policyData) { // TODO: implement assertions to modify PolicyData switch (Name) { case nameof(WithServerRoot): policyData.ServerRoot = Args[0]; break; case nameof(PluralTypes): policyData.PluralTypes = true; break; case nameof(HyphenCasedTypes): policyData.HyphenCasedTypes = true; break; case nameof(WithType): policyData.TypeName = Args[0]; break; case nameof(WithId): policyData.IdMembers = policyData.Type.FindFieldOrPropertyMembers(m => m.Name == Args[0]); break; case nameof(WithSelfLinks): policyData.LinkNames["self"] = true; break; case nameof(WithCanonicalLinks): policyData.LinkNames["canonical"] = true; break; case nameof(CamelCasedNames): policyData.CamelCasedNames = true; break; case nameof(ReplaceName): policyData.ReplaceName.Add(policyData.Type.FindMemberInfo(Args[0]), Args[1]); break; case nameof(HideDefaults): break; // TODO case nameof(HideName): break; // TODO case nameof(HideDefault): break; // TODO case nameof(ShowDefault): break; // TODO default: throw new Exception($"Missing case {Name} in policy builder"); } }
/// <summary> /// Create a policy builder for a policy that applies to type /// </summary> /// <param name="type">type for which policy is being asserted</param> /// <param name="initialAsserts">parent policy from which to inherit behavior</param> public PolicyBuilder(Type type, IPolicyAsserter initialAsserts) { _policyData = new PolicyData(type, initialAsserts.Copy()); }
/// <summary> /// Create a policy builder that applies to the default type /// </summary> public PolicyBuilder() { _policyData = new PolicyData(typeof(object), new PolicyAsserter()); _defaultAsserts?.Invoke(_policyData.Asserter); }
/// <summary> /// Apply stored assertions to policy data. /// </summary> /// <param name="policyData">PolicyData object to modify.</param> internal void ProcessAssertions(PolicyData policyData) { // Console.WriteLine($"Building Policy for {policyData.Type}."); _asserts.ForEach(a => a.Execute(policyData)); }
/// <summary> /// Create a policy with specified behavior for a type. /// This constructor is only used by <see cref="PolicyBuilder"/>. /// </summary> internal Policy(PolicyData policyData) { _policyData = policyData ?? throw new ArgumentNullException(nameof(policyData)); }