private static object GetValue(Type type, Value value, IPocoFactory pocoFactory) { Tuple <bool, object> x; x = HandleNull(type, value); if (x.Item1) { return(x.Item2); } x = HandleEnum(type, value); if (x.Item1) { return(x.Item2); } x = HandleBinary(type, value); if (x.Item1) { return(x.Item2); } x = HandleString(type, value); if (x.Item1) { return(x.Item2); } x = HandleEnumerable(type, value, pocoFactory); if (x.Item1) { return(x.Item2); } x = HandleBasicTypes(type, value); if (x.Item1) { return(x.Item2); } x = HandleComplexType(type, value, pocoFactory); if (x.Item1) { return(x.Item2); } throw new NotSupportedException($"The type {type.Name} is not supported"); }
/// <summary> /// Instantiate using the default stack /// </summary> /// <param name="datastoreDb">The low-level datastore connection. Needed to generate keys.</param> public DatastoreOrm(DatastoreDb datastoreDb) { this.datastoreDb = datastoreDb; reflector = new DatastoreReflector(); entityFactory = new ReflectionBasedEntityFactory(reflector); pocoFactory = new ReflectionBasedPocoFactory(reflector); }
/// <summary> /// Creates and configures a <see cref="IPoco"/> instance. /// </summary> /// <typeparam name="T">Type of the poco.</typeparam> /// <param name="this">This poco factory.</param> /// <param name="configure">Configuration action.</param> /// <returns>The configured instance.</returns> public static T Create <T>(this IPocoFactory <T> @this, Action <T> configure) where T : IPoco { T p = @this.Create(); configure(p); return(p); }
public AmbientThatDependsOnAllKindOfSingleton( IExternalService e, IPocoFactory <ISamplePoco> pocoFactory, ISampleRealObject contract, AmbientThatDependsOnSingleton d, SimpleClassSingleton s, AmbientThatWillBeResolvedAsSingleton other) { }
public AmbientThatDependsOnAllKindOfSingletonAndAnOtherExternalService( IExternalService e, IPocoFactory <ISamplePoco> pocoFactory, ISampleRealObject contract, AmbientThatDependsOnSingleton d, SimpleClassSingleton s, AmbientThatDependsOnAnotherExternalService o) { }
/// <summary> /// Reads a typed Poco from a Json reader (that can be null). /// <para> /// If the reader starts with a '[', it must be a 2-cells array with this Poco's type that /// comes first (otherwise an exception is thrown). /// If the reader starts with a '{', then it must be the Poco's value. /// </para> /// </summary> /// <typeparam name="T">The poco type.</typeparam> /// <param name="this">This poco factory.</param> /// <param name="reader">The reader.</param> /// <param name="options">The options.</param> /// <returns>The Poco (can be null).</returns> public static T?Read <T>(this IPocoFactory <T> @this, ref Utf8JsonReader reader, PocoJsonSerializerOptions?options = null) where T : class, IPoco { if (@this == null) { throw new ArgumentNullException(nameof(@this)); } if (CheckNullStart(ref reader, "expecting Json object, Json array or null value.")) { return(null); } return(((IFactoryReader <T>)@this).Read(ref reader, options)); }
/// <summary> /// Instantiate with custom stack or through Depency Injection /// </summary> /// <param name="datastoreDb">The low-level datastore connection. Needed to generate keys.</param> /// <param name="reflector">Answers questions about a POCO</param> /// <param name="entityFactory">Creates entities from POCOs</param> /// <param name="pocoFactory">Creates POCOs from entities</param> public DatastoreOrm( DatastoreDb datastoreDb, DatastoreReflector reflector, IEntityFactory entityFactory, IPocoFactory pocoFactory ) { this.datastoreDb = datastoreDb; this.reflector = reflector; this.entityFactory = entityFactory; this.pocoFactory = pocoFactory; }
/// <summary> /// Populates a new instance of <typeparamref name="T"/> with the provided KeyValuePair<string, object>. /// </summary> /// <typeparam name="T">The POCO type.</typeparam> /// <param name="this">This POCO factory.</param> /// <param name="payload">The payload. Must not be null.</param> /// <returns>The resulting POCO.</returns> public static T ExtractPayload <T>( this IPocoFactory <T> @this, IEnumerable <KeyValuePair <string, object> > payload) where T : IPoco { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } T info = @this.Create(); var properties = @this.PocoClassType.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var kv in payload) { var property = properties.FirstOrDefault(p => StringComparer.OrdinalIgnoreCase.Equals(kv.Key, p.Name)); if (property != null) { try { var targetType = property.PropertyType; var pType = targetType.GetTypeInfo(); if (pType.IsGenericType && (pType.GetGenericTypeDefinition() == typeof(Nullable <>))) { if (kv.Value == null) { property.SetValue(info, null); continue; } targetType = Nullable.GetUnderlyingType(targetType); pType = targetType.GetTypeInfo(); } object value; string stringValue; if (pType.IsEnum && (stringValue = kv.Value as string) != null) { value = Enum.Parse(targetType, stringValue); } else { value = Convert.ChangeType(kv.Value, targetType); } property.SetValue(info, value); } catch (Exception ex) { throw new ArgumentException($"Invalid payload. Unable to set '{property.Name}'. See inner exceptions for details.", nameof(payload), ex); } } } return(info); }
public static object FromEntityValue(PropertyInfo prop, Value value, IPocoFactory pocoFactory) { if (prop == null) { throw new ArgumentNullException(nameof(prop), "Cannot extract the proper data from the Value without having the property"); } if (value == null) { throw new ArgumentNullException(nameof(value), "Cannot extract data from a null Value"); } return(GetValue(prop.PropertyType.GetTypeOrUnderlyingNullableType(), value, pocoFactory)); }
public DatastoreOrmTests() { reflector = A.Fake <DatastoreReflector>(); entityFactory = A.Fake <IEntityFactory>(); datastoreDb = A.Fake <DatastoreDb>(); pocoFactory = A.Fake <IPocoFactory>(); entity = new Entity(); A.CallTo(() => entityFactory.FromPoco(datastoreDb, A <string> ._, A <object> ._)).Returns(entity); A.CallTo(() => reflector.GetKind(A <Type> ._)).Returns("SoKind"); orm = new DatastoreOrm( datastoreDb, reflector, entityFactory, pocoFactory ); }
/// <summary> /// Extracts payload by first checking whether <paramref name="payload"/> is already a <typeparamref name="T"/> /// and then by trying <see cref="ExtractPayload{T}(IPocoFactory{T}, IEnumerable{KeyValuePair{string, object}})"/>. /// </summary> /// <typeparam name="T">The POCO type.</typeparam> /// <param name="this">This POCO factory.</param> /// <param name="payload">The payload. Must not be null.</param> /// <returns>The resulting POCO.</returns> public static T ExtractPayload <T>(this IPocoFactory <T> @this, object payload) where T : IPoco { if (payload == null) { throw new ArgumentNullException(nameof(payload)); } if (payload is T) { return((T)payload); } var kindOfDic = payload as IEnumerable <KeyValuePair <string, object> >; if (kindOfDic != null) { return(ExtractPayload(@this, kindOfDic)); } throw new ArgumentException($"Invalid payload. It must be a '{typeof( T ).Name}' POCO or a IEnumerable<KeyValuePair<string, object>>.", nameof(payload)); }
private static Tuple <bool, object> HandleEnumerable(Type type, Value value, IPocoFactory pocoFactory) { if (type.ImplementsInterface(typeof(IEnumerable))) { var values = value.ArrayValue.Values; var elmType = type.GetElementType(); if (type.IsArray) { var output = Array.CreateInstance(elmType, values.Count); for (var i = 0; i < output.Length; i++) { output.SetValue(GetValue(elmType, values[i], pocoFactory), i); } return(new Tuple <bool, object>(true, output)); } elmType = GetElementTypeFromEnumerable(type); if (elmType == null) { throw new NotSupportedException($"I am unable to recreate a {type.Name} as I do not know what type of element it enumerates"); } var data = (IList)Activator.CreateInstance(type); foreach (var item in values) { data.Add(GetValue(elmType, item, pocoFactory)); } return(new Tuple <bool, object>(true, data)); } return(new Tuple <bool, object>(false, null)); }
public SomethingThatUsesAPoco(IPocoFactory pocoFactory, IPocoLogic logicForSomething) { _poco = pocoFactory.CreateInstance(); }
void Construct(IPocoFactory <IUserGoogleInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IUserOidcInfo> infoFactory) { _infoFactory = infoFactory; }
/// <summary> /// Reads a typed Poco (that can be null) from a string. /// <para> /// If the buffer starts with a '[', it must be a 2-cells array with this Poco's type that /// comes first (otherwise an exception is thrown). /// If the buffer starts with a '{', then it must be the Poco's value. /// </para> /// </summary> /// <typeparam name="T">The poco type.</typeparam> /// <param name="this">This poco factory.</param> /// <param name="s">The string to deserialize.</param> /// <param name="options">The options.</param> /// <returns>The Poco (can be null).</returns> public static T?JsonDeserialize <T>(this IPocoFactory <T> @this, string s, PocoJsonSerializerOptions?options = null) where T : class, IPoco { return(JsonDeserialize(@this, System.Text.Encoding.UTF8.GetBytes(s).AsSpan(), options)); }
private static Tuple <bool, object> HandleComplexType(Type type, Value value, IPocoFactory pocoFactory) { if (type.GetTypeInfo().IsClass) { var obj = pocoFactory.FromEmbeddedEntity(type, value.EntityValue); return(new Tuple <bool, object>(true, obj)); } return(new Tuple <bool, object>(false, null)); }
/// <summary> /// Instantiates a new GuestActor direct login allower /// </summary> /// <param name="infoFactory">The factory used for payload extraction.</param> public GuestActorDirectLoginAllower(IPocoFactory <IGuestActorInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IUserSimpleCodeInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IUserSimpleInvitationInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IUserFacebookInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IBasicPoco> f) { Factory = f; }
internal void StObjConstruct(IPocoFactory <ITokenInfo> tokenFactory) { _tokenFactory = tokenFactory; }
public PocoValueFactoryTests() { entityFactory = new ReflectionBasedEntityFactory(new DatastoreReflector()); pocoFactory = A.Fake <IPocoFactory>(o => o.Strict()); poco = new DasPoco(); }
internal void StObjConstruct(IPocoFactory <IGuestActorInfo> infoFactory, TokenStoreTable tokenStoreTable, ActorTable actorTable) { _infoFactory = infoFactory; }
/// <summary> /// Reads a typed Poco (that can be null) from Utf8 encoded bytes. /// <para> /// If the buffer starts with a '[', it must be a 2-cells array with this Poco's type that /// comes first (otherwise an exception is thrown). /// If the buffer starts with a '{', then it must be the Poco's value. /// </para> /// </summary> /// <typeparam name="T">The poco type.</typeparam> /// <param name="this">This poco factory.</param> /// <param name="utf8Json">The utf8 encoded bytes to deserialize.</param> /// <param name="options">The options.</param> /// <returns>The Poco (can be null).</returns> public static T?JsonDeserialize <T>(this IPocoFactory <T> @this, ReadOnlySpan <byte> utf8Json, PocoJsonSerializerOptions?options = null) where T : class, IPoco { var r = new Utf8JsonReader(utf8Json); return(Read(@this, ref r, options)); }
void StObjConstruct(IPocoFactory <IUserGitHubInfo> infoFactory) { _infoFactory = infoFactory; }
void StObjConstruct(IPocoFactory <IUserTwitterInfo> infoFactory) { _infoFactory = infoFactory; }