Exemplo n.º 1
0
        protected virtual void LoadEntities(SaveModel saveModel)
        {
            foreach (SaveEntitySet saveEntitySet in saveModel.GetEntitySets())
            {
                Type entityType = saveEntitySet.EntityType;
                ApiEntityTypeConfiguration entityTypeConfig;

                if (!this.Model.TryGetEntityTypeConfig(entityType, out entityTypeConfig))
                {
                    throw new Exception("Entity type not supported.");
                }

                object     resource  = this.resources[entityTypeConfig.ResourceType];
                IQueryable queryable = (IQueryable)entityTypeConfig.ResourceAccessor.DynamicInvoke(resource);

                // TODO: It's not always int IDs
                PropertyInfo idPropertyInfo = entityType.GetProperty("Id");
                List <int>   ids            = saveEntitySet.Where(m => !m.IsAdded).Select(
                    m => (int)idPropertyInfo.GetValue(m.EntityInfo.Entity)).ToList();

                var arg = Expression.Parameter(entityType, "p");

                var body = Expression.Call(Expression.Constant(ids), "Contains", null,
                                           Expression.Property(arg, "Id"));

                var predicate = Expression.Lambda(body, arg);

                var where = Expression.Call(typeof(Queryable), "Where",
                                            new Type[] { queryable.ElementType },
                                            queryable.Expression, Expression.Quote(predicate));

                foreach (object entity in queryable.Provider.CreateQuery(where))
                {
                    int        id         = (int)idPropertyInfo.GetValue(entity);
                    SaveEntity saveEntity = saveEntitySet.First(m => (int)idPropertyInfo.GetValue(m.EntityInfo.Entity) == id);
                    saveEntity.Entity = entity;
                }
            }
        }
Exemplo n.º 2
0
 void ISaveHandler.Save(SaveEntity saveEntity)
 {
     Save((SaveEntity <TEntityType>)saveEntity);
 }
Exemplo n.º 3
0
 public abstract void Save(SaveEntity <TEntityType> saveEntity);