Exemplo n.º 1
0
        /// <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;
        }
Exemplo n.º 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(); });
        }
Exemplo n.º 3
0
		public void Ctor_withRole_works()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			
			// act:
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			Assert.AreSame(role, roleRef.Role);
		}
Exemplo n.º 4
0
        public void Ctor_withRole_works()
        {
            // arrange:
            RoleBase role = MockRepository.GenerateStrictMock <RoleBase>();

            // act:
            RoleRef roleRef = new RoleRef(role);

            // assert:
            Assert.AreSame(role, roleRef.Role);
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
0
		public void Increase_increaseTheNumberOfReferences()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			for(int i = 0; i < 100; ++i)
			{
				Assert.AreEqual(i+1, roleRef.Increase());
			}
		}
Exemplo n.º 7
0
        public void Increase_increaseTheNumberOfReferences()
        {
            // arrange:
            RoleBase role    = MockRepository.GenerateStrictMock <RoleBase>();
            RoleRef  roleRef = new RoleRef(role);

            // assert:
            for (int i = 0; i < 100; ++i)
            {
                Assert.AreEqual(i + 1, roleRef.Increase());
            }
        }
Exemplo n.º 8
0
        public void Serialize_works()
        {
            // arrange:
            RoleBase role    = new FakeRole();
            RoleRef  roleRef = new RoleRef(role);
            int      numRef  = 100;

            for (int i = 0; i < numRef; ++i)
            {
                roleRef.Increase();
            }

            // act:
            Stream stream = TestUtilities.Serialize(roleRef);

            // assert:
            Assert.IsNotNull(stream);
        }
Exemplo n.º 9
0
        public void Deserialize_works()
        {
            // arrange:
            RoleBase role    = new FakeRole();
            RoleRef  roleRef = new RoleRef(role);
            int      numRef  = 100;

            for (int i = 0; i < numRef; ++i)
            {
                roleRef.Increase();
            }
            Stream stream = TestUtilities.Serialize(roleRef);

            // act:
            RoleRef deserialized = TestUtilities.Deserialize <RoleRef>(stream);
            int     nextRef      = deserialized.Increase();

            // assert:
            Assert.AreEqual(numRef + 1, nextRef);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Achieve the specified role.
        /// </summary>
        /// <param name='role'>
        /// User's role, entry point to a specific context boundary in the domain.
        /// </param>
        /// <remarks>
        /// <para>
        /// This method call a the <see cref="WorkingSessionBase.IsAllowed{TRole}"/> protected method.
        /// It will throw <see cref="InvalidOperationException"/> as far as the method returns <value>false</value>.
        /// </para>
        /// <para>
        /// If <typeparamref name="TRole"/> is allowed to the working session's owner, <see cref="WorkingSessionBase.GetRoleBuilder{TRole}()"/>
        /// will provide the <see cref="RoleBuilder{TRole}"/> that will build the role for the owner.
        /// </para>
        /// <para>
        /// Once a role is achieved, all subsequent calls to this method will return the previous instance
        /// without any futher call to either <see cref="WorkingSessionBase.IsAllowed{TRole}"/> or
        /// <see cref="WorkingSessionBase.GetRoleBuilder{TRole}()"/>.
        /// </para>
        /// </remarks>
        /// <typeparam name='TRole'>
        /// The type of the role to achieve.
        /// </typeparam>
        /// <exception cref="InvalidOperationException">The <see cref="IWorkingSession.Owner"/> can not achieve
        /// the required <typeparamref name="TRole"/>.</exception>
        public void Achieve <TRole> (out TRole role) where TRole : class
        {
            ThrowAfterDisposition();
            Type    roleType = typeof(TRole);
            RoleRef roleRef  = null;

            if (!_roles.TryGetValue(roleType, out roleRef))
            {
                if (!IsAllowed <TRole>())
                {
                    string message = string.Format("The owner of the working session ({0}) can not achieve the {1} role.", ((IWorkingSession)this).Owner, roleType.FullName);
                    throw new InvalidOperationException(message);
                }
                TRole newRole = Build <TRole>();
                roleRef          = new RoleRef(newRole as RoleBase);
                _roles[roleType] = roleRef;
            }
            roleRef.Increase();
            role = roleRef.Role as TRole;
        }
Exemplo n.º 11
0
		public void Decrease_underZero_throwsInvalidOperationException()
		{
			// arrange:
			RoleBase role = MockRepository.GenerateStrictMock<RoleBase>();
			RoleRef roleRef = new RoleRef(role);
			
			// assert:
			Assert.Throws<InvalidOperationException>(delegate{ roleRef.Decrease(); });
		}
Exemplo n.º 12
0
		public void Deserialize_works()
		{
			// arrange:
			RoleBase role = new FakeRole();
			RoleRef roleRef = new RoleRef(role);
			int numRef = 100;
			for(int i = 0; i < numRef; ++i)
				roleRef.Increase();
			Stream stream = TestUtilities.Serialize(roleRef);
			
			// act:
			RoleRef deserialized = TestUtilities.Deserialize<RoleRef>(stream);
			int nextRef = deserialized.Increase();
			
			// assert:
			Assert.AreEqual(numRef + 1, nextRef);
		}
Exemplo n.º 13
0
		public void Serialize_works()
		{
			// arrange:
			RoleBase role = new FakeRole();
			RoleRef roleRef = new RoleRef(role);
			int numRef = 100;
			for(int i = 0; i < numRef; ++i)
				roleRef.Increase();
			
			// act:
			Stream stream = TestUtilities.Serialize(roleRef);
			
			// assert:
			Assert.IsNotNull(stream);
		}
Exemplo n.º 14
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();
		}