public InheritensObjectProvider(string connectionString, IDataBaseProvider databaseProvider, MappingOptionsSet mappingOptionSet) { _mappingOptionSet = mappingOptionSet; _databaseProvider = databaseProvider; _connectionString = connectionString; InitializeMapping(); }
public RelationalObjectStore(string connectionString, IDataBaseProvider databaseProvider, MappingOptionsSet mappingOptionsSet, bool autoregister) { _relationalObjectProvider = new Dictionary <Type, IObjectProvider>(); _connectionString = connectionString; _autoregisterTypes = autoregister; _databaseProvider = databaseProvider; _mappingOptionsSet = mappingOptionsSet; }
public static void AddObjectStoreWithSqlite(this IServiceCollection services, string connectionString, MappingOptionsSet mappingOptionsSet = null) { if (mappingOptionsSet == null) { mappingOptionsSet = new MappingOptionsSet().AddDefaultRules(); } RelationalObjectStore relationalObjectStore = new RelationalObjectStore(connectionString, DataBaseProvider.Instance, mappingOptionsSet, true); ObjectStoreManager.DefaultObjectStore.RegisterObjectProvider(relationalObjectStore); services.Add(new ServiceDescriptor(typeof(IObjectProvider), relationalObjectStore)); }
partial void InitExpressionParser() { ExpressionParser = new ExpressionParser() .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} + {args.ParseChild(exp.Right)}", ExpressionType.Add, ExpressionType.AddChecked) .AddRule <BinaryExpression>((exp, args) => $"({args.ParseChild(exp.Left)}) AND ({args.ParseChild(exp.Right)})", ExpressionType.And, ExpressionType.AndAlso) .AddRule <BinaryExpression>((exp, args) => $"({args.ParseChild(exp.Left)}) OR ({args.ParseChild(exp.Right)})", ExpressionType.Or, ExpressionType.OrElse) .AddRule <ConstantExpression>((exp, args) => args.GetService <IParsingContext>().GetParameter(((IFillAbleObject)exp.Value).Keys.Single()), e => e.Value is IFillAbleObject, ExpressionType.Constant) .AddRule <ConstantExpression>((exp, args) => { string[] values = ((IEnumerable)exp.Value).OfType <object>().Select(x => args.ParseChild(Expression.Constant(x))).ToArray(); return(values.Length == 0 ? "(SELECT NULL)" : $"({string.Join(", ", values)})"); }, e => !(e.Value is string) && e.Value is IEnumerable, ExpressionType.Constant) .AddRule <ConstantExpression>((exp, args) => args.GetService <IParsingContext>().GetParameter(exp.Value)) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} IS NULL", x => x.Right is ConstantExpression && ((ConstantExpression)x.Right).Value == null, ExpressionType.Equal) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Right)} IS NULL", x => x.Left is ConstantExpression && ((ConstantExpression)x.Left).Value == null, ExpressionType.Equal) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} IS NOT NULL", x => x.Right is ConstantExpression && ((ConstantExpression)x.Right).Value == null, ExpressionType.NotEqual) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Right)} IS NOT NULL", x => x.Left is ConstantExpression && ((ConstantExpression)x.Left).Value == null, ExpressionType.NotEqual) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} = {args.ParseChild(exp.Right)}", ExpressionType.Equal) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} != {args.ParseChild(exp.Right)}", ExpressionType.NotEqual) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} > {args.ParseChild(exp.Right)}", ExpressionType.GreaterThan) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} >= {args.ParseChild(exp.Right)}", ExpressionType.GreaterThanOrEqual) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} < {args.ParseChild(exp.Right)}", ExpressionType.LessThan) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} <= {args.ParseChild(exp.Right)}", ExpressionType.LessThanOrEqual) .AddRule <BinaryExpression>((exp, args) => $"{args.ParseChild(exp.Left)} - {args.ParseChild(exp.Right)}", ExpressionType.Subtract, ExpressionType.SubtractChecked) .AddRule <MemberExpression>((exp, args) => $"{args.ParseChild(exp.Expression)}.{(MappingOptionsSet.GetExistingMemberMappingOptions(exp) as FieldMappingOptions).DatabaseFieldName}", e => e.Member.MemberType == MemberTypes.Property && e.Expression.NodeType == ExpressionType.Parameter, ExpressionType.MemberAccess) .AddRule <MemberExpression>((exp, args) => $"{args.GetService<IParsingContext>().GetJoin((MemberExpression)exp.Expression)}.{(MappingOptionsSet.GetExistingMemberMappingOptions(exp) as FieldMappingOptions).DatabaseFieldName}", e => e.Member.MemberType == MemberTypes.Property && e.Expression is MemberExpression && ((MemberExpression)e.Expression).Member.MemberType == MemberTypes.Property && MappingOptionsSet.GetExistingMemberMappingOptions(e.Expression) is ForeignObjectMappingOptions, ExpressionType.MemberAccess) .AddRule <MethodCallExpression>((exp, args) => $"{args.ParseChild(exp.Arguments[1])} IN {args.ParseChild(exp.Arguments[0])}", x => x.Method.DeclaringType == typeof(Enumerable) && x.Method.Name == nameof(Enumerable.Contains) && x.Arguments.Count == 2) .AddRule <ParameterExpression>((exp, args) => args.GetService <IParsingContext>().GetAlias(exp)) .AddRule <LambdaExpression>((exp, args) => args.ParseChild(exp.Body)) .AddRule <UnaryExpression>((exp, args) => args.ParseChild(exp.Operand), ExpressionType.Convert, ExpressionType.ConvertChecked); }
public static MappingOptionsSet AddDefaultRules(this MappingOptionsSet mappingOptionsSet) { mappingOptionsSet.AddTypeRule(x => !x.GetTypeInfo().IsAbstract, o => { throw new NotSupportedException("InheritedPropertyMapping supports abstract classes and interfaces only."); }); mappingOptionsSet.AddTypeRule(x => x.GetCustomAttribute <TableAttribute>() != null, o => { TableAttribute attribute = o.Type.GetCustomAttribute <TableAttribute>(); o.TableName = attribute.TableName; o.LoadBehavior = attribute.LoadBehavior; }); mappingOptionsSet.AddTypeRule(x => x.GetTypeInfo().ImplementedInterfaces.Contains(typeof(INotifyPropertyChanged)), o => { o.RaisePropertyChangeMethod = o.Type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == typeof(PropertyChangedEventArgs) && x.GetCustomAttributes(typeof(IsRaisePropertyChangeMethodAttribute), true).Any()).FirstOrDefault(); }); mappingOptionsSet.AddMemberTypeRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() != null, x => { x.MappingType = MappingType.ForeignObjectMapping; }); mappingOptionsSet.AddMemberTypeRule(x => x.GetCustomAttribute <ReferenceListMappingAttribute>() != null, x => { x.MappingType = MappingType.ReferenceListMapping; }); mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() != null, o => { ForeignObjectMappingOptions options = (ForeignObjectMappingOptions)o; ForeignObjectMappingAttribute attribute = options.Member.GetCustomAttribute <ForeignObjectMappingAttribute>(); options.DatabaseFieldName = attribute.Fieldname; if (attribute.ForeignObjectType != null) { options.ForeignObjectType = attribute.ForeignObjectType; } if (attribute.ReadOnly) { options.IsReadonly = true; } else { options.IsInsertable = attribute.Insertable; options.IsUpdateable = attribute.Updateable; } options.IsPrimaryKey = options.Member.GetCustomAttribute <IsPrimaryKeyAttribute>() != null; }); mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ReferenceListMappingAttribute>() != null, o => { ReferenceListMappingOptions options = (ReferenceListMappingOptions)o; ReferenceListMappingAttribute attribute = options.Member.GetCustomAttribute <ReferenceListMappingAttribute>(); options.SaveCascade = attribute.SaveCascade; options.DeleteCascade = attribute.DeleteCascade; options.DropChangesCascade = attribute.DropChangesCascade; options.ForeignProperty = attribute.ForeignProperty ?? attribute.ForeignType.GetProperties().Where(x => x.PropertyType == options.Member.DeclaringType).Single(); foreach (EqualsObjectConditionAttribute equalsObjectAttribute in options.Member.GetCustomAttributes <EqualsObjectConditionAttribute>()) { options.Conditions.Add(attribute.ForeignType.GetProperty(equalsObjectAttribute.PropertyName), equalsObjectAttribute.Value); } }); mappingOptionsSet.AddMemberMappingRule(x => x.GetCustomAttribute <ForeignObjectMappingAttribute>() == null && x.GetCustomAttribute <ReferenceListMappingAttribute>() == null, o => { FieldMappingOptions options = (FieldMappingOptions)o; MappingAttribute attribute = options.Member.GetCustomAttribute <MappingAttribute>(); if (attribute != null) { options.DatabaseFieldName = attribute.FieldName ?? options.DatabaseFieldName; if (attribute.ReadOnly) { options.IsReadonly = true; } else { options.IsInsertable = attribute.Insertable; options.IsUpdateable = attribute.Updateable; } } options.IsPrimaryKey = options.Member.GetCustomAttribute <IsPrimaryKeyAttribute>() != null; }); return(mappingOptionsSet); }