BO4E/meta/Bo4eUri.cs
/// <summary>
///     Get an URI for a Business Object
/// </summary>
/// <see cref="GetQueryObject" />
/// for the reverse operation.
/// <param name="bo">Business Object</param>
/// <param name="includeUserProperties">set true to add userProperties as query parameters</param>
/// <returns>Bo4eUri</returns>
/// call e.g.
/// <see cref="System.Uri.GetComponents" />
/// or
/// <see cref="System.Uri.ToString" />
/// on the returned object.
Get an URI for a Business Object
Get an URI for a Business Object
public static Bo4eUri GetUri(BusinessObject bo, bool includeUserProperties = false)
        {
            if (bo == null) throw new ArgumentNullException(nameof(bo), "Business Object must not be null.");
            var baseUriString = Bo4EScheme + bo.GetType().Name + "/";
            var baseUri = new Bo4eUri(baseUriString);
            var relativeUriBuilder = new StringBuilder();
            foreach (var keyProp in GetKeyFields(bo))
                if (keyProp.GetValue(bo) != null)
                {
                    if (keyProp.GetValue(bo) is string)
                    {
                        if (keyProp.GetValue(bo).ToString() == string.Empty)
                            relativeUriBuilder.Append(NullKeyPlaceholder + "/");
                        else
                            relativeUriBuilder.Append(keyProp.GetValue(bo) + "/");
                    }
                    else if (keyProp.GetValue(bo) is int)
                    {
                        relativeUriBuilder.Append(keyProp.GetValue(bo) + "/");
                    }
                    else if (keyProp.GetValue(bo) is Enum)
                    {
                        relativeUriBuilder.Append(keyProp.GetValue(bo) + "/");
                    }
                    else if (keyProp.GetValue(bo).GetType().IsSubclassOf(typeof(BusinessObject)))
                    {
                        var innerBo = (BusinessObject)keyProp.GetValue(bo);
                        relativeUriBuilder.Append(GetUri(innerBo)
                            .GetComponents(UriComponents.Path, UriFormat.UriEscaped));
                    }
                    else
                    {
                        throw new NotImplementedException(
                            $"Using {keyProp.GetValue(bo).GetType()} as [BoKey] is not supported yet.");
                    }
                }
                else
                {
                    relativeUriBuilder.Append(NullKeyPlaceholder + "/");
                }
            if (includeUserProperties && bo.UserProperties != null && bo.UserProperties.Any())
            {
                var n = 0;
                foreach (var up in bo.UserProperties)
                {
                    relativeUriBuilder.Append(n == 0 ? "?" : "&");
                    relativeUriBuilder.Append($"{up.Key}={up.Value}");
                    n += 1;
                }
            }
#pragma warning disable SYSLIB0013
            var relativeUri = new Uri(EscapeUriString(relativeUriBuilder.ToString()), UriKind.Relative);
#pragma warning restore SYSLIB0013
            return TryCreate(baseUri, relativeUri, out var resultUri) ? new Bo4eUri(resultUri.AbsoluteUri) : null;
        }
public static Bo4eUri GetUri ( BusinessObject bo , bool includeUserProperties = false ) { if ( bo == null ) throw new ArgumentNullException ( nameof ( bo ) , " " ) ; var baseUriString = Bo4EScheme + bo . GetType ( ) . Name + " " ; var baseUri = new Bo4eUri ( baseUriString ) ; var relativeUriBuilder = new StringBuilder ( ) ; foreach ( var keyProp in GetKeyFields ( bo ) ) if ( keyProp . GetValue ( bo ) != null ) { if ( keyProp . GetValue ( bo ) is string ) { if ( keyProp . GetValue ( bo ) . ToString ( ) == string . Empty ) relativeUriBuilder . Append ( NullKeyPlaceholder + " " ) ; else relativeUriBuilder . Append ( keyProp . GetValue ( bo ) + " " ) ; } else if ( keyProp . GetValue ( bo ) is int ) { relativeUriBuilder . Append ( keyProp . GetValue ( bo ) + " " ) ; } else if ( keyProp . GetValue ( bo ) is Enum ) { relativeUriBuilder . Append ( keyProp . GetValue ( bo ) + " " ) ; } else if ( keyProp . GetValue ( bo ) . GetType ( ) . IsSubclassOf ( typeof ( BusinessObject ) ) ) { var innerBo = ( BusinessObject ) keyProp . GetValue ( bo ) ; relativeUriBuilder . Append ( GetUri ( innerBo ) . GetComponents ( UriComponents . Path , UriFormat . UriEscaped ) ) ; } else { throw new NotImplementedException ( $" Using  { keyProp . GetValue ( bo ) . GetType ( ) }  as [BoKey] is not supported yet. " ) ; } } else { relativeUriBuilder . Append ( NullKeyPlaceholder + " " ) ; } if ( includeUserProperties && bo . UserProperties != null && bo . UserProperties . Any ( ) ) { var n = 0 ; foreach ( var up in bo . UserProperties ) { relativeUriBuilder . Append ( n == 0 ? " " : " " ) ; relativeUriBuilder . Append ( $" { up . Key } = { up . Value } " ) ; n += 1 ; } } pragma warning disable SYSLIB0013 var relativeUri = new Uri ( EscapeUriString ( relativeUriBuilder . ToString ( ) ) , UriKind . Relative ) ; pragma warning restore SYSLIB0013 return TryCreate ( baseUri , relativeUri , out var resultUri ) ? new Bo4eUri ( resultUri . AbsoluteUri ) : null ; }
Get an URI for a Business Object
Get an URI for a Business Object
//relativeUriString += keyField.Name; // line is useful for debugging // there must be some value for null, because if the key is a composite key // you could not distinguish which field was null. // Think of two Ansprechpartners: // - Günther Hermann but Günther is not set/null ==> /~/Hermann
 // - Hermann Maier but Maier is not set/null ==> /Hermann/~/
None
bo
BusinessObject
None
includeUserProperties
bool
set true to add userProperties as query parameters
set true to add userProperties as query parameters
see
for the reverse operation.
for the reverse operation .
see
None
see
on the returned object.
on the returned object .