Exemple #1
0
        public void OpenAccess_ConcurrentlyUpdateTheSameRecord_OptimisticConcurrencyExceptionShouldBeThrown()
        {
            //arrange
            var newRecordId = CreateNewOpenAccessRecord();

            using (var context1 = new OpenAccessContext())
            {
                var product1 = context1.GetAll <OpenAccessProduct>().First(p => p.Id == newRecordId);
                product1.Price = 1m;
                using (var context2 = new OpenAccessContext())
                {
                    var product2 = context2.GetAll <OpenAccessProduct>().First(p => p.Id == newRecordId);
                    product2.Price = 2m;

                    //act
                    context2.SaveChanges();
                }
                try
                {
                    context1.SaveChanges();
                }
                //assert
                catch (OptimisticVerificationException exception)
                {
                    System.Diagnostics.Trace.WriteLine(exception.ToString());
                    return;
                }
            }

            //We reached end of the test...it means that we assumed incorrect behaviour
            Assert.Fail();
        }
Exemple #2
0
 protected override void OnActionExecuted(ActionExecutedContext filterContext)
 {
     base.OnActionExecuted(filterContext);
     if ((!filterContext.IsChildAction && (filterContext.Exception == null)) && (_context != null))
     {
         _context.SaveChanges(ConcurrencyConflictsProcessingMode.StopOnFirst);
     }
 }
 /// <summary>
 /// Logouts a session from the system.
 /// </summary>
 /// <param name="sessionToken">The session token.</param>
 /// <exception cref="System.NotImplementedException"></exception>
 public void Logout(string sessionToken)
 {
     using (OpenAccessContext context = this.NewContext())
     {
         this.SessionManager.RemoveSession(context, sessionToken);
         context.SaveChanges();
     }
 }
Exemple #4
0
        private Guid CreateNewOpenAccessRecord()
        {
            using (var context = new OpenAccessContext())
            {
                var product = new OpenAccessProduct()
                {
                    Id = Guid.NewGuid()
                };

                context.Add(product);
                context.SaveChanges();
                return(product.Id);
            }
        }
 /// <summary>
 /// Logins a user into the system.
 /// </summary>
 /// <param name="login">The login.</param>
 /// <param name="password">The password.</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentException">Login invalid</exception>
 public IUserDto Login(string login, string password)
 {
     using (OpenAccessContext context = this.NewContext())
     {
         object user = this.GetUserByLoginPass(context, login, password);
         if (user == null)
         {
             throw new ArgumentException("Login invalid");
         }
         IUserDto result = (IUserDto)ObjectProxyFactory.MapTo(this.UserDtoType, user);
         result.Password     = null;
         result.SessionToken = this.SessionManager.NewSessionToken();
         this.SessionManager.AddSessionForUser(context, result.UserId.Value, result.SessionToken);
         context.SaveChanges();
         return(result);
     }
 }
        /// <summary>
        /// Gets the user information.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="sessionToken">The session token.</param>
        /// <returns></returns>
        protected virtual IUserDto GetUserInfo(OpenAccessContext context, string sessionToken)
        {
            ISessionDto sessionData = this.SessionManager.GetSessionData(context, sessionToken);

            if (sessionData == null)
            {
                return(null);
            }
            object user = this.GetUserById(context, sessionData.UserId);

            if (user == null)
            {
                this.SessionManager.RemoveSession(context, sessionToken);
                context.SaveChanges();
                return(null);
            }
            IUserDto result = (IUserDto)ObjectProxyFactory.MapTo(this.UserDtoType, user);

            result.Password     = null;
            result.SessionToken = sessionData.SessionToken;
            return(result);
        }