public void CreateAll(IEnumerable <T> rangeToCreate)
 {
     foreach (var obj in rangeToCreate)
     {
         ActiveRecordMediator <T> .Create(obj);
     }
 }
Пример #2
0
        public void CreateOrUpdateVote(int userId, int postId, VoteType voteType)
        {
            var key = new VoteKey {
                UserId = userId, PostId = postId
            };
            int existingVoteValue = GetExistingVoteValue(key);
            int voteDiff          = GradeVote(voteType) - existingVoteValue;
            var vote = new TVoteOnPost
            {
                Key  = key,
                Vote = voteType
            };

            try
            {
                ActiveRecordMediator <TVoteOnPost> .Create(vote);
            }
            catch (GenericADOException)
            {
                ActiveRecordMediator <TVoteOnPost> .Update(vote);
            }
            // todo - use this instead of the above
            // http://stackoverflow.com/questions/2077949/castle-activerecord-save-is-throwing-stalestateexception
            // ActiveRecordMediator<TVoteOnPost>.Save(vote);
            UpdateVotesOnAnswer(postId, voteDiff);
        }
Пример #3
0
        internal static void SaveInstance(object instance, IController controller,
                                          ArrayList errors, ref IDictionary prop2Validation, bool create)
        {
            var isValid = true;

            var validationProvider = instance as IValidationProvider;

            if (validationProvider != null)
            {
                isValid = validationProvider.IsValid();

                if (!isValid)
                {
                    errors.AddRange(validationProvider.ValidationErrorMessages);
                    prop2Validation = validationProvider.PropertiesValidationErrorMessages;
                }
            }


            if (isValid)
            {
                if (create)
                {
                    ActiveRecordMediator.Create(instance);
                }
                else
                {
                    ActiveRecordMediator.Update(instance);
                }
            }
        }
Пример #4
0
        protected override int ExecuteInsert(IDictionary values)
        {
            ActiveRecordDataSourceCreateEventArgs args = new ActiveRecordDataSourceCreateEventArgs();

            args.ModelType    = BuildManager.GetType(Source.TypeName, false, true);
            args.CreateValues = values;

            CreateValuesFromCreateParameters(args);

            IClassMetadata meta = ActiveRecordMediator.GetSessionFactoryHolder().GetSessionFactory(args.ModelType).GetClassMetadata(args.ModelType);

            if (!meta.HasIdentifierProperty)
            {
                throw new ApplicationException("ActiveRecordDataSourceView requires a primary key for the update method.");
            }

            Source.OnBeforeCreate(args);

            if (args.Cancel)
            {
                return(0);
            }

            try
            {
                if (args.Entity == null)
                {
                    args.Entity = Activator.CreateInstance(args.ModelType);
                }

                foreach (string key in args.CreateValues.Keys)
                {
                    meta.SetPropertyValue(args.Entity, key, args.CreateValues[key]);
                }

                ActiveRecordMediator.Create(args.Entity);
            }
            catch (Exception e)
            {
                args.Exception = e;
                args.WasError  = true;

                Source.OnCreateError(args);

                if (Source.ThrowOnError && !args.DoNotThrow)
                {
                    throw;
                }

                return(0);
            }

            Source.OnCreate(args);

            return(1);
        }
        public void A_simple_object_can_be_read()
        {
            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Ship));
            Recreate();

            using (new SessionScope())
                ActiveRecordMediator <Ship> .Create(new Ship { Name = "Andrea Doria" });

            using (new StatelessSessionScope())
            {
                Assert.IsTrue(ActiveRecordMediator <Ship> .Exists(1));
                Assert.AreEqual("Andrea Doria", ActiveRecordMediator <Ship> .FindByPrimaryKey(1).Name);
            }
        }
Пример #6
0
        public void CanAddLazyToObject()
        {
            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(ObjectWithLazyAssociation),
                                           typeof(VeryLazyObject));

            Recreate();

            using (new SessionScope()) {
                var lazy = new VeryLazyObject();
                lazy.Title = "test";
                ActiveRecordMediator.Save(lazy);

                var lazyFromDb = (VeryLazyObject)ActiveRecordMediator.FindByPrimaryKey(typeof(VeryLazyObject), lazy.Id);
                var newObj     = new ObjectWithLazyAssociation();
                newObj.LazyObj = lazyFromDb;
                ActiveRecordMediator.Create(newObj);
            }
        }
Пример #7
0
        protected override void CreateTestData()
        {
            lic1 = new ProductLicense();
            lic2 = new ProductLicense();

            perm1 = new AccountPermission("Permission 1");
            perm2 = new AccountPermission("Permission 2");

            user1 = new User("John Doe");
            user2 = new User("Mary Jane");

            ActiveRecordMediator <ProductLicense> .Create(lic1);

            ActiveRecordMediator <ProductLicense> .Create(lic2);

            ActiveRecordMediator <AccountPermission> .Create(perm1);

            ActiveRecordMediator <AccountPermission> .Create(perm2);

            ActiveRecordMediator <User> .Create(user1);

            ActiveRecordMediator <User> .Create(user2);
        }
Пример #8
0
 internal void Create()
 {
     ActiveRecordMediator.Create(this);
 }
 /// <summary>
 /// Creates (Saves) a new instance to the database.
 /// </summary>
 /// <param name="instance"></param>
 public void Create(T instance)
 {
     ActiveRecordMediator.Create(instance);
 }
 public void Create(T objectToCreate)
 {
     ActiveRecordMediator <T> .Create(objectToCreate);
 }
Пример #11
0
 /// <summary>
 /// Register te entity for save in the database when the unit of work
 /// is completed.
 /// </summary>
 /// <param name="entity">the entity to save</param>
 /// <returns>The saved entity</returns>
 public virtual T Save(T entity)
 {
     ActiveRecordMediator.Create(entity);
     return(entity);
 }