/// <summary>
        /// Leave the specified role.
        /// After calling this method, your role will be disposed
        /// and the reference to <paramref name="role"/> will be set to <value>null</value>.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The role will be disposed when the number of call to <see cref="Achieve{TRole}(TRole)"/>
        /// will be balanced with the calls to this method.
        /// </para>
        /// <para>
        /// In other world you must leave the achieved roles exactly the same number of times you achieved each one.
        /// </para>
        /// </remarks>
        /// <param name='role'>
        /// User's role to be disposed.
        /// </param>
        /// <typeparam name='TRole'>
        /// The type of the role to leave.
        /// </typeparam>
        /// <exception cref="ArgumentNullException">The <paramref name="role"/> is <value>null</value>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="role"/> is unknown to the session.</exception>
        public void Leave <TRole> (ref TRole role) where TRole : class
        {
            ThrowAfterDisposition();
            if (null == role)
            {
                throw new ArgumentNullException("role");
            }
            Type    roleType = typeof(TRole);
            RoleRef roleRef  = null;

            if (!_roles.TryGetValue(roleType, out roleRef))
            {
                string message = string.Format("Unknown role type {0}.", roleType.FullName);
                throw new ArgumentException(message, "role");
            }
            if (!object.ReferenceEquals(role, roleRef.Role))
            {
                string message = string.Format("Unknown role {0}.", roleType.FullName);
                throw new ArgumentException(message, "role");
            }
            if (roleRef.Decrease() == 0)
            {
                _roles.Remove(roleType);
                roleRef.Dispose();
            }
            role = null;
        }
示例#2
0
        public void Decrease_underZero_throwsInvalidOperationException()
        {
            // arrange:
            RoleBase role    = MockRepository.GenerateStrictMock <RoleBase>();
            RoleRef  roleRef = new RoleRef(role);

            // assert:
            Assert.Throws <InvalidOperationException>(delegate { roleRef.Decrease(); });
        }
示例#3
0
        public void Decrease_decreaseTheNumberOfReferences()
        {
            // arrange:
            RoleBase role    = MockRepository.GenerateStrictMock <RoleBase>();
            RoleRef  roleRef = new RoleRef(role);

            // assert:
            for (int i = 0; i < 100; ++i)
            {
                roleRef.Increase();
                Assert.AreEqual(0, roleRef.Decrease());
            }
            for (int i = 0; i < 100; ++i)
            {
                roleRef.Increase();
            }
            for (int i = 0; i < 100; ++i)
            {
                Assert.AreEqual(100 - (i + 1), roleRef.Decrease());
            }
        }
示例#4
0
		public void Decrease_decreaseTheNumberOfReferences()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			for(int i = 0; i < 100; ++i)
			{
				roleRef.Increase();
				Assert.AreEqual(0, roleRef.Decrease());
			}
			for(int i = 0; i < 100; ++i)
				roleRef.Increase();
			for(int i = 0; i < 100; ++i)
			{
				Assert.AreEqual(100 - (i+1), roleRef.Decrease());
			}
		}
示例#5
0
		public void Decrease_underZero_throwsInvalidOperationException()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			Assert.Throws<InvalidOperationException>(delegate{ roleRef.Decrease(); });
		}