Esempio n. 1
0
 public virtual void Delete(string[] ids, string[] memberTypes = null)
 {
     using (var repository = RepositoryFactory())
     {
         var members = GetByIds(ids, null, memberTypes);
         if (!members.IsNullOrEmpty())
         {
             foreach (var member in members)
             {
                 MemberChangingEventPublisher.Publish(new MemberChangingEvent(EntryState.Deleted, member));
             }
             repository.RemoveMembersByIds(members.Select(m => m.Id).ToArray());
             CommitChanges(repository);
             foreach (var member in members)
             {
                 DynamicPropertyService.DeleteDynamicPropertyValues(member);
                 var seoObject = member as ISeoSupport;
                 if (seoObject != null)
                 {
                     CommerceService.DeleteSeoForObject(seoObject);
                 }
             }
             foreach (var member in members)
             {
                 MemberChangedEventPublisher.Publish(new MemberChangedEvent(EntryState.Deleted, member));
             }
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Create or update members in database
        /// </summary>
        /// <param name="members"></param>
        public virtual void SaveChanges(Member[] members)
        {
            var pkMap         = new PrimaryKeyResolvingMap();
            var changedEvents = new List <MemberChangedEvent>();

            using (var repository = RepositoryFactory())
                using (var changeTracker = GetChangeTracker(repository))
                {
                    var existingMemberEntities = repository.GetMembersByIds(members.Where(m => !m.IsTransient()).Select(m => m.Id).ToArray());

                    foreach (var member in members)
                    {
                        var memberEntityType = AbstractTypeFactory <Member> .AllTypeInfos.Where(t => t.MappedType != null && t.IsAssignableTo(member.MemberType)).Select(t => t.MappedType).FirstOrDefault();

                        if (memberEntityType != null)
                        {
                            var dataSourceMember = AbstractTypeFactory <MemberDataEntity> .TryCreateInstance(memberEntityType.Name);

                            if (dataSourceMember != null)
                            {
                                dataSourceMember.FromModel(member, pkMap);

                                var dataTargetMember = existingMemberEntities.FirstOrDefault(m => m.Id == member.Id);
                                if (dataTargetMember != null)
                                {
                                    changeTracker.Attach(dataTargetMember);
                                    dataSourceMember.Patch(dataTargetMember);
                                    MemberChangingEventPublisher.Publish(new MemberChangingEvent(EntryState.Modified, member));
                                    changedEvents.Add(new MemberChangedEvent(EntryState.Modified, member));
                                }
                                else
                                {
                                    repository.Add(dataSourceMember);
                                    MemberChangingEventPublisher.Publish(new MemberChangingEvent(EntryState.Added, member));
                                    changedEvents.Add(new MemberChangedEvent(EntryState.Added, member));
                                }
                            }
                        }
                    }

                    CommitChanges(repository);
                    pkMap.ResolvePrimaryKeys();
                }

            //Save dynamic properties
            foreach (var member in members)
            {
                DynamicPropertyService.SaveDynamicPropertyValues(member);
            }

            CommerceService.UpsertSeoForObjects(members.OfType <ISeoSupport>().ToArray());

            foreach (var changedEvent in changedEvents)
            {
                MemberChangedEventPublisher.Publish(changedEvent);
            }
        }