| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | | 3 | | |
| | | 4 | | namespace Microsoft.Azure.Batch |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Globalization; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Controls access to a set of properties. All reads/writes pass through here. |
| | | 11 | | /// </summary> |
| | | 12 | | internal class PropertyAccessController |
| | | 13 | | { |
| | | 14 | | private readonly BindingState bindingState; |
| | | 15 | | |
| | | 16 | | internal BindingState BindingState |
| | | 17 | | { |
| | 957 | 18 | | get { return this.bindingState; } |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates a new instance of a <see cref="PropertyAccessController"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="bindingState">The binding state to set this on this acesss controller.</param> |
| | 49915 | 25 | | public PropertyAccessController(BindingState bindingState) |
| | | 26 | | { |
| | 49915 | 27 | | this.bindingState = bindingState; |
| | 49915 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Executes the specified <paramref name="propertyReadAction"/>. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <typeparam name="T">The type of the property to read.</typeparam> |
| | | 34 | | /// <param name="propertyReadAction">The property read action to execute.</param> |
| | | 35 | | /// <param name="allowedAccess">The allowed access of the particular property.</param> |
| | | 36 | | /// <param name="propertyName">The name of the property.</param> |
| | | 37 | | /// <returns>The result of the <paramref name="propertyReadAction"/>.</returns> |
| | | 38 | | public T ReadProperty<T>(Func<T> propertyReadAction, BindingAccess allowedAccess, string propertyName) |
| | | 39 | | { |
| | 552436 | 40 | | if (!this.IsAccessAllowed(BindingAccess.Read, allowedAccess)) |
| | | 41 | | { |
| | 54 | 42 | | throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, BatchErrorMessages.Prope |
| | | 43 | | } |
| | | 44 | | |
| | 552382 | 45 | | return propertyReadAction(); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Executes the specified <paramref name="propertyWriteAction"/> |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="propertyWriteAction">The write action.</param> |
| | | 52 | | /// <param name="allowedAccess">The allowed access of the particular property.</param> |
| | | 53 | | /// <param name="propertyName">The name of the property.</param> |
| | | 54 | | public void WriteProperty(Action propertyWriteAction, BindingAccess allowedAccess, string propertyName) |
| | | 55 | | { |
| | 41231 | 56 | | if (!this.IsAccessAllowed(BindingAccess.Write, allowedAccess)) |
| | | 57 | | { |
| | 20 | 58 | | throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, BatchErrorMessages.Prope |
| | | 59 | | } |
| | | 60 | | |
| | 41211 | 61 | | propertyWriteAction(); |
| | 40968 | 62 | | } |
| | | 63 | | |
| | | 64 | | private bool IsAccessAllowed(BindingAccess requestedAccess, BindingAccess allowedAccess) |
| | | 65 | | { |
| | 593667 | 66 | | bool accessAllowed = allowedAccess.HasFlag(requestedAccess); |
| | | 67 | | |
| | 593667 | 68 | | return accessAllowed; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |