/// <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 Dispose_willDisposeTheRole()
        {
            // arrange:
            RoleBase role = MockRepository.GenerateStrictMock <RoleBase>();

            role.Expect(r => r.Dispose()).Repeat.Once();
            RoleRef roleRef = new RoleRef(role);

            // assert:
            roleRef.Dispose();
        }
示例#3
0
		public void Dispose_willDisposeTheRole()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			role.Expect(r => r.Dispose()).Repeat.Once();
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			roleRef.Dispose();
		}