1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
//---------------------------------------------------------------------
// <copyright file="Facet.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
namespace System.Data.Metadata.Edm
{
using System;
using System.Diagnostics;
using System.Globalization;
/// <summary>
/// Class for representing a Facet object
/// This object is Immutable (not just set to readonly) and
/// some parts of the system are depending on that behavior
/// </summary>
[DebuggerDisplay("{Name,nq}={Value}")]
public sealed class Facet : MetadataItem
{
#region Constructors
/// <summary>
/// The constructor for constructing a Facet object with the facet description and a value
/// </summary>
/// <param name="facetDescription">The object describing this facet</param>
/// <param name="value">The value of the facet</param>
/// <exception cref="System.ArgumentNullException">Thrown if facetDescription argument is null</exception>
private Facet(FacetDescription facetDescription, object value)
:base(MetadataFlags.Readonly)
{
EntityUtil.GenericCheckArgumentNull(facetDescription, "facetDescription");
_facetDescription = facetDescription;
_value = value;
}
/// <summary>
/// Creates a Facet instance with the specified value for the given
/// facet description.
/// </summary>
/// <param name="facetDescription">The object describing this facet</param>
/// <param name="value">The value of the facet</param>
/// <exception cref="System.ArgumentNullException">Thrown if facetDescription argument is null</exception>
internal static Facet Create(FacetDescription facetDescription, object value)
{
return Create(facetDescription, value, false);
}
/// <summary>
/// Creates a Facet instance with the specified value for the given
/// facet description.
/// </summary>
/// <param name="facetDescription">The object describing this facet</param>
/// <param name="value">The value of the facet</param>
/// <param name="bypassKnownValues">true to bypass caching and known values; false otherwise.</param>
/// <exception cref="System.ArgumentNullException">Thrown if facetDescription argument is null</exception>
internal static Facet Create(FacetDescription facetDescription, object value, bool bypassKnownValues)
{
EntityUtil.CheckArgumentNull(facetDescription, "facetDescription");
if (!bypassKnownValues)
{
// Reuse facets with a null value.
if (object.ReferenceEquals(value, null))
{
return facetDescription.NullValueFacet;
}
// Reuse facets with a default value.
if (object.Equals(facetDescription.DefaultValue, value))
{
return facetDescription.DefaultValueFacet;
}
// Special case boolean facets.
if (facetDescription.FacetType.Identity == "Edm.Boolean")
{
bool boolValue = (bool)value;
return facetDescription.GetBooleanFacet(boolValue);
}
}
Facet result = new Facet(facetDescription, value);
// Check the type of the value only if we know what the correct CLR type is
if (value != null && !Helper.IsUnboundedFacetValue(result) && !Helper.IsVariableFacetValue(result) && result.FacetType.ClrType != null)
{
Type valueType = value.GetType();
Debug.Assert(
valueType == result.FacetType.ClrType
|| result.FacetType.ClrType.IsAssignableFrom(valueType),
string.Format(CultureInfo.CurrentCulture, "The facet {0} has type {1}, but a value of type {2} was supplied.", result.Name, result.FacetType.ClrType, valueType)
);
}
return result;
}
#endregion
#region Fields
/// <summary>The object describing this facet.</summary>
private readonly FacetDescription _facetDescription;
/// <summary>The value assigned to this facet.</summary>
private readonly object _value;
#endregion
#region Properties
/// <summary>
/// Returns the kind of the type
/// </summary>
public override BuiltInTypeKind BuiltInTypeKind { get { return BuiltInTypeKind.Facet; } }
/// <summary>
/// Gets the description object for describing the facet
/// </summary>
public FacetDescription Description
{
get
{
return _facetDescription;
}
}
/// <summary>
/// Gets/Sets the name of the facet
/// </summary>
[MetadataProperty(PrimitiveTypeKind.String, false)]
public String Name
{
get
{
return _facetDescription.FacetName;
}
}
/// <summary>
/// Gets/Sets the type of the facet
/// </summary>
[MetadataProperty(BuiltInTypeKind.EdmType, false)]
public EdmType FacetType
{
get
{
return _facetDescription.FacetType;
}
}
/// <summary>
/// Gets/Sets the value of the facet
/// </summary>
/// <exception cref="System.InvalidOperationException">Thrown if the Facet instance is in ReadOnly state</exception>
[MetadataProperty(typeof(Object), false)]
public Object Value
{
get
{
return _value;
}
}
/// <summary>
/// Gets the identity for this item as a string
/// </summary>
internal override string Identity
{
get
{
return _facetDescription.FacetName;
}
}
/// <summary>
/// Indicates whether the value of the facet is unbounded
/// </summary>
public bool IsUnbounded
{
get
{
return object.ReferenceEquals(Value, EdmConstants.UnboundedValue);
}
}
#endregion
#region Methods
/// <summary>
/// Overriding System.Object.ToString to provide better String representation
/// for this type.
/// </summary>
public override string ToString()
{
return this.Name;
}
#endregion
}
}
|