/// <summary>
        /// Insert Data Object
        /// </summary>
        /// <param name="dataContext">Data Context</param>
        /// <param name="table">Data Table</param>
        /// <param name="newDataObject">New Data Object</param>
        protected override void InsertDataObject(object dataContext, object table, object newDataObject)
        {
            if (newDataObject is IValidate)
            {
                ((IValidate)newDataObject).Validate();
            }

            if (this.owner.RetrieveGeneratedId)
            {
                // look for the Provider property
                PropertyInfo pi = dataContext.GetType().GetProperties().Where(p => p.PropertyType.IsSubclassOf(typeof(DbEntityProvider))).FirstOrDefault();

                if (pi != null)
                {
                    // cast the Provider
                    DbEntityProvider provider = (DbEntityProvider)pi.GetValue(dataContext);

                    // get the Mapping for the Provider
                    BasicMapping mapping = (BasicMapping)provider.Mapping;

                    // get the Mapping Entity for the data object
                    MappingEntity mappingEntity = mapping.GetEntity(newDataObject.GetType());

                    // get the primary key MemberInfo
                    MemberInfo primaryKeyMemberInfo = mapping.GetMappedMembers(mappingEntity).Where(m => mapping.IsPrimaryKey(mappingEntity, m)).Single();

                    // verify that the primary key is generated by calling the protected method IsGenerated
                    MethodBase isGeneratedMethod = mapping.GetType().GetMethod("IsGenerated", BindingFlags.NonPublic | BindingFlags.Instance);
                    bool       isGeneratedId     = (bool)isGeneratedMethod.Invoke(mapping, new object[] { mappingEntity, primaryKeyMemberInfo });

                    if (isGeneratedId)
                    {
                        // create a type array for the generic types for the Insert<T, S> method
                        Type[] genericTypes = new Type[] { newDataObject.GetType(), typeof(int) };

                        // create type for Expression<Func<T, S>>
                        Type functionExpressionType = Expression.GetFuncType(genericTypes);

                        // create a Expression Parameter for the data object type
                        var p1 = Expression.Parameter(newDataObject.GetType(), "instance");

                        // create a Lambda Expression for the Func<T, S> call ... expression example:  d => d.ID
                        LambdaExpression expression = Expression.Lambda(functionExpressionType, Expression.Property(p1, primaryKeyMemberInfo.Name), p1);

                        // MethodInfo for: public static S Insert<T, S>(this IUpdatable<T> collection, T instance, Expression<Func<T, S>> resultSelector)
                        MethodInfo mi = typeof(IQToolkit.Updatable).GetMethods().Where(d => d.Name == "Insert" && d.IsGenericMethod && d.ReturnType.FullName == null).First();
                        mi = mi.MakeGenericMethod(genericTypes);
                        object id = mi.Invoke(null, new object[] { table, newDataObject, expression });

                        // set the newDataObject's primary key property
                        newDataObject.GetType().GetProperty(primaryKeyMemberInfo.Name).SetValue(newDataObject, (int)id);
                        return;
                    }
                }
            }

            ((IEntityTable)table).Insert(newDataObject);
        }
示例#2
0
 public CompositiveMapper(BasicMapping mapping, QueryTranslator translator)
     : base(mapping, translator)
 {
 }
示例#3
0
 public VfpBasicMapper(BasicMapping mapping, QueryTranslator translator)
     : base(mapping, translator)
 {
 }