Ejemplo n.º 1
0
        public EnumerableContract(Type modelType, PropertyInfo enumerableProperty)
        {
            enumerableItemType = enumerableProperty.PropertyType.GenericTypeArguments[0];
            listType           = typeof(List <>).MakeGenericType(enumerableItemType);

            tableName = $"{enumerableItemType.Name.ToLower()}sTo{modelType.Name.ToLower()}s";
            this.enumerableProperty = enumerableProperty;

            virtualEnumerableProperty = new ModelProperty(new VirtualPropertyInfo(enumerableItemType.Name.ToLower(), enumerableItemType));
            virtualModelProperty      = new ModelProperty(new VirtualPropertyInfo(modelType.Name.ToLower(), modelType));

            // Use different getModel and getForeignKey Methods on non Model enumerables
            if (enumerableItemType.TryGetGenericBaseClass(typeof(Model <,>), out var baseClass))
            {
                var idProperty = baseClass !.GetProperty("Id", BindingFlags.NonPublic | BindingFlags.Instance);
                var getMethod  = baseClass.GetMethod("Get", new[] { idProperty.PropertyType });

                getModel      = foreignKey => getMethod.Invoke(null, new[] { foreignKey });
                getForeignKey = model => idProperty.GetValue(model);
            }
            else
            {
                getModel      = Return;
                getForeignKey = Return;
            }

            // Setup sql statements
            var virtualModelProperties = new[] { virtualEnumerableProperty, virtualModelProperty };

            onCreateSql = DbRequirements.SqlEngine.CreateTable(tableName, virtualModelProperties);
            onInsertSql = DbRequirements.SqlEngine.InsertModel(tableName, virtualModelProperties);
            onDeleteSql = DbRequirements.SqlEngine.DeleteModel(tableName, virtualModelProperty);
            onSelectSql = DbRequirements.SqlEngine.SelectModelEntriesByForeignKey(tableName, virtualModelProperties, virtualModelProperty);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="getForeignKey1Delegate">See <see cref="ManyToManyMemory{TManyToManyModelCreate,TManyToManyModel,TReferenceModel1Create,TReferenceModel1,TReferenceModel2Create,TReferenceModel2,TId}.GetForeignKeyDelegate"/>.</param>
 /// <param name="getForeignKey2Delegate">See <see cref="ManyToManyMemory{TManyToManyModelCreate,TManyToManyModel,TReferenceModel1Create,TReferenceModel1,TReferenceModel2Create,TReferenceModel2,TId}.GetForeignKeyDelegate"/>.</param>
 /// <param name="foreignHandler1">Functionality to read a specified parent.</param>
 /// <param name="foreignHandler2">Functionality to read a specified parent.</param>
 public ManyToManyMemory(GetForeignKeyDelegate getForeignKey1Delegate, GetForeignKeyDelegate getForeignKey2Delegate, ICrud <TReferenceModel1, TId> foreignHandler1, ICrud <TReferenceModel2, TId> foreignHandler2)
     : base(getForeignKey1Delegate, getForeignKey2Delegate, foreignHandler1, foreignHandler2)
 {
 }