public void Create_With_Indexed_And_Dynamic_Syntaxes()
		{
			DeepObject obj = new DeepObject();
			obj["FirstName"] = "James";
			obj["LastName"] = "Bond";
			ConsoleEx.WriteLine("\n> {0}", obj);
			Assert.AreEqual(
				"{[{FirstName='James'}, {LastName='Bond'}]}"
				, obj.ToString());

			dynamic d = obj;
			d.Age = 50;
			d.Roles[0] = "Spy";
			d.Roles[1] = "Lover";
			d.Books["1965", 5] = "Thunderball";
			d.Gear.Gun = true;
			d.Gear.Knife = false;
			ConsoleEx.WriteLine("\n> {0}", obj);
			Assert.AreEqual(
				"{[" + "{FirstName='James'}, {LastName='Bond'}, {Age='50'}, " +
				"{Roles [{[0]='Spy'}, {[1]='Lover'}]}, " +
				"{Books [{[1965, 5]='Thunderball'}]}, " +
				"{Gear [{Gun='True'}, {Knife='False'}]}" + "]}"
				, obj.ToString());
		}
Esempio n. 2
0
		static void Main( string[] args )
		{
			TextWriterTraceListener tr = new TextWriterTraceListener( System.Console.Out );
			Debug.Listeners.Add( tr ); Debug.AutoFlush = true; Debug.IndentSize = 3;
			ConsoleHelper.SetWindowPosition( 1, 10 );

			Console.WriteLine( "\n===== Press [Enter] to start the program..." ); Console.ReadLine();

			Console.WriteLine( "\n===== Registering types for the WCF machinery ..." );
			KTypesWCF.AddType( typeof( CalendarDate ) );
			KTypesWCF.AddType( typeof( ClockTime ) );

			Console.WriteLine( "\n===== Creating the Link object ..." );
			dynamic package = new DeepObject(); // Or use null if you don't need it
			package.Login = "******";
			package.Password = "******";
			package.NowDate = new CalendarDate( DateTime.Now );
			package.NowTime = new ClockTime( DateTime.Now );
			var endpoint = "Service_OnTcp_Endpoint"; // Use the endpoint on you App.config file
			var factory = new KFactorySQL(); // Use the appropriate factory
			var link = new KLinkWCF( factory, endpoint, package );

			Console.WriteLine( "\n== Executing your examples ..." );
			Test_Core.Dispatcher( link );

			Console.WriteLine( "\n== Press [Enter] to dispose the link ..." ); Console.ReadLine();
			link.Dispose();
			
			Console.WriteLine( "\n===== Press [Enter] to terminate program..." ); Console.ReadLine();
		}
Esempio n. 3
0
			internal void Destroy()
			{
				_Owner = null;
				_Host = null;
				_Value = null;
				if( _Members != null ) _Members.Clear(); _Members = null;
			}
		public void Dynamic_Adding()
		{
			var obj = new DeepObject();
			dynamic d = obj;

			d.DeepAdd("James"); Assert.AreEqual("{[{James}]}", obj.ToString());

			obj.DeepClear();
			d["Bond"] = "Spy"; Assert.AreEqual("{[{Bond='Spy'}]}", obj.ToString());
		}
		public void Null_Indexes()
		{
			var obj = new DeepObject();
			obj[null] = "Hello";
			Assert.AreEqual("{[{[]='Hello'}]}", obj.ToString());

			obj = new DeepObject();
			obj[null, null] = "Hello";
			Assert.AreEqual("{[{[, ]='Hello'}]}", obj.ToString());
		}
		public void DynamicSource_Read_And_Write()
		{
			DeepObject obj = new DeepObject();
			dynamic d = obj;
			d.Name.First = "James";
			d.Name.Last = "Bond";

			var name = DynamicInfo.Read(obj, x => x.Name.First);
			Assert.AreEqual(name, "James");

			DynamicInfo.Write(obj, x => x.Name.Last, "Smith");
			name = DynamicInfo.Read(obj, x => x.Name.Last);
			Assert.AreEqual(name, "Smith");
		}
Esempio n. 7
0
        public void Should_IncludeOnly_DeepPath()
        {
            var provider = new DiffProvider();

            var obj1 = new DeepObject();
            var obj2 = new DeepObject();

            obj2.DeepChildObject.Id = 999;
            obj2.DeepChildObject.DeepChild2Object.DeepChild3Object.Name = "Test";

            var diff = provider.ComputeDiff <DeepObject>(obj1, obj2, ComparisonOptions.All | ComparisonOptions.IncludeList, x => x.DeepChildObject.DeepChild2Object.DeepChild3Object.Name);

            Assert.AreEqual(1, diff.Count);
            Assert.AreEqual(".DeepChildObject.DeepChild2Object.DeepChild3Object.Name", diff.First().Path);
        }
		public void Removed_Not_Owned()
		{
			var obj = new DeepObject();
			dynamic d = obj;
			d.Name.First = "James";
			d.Name.Last = "Bond";
			ConsoleEx.WriteLine("\n> {0}", obj);

			var temp = new DeepObject();
			dynamic t = temp;
			t.Name.First = "James";

			bool r;

			var dyn = t.Name.DeepFind("First");
			r = obj.DeepRemove(dyn);
		}
Esempio n. 9
0
			/// <summary>
			/// Returns the index at which the given member is stored.
			/// </summary>
			/// <param name="member">The member to obtain its index for.</param>
			/// <returns>The index at which the given member is stored, or -1.</returns>
			public int IndexOf( DeepObject member )
			{
				if( member == null ) throw new ArgumentNullException( "member", "Member cannot be null." );
				if( _Members == null ) return -1;
				int r = _Members.IndexOf( member );
				return r;
			}
		public void DynamicSource_Not_Existent_Member()
		{
			try
			{
				DeepObject obj = new DeepObject();
				dynamic d = obj;
				d.Name.First = "James";
				d.Name.Last = "Bond";

				var item = DynamicInfo.Read(obj, x => x.Name.Whatever);
				Assert.Fail();
			}
			catch (NotFoundException) { }
		}
		public void DynamicSource_Intermediate_Member_Is_Null_Shall_Fail()
		{
			try
			{
				DeepObject obj = new DeepObject();
				dynamic d = obj;
				d.Name.First = "James";
				d.Name.Last = "Bond";

				d.Name = null;
				var item = DynamicInfo.Read(obj, x => x.Name.First);
				Assert.Fail();
			}
			catch (EmptyException) { }
		}
Esempio n. 12
0
		/// <summary>
		/// Creates a new	<see cref="KLinkWCF"/> for the personality of the database defined by the factory argument, and
		/// connects to the service located by the endpoint argument using the connection package given.
		/// </summary>
		/// <param name="factory">The factory defining the personality of the database.</param>
		/// <param name="endpoint">The endpoint to use to locate the WCF service.</param>
		/// <param name="package">The connection package to use by the service to modulate how it will connect with the real
		/// underlying database, if needed, or null if it is not needed.</param>
		public KLinkWCF( IKFactory factory, string endpoint, DeepObject package = null ) : base( factory )
		{
			DEBUG.IndentLine( "\n-- KLinkWCF( Factory, EndPoint={0}, Package={1} )", endpoint ?? "<null>", package == null ? "<null>" : package.ToString() );
			ProxyConnect( endpoint, package );
			DEBUG.Unindent();
		}
Esempio n. 13
0
		/// <summary>
		/// Disconnects this proxy from the client-side, and clears all the resources it holds.
		/// </summary>
		public virtual void ProxyDisconnect()
		{
			DEBUG.IndentLine( "\n-- [{0}] KServerWCF.ProxyDisconnect()", _ProxyId.TagString() );
			try {
				if( _Elements != null ) {
					foreach( var kvp in _Elements ) { if( kvp.Value != null && kvp.Value is IDisposable ) ( (IDisposable)kvp.Value ).Dispose(); }
					_Elements.Clear();
				}
				if( _Link != null ) { _Link.DbClose(); _Link = null; }
				_Package = null;
			}
			catch( Exception e ) { DEBUG.PrintException( e ); throw; }
			finally { DEBUG.Unindent(); }
		}
Esempio n. 14
0
			/// <summary>
			/// Creates and add a new indexed member using the indexes given.
			/// </summary>
			/// <param name="indexes">The indexes to use to create and add the new member.</param>
			/// <returns>The newly created and added member.</returns>
			public DeepObject AddMember( object[] indexes )
			{
				if( _Sealed ) throw new InvalidOperationException( "This object is sealed." );
				if( indexes == null ) throw new ArgumentNullException( "Indexes cannot be a null array." );
				if( indexes.Length == 0 ) throw new ArgumentException( "Indexes cannot be an empty array." );

				if( _Members == null ) _Members = new List<DeepObject>();

				var member = FindMember( indexes, raise: false );
				if( member != null ) throw new InvalidOperationException( "A member already exists with the same index: " + TypeHelper.ToString( indexes, "[]" ) );

				string name = TypeHelper.ToString( indexes, "[]" );

				member = new DeepObject(); // to avoid name validation
				member.Deep._CaseSensitiveNames = _CaseSensitiveNames;
				member.Deep._Name = name;
				member.Deep._IsIndex = true;
				member.Deep._Host = this._Owner; _Members.Add( member );
				return member;
			}
Esempio n. 15
0
			internal DeepCarrier( DeepObject owner )
			{
				_Owner = owner;
			}
Esempio n. 16
0
			/// <summary>
			/// Removes the given member from this DeepObject.
			/// </summary>
			/// <param name="member">The member to remove.</param>
			/// <returns>True if the member has been removed, false otherwise.</returns>
			public bool RemoveMember( DeepObject member )
			{
				if( _Sealed ) throw new InvalidOperationException( "This object is sealed." );
				if( member == null ) throw new ArgumentNullException( "member", "Member to remove cannot be null." );
				bool r = false;

				if( _Members != null ) {
					r = _Members.Remove( member ); if( r ) member.Deep._Host = null;
					if( _Members.Count == 0 ) _Members = null;
				}
				return r;
			}
Esempio n. 17
0
 /// <summary>
 /// Clones this <see cref="DeepObject"/> into a new instance of the given type.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="original"></param>
 /// <returns></returns>
 public static T CloneAs <T>(this DeepObject original) where T : DeepObject
 {
     return(original.clone() as T);
 }
		public void Serialization()
		{
			Action<object, bool> del = (source, binary) =>
			{
				var mode = binary ? "binary" : "text/soap";
				ConsoleEx.WriteLine("\n- Source ({0}): {1}", mode, source.Sketch());

				var path = "c:\\temp\\data"; path += binary ? ".bin" : ".xml";
				path.PathSerialize(source, binary);

				var temp = path.PathDeserialize(binary);
				ConsoleEx.WriteLine("- Created ({0}): {1}", mode, temp.Sketch());

				var result = source.IsEquivalentTo(temp);
				Assert.IsTrue(result,
					"With mode '{0}' source '{1}' and deserialized '{2}' are not equivalent."
					.FormatWith(mode, source.Sketch(), temp.Sketch()));
			};

			var obj = new DeepObject();
			dynamic d = obj;
			d.Name.First = "James";
			d.Name.Last = "Bond";

			del(obj, true);
			del(obj, false);
		}
		public void Cloning()
		{
			Action<object> del = (source) =>
			{
				ConsoleEx.WriteLine("\n- Source: {0}", source.Sketch());

				var temp = ((ICloneable)source).Clone();
				ConsoleEx.WriteLine("- Cloned: {0}", temp.Sketch());

				Assert.IsTrue(source.IsEquivalentTo(temp),
					"Source '{0}' and cloned '{1}' are not equivalent."
					.FormatWith(source, temp));
			};

			var obj = new DeepObject();
			dynamic d = obj;
			d.Name.First = "James";
			d.Name.Last = "Bond";

			del(obj);
		}
Esempio n. 20
0
			/// <summary>
			/// Adds the given DeepObject into the collection of members of this instance.
			/// </summary>
			/// <param name="deep">The DeepObject member to add.</param>
			public void AddMember( DeepObject deep )
			{
				if( _Sealed ) throw new InvalidOperationException( "This object is sealed." );
				if( deep == null ) throw new ArgumentNullException( "deep", "Member to add cannot be null." );
				if( deep.Deep._Host != null ) throw new InvalidOperationException( "Member to add is already a member in other host." );
				deep.Deep._Name.Validated( "Deep Member Name" );

				if( _Members == null ) _Members = new List<DeepObject>();

				var member = FindMember( deep.Deep._Name, raise: false );
				if( member != null ) throw new InvalidOperationException( "A member already exists with the same name: " + deep.Deep._Name );

				_Members.Add( deep ); deep.Deep._Host = this._Owner;
			}
		public void Dispose()
		{
			var obj = new DeepObject();
			dynamic d = obj;
			d.Name.First = "James";
			d.Name.Last = "Bond";

			obj.Dispose();
			ConsoleEx.WriteLine("\n- Disposed: {0}", obj);
			Assert.IsTrue(obj.IsDisposed);
			Assert.AreEqual(0, obj.DeepCount());

			obj.Dispose();
			ConsoleEx.WriteLine("- Disposed Twice: {0}", obj);
		}
Esempio n. 22
0
			/// <summary>
			/// Creates and adds a new member with the name given.
			/// </summary>
			/// <param name="name">The name of the new member to create and add.</param>
			/// <returns>The newly created member.</returns>
			public DeepObject AddMember( string name )
			{
				if( _Sealed ) throw new InvalidOperationException( "This object is sealed." );
				name = name.Validated( "Name", invalidChars: TypeHelper.InvalidNameChars );

				if( _Members == null ) _Members = new List<DeepObject>();

				var member = FindMember( name, raise: false );
				if( member != null ) throw new InvalidOperationException( "A member already exists with the same name: " + name );

				member = new DeepObject( name, _CaseSensitiveNames );
				member.Deep._Host = this._Owner; _Members.Add( member );
				return member;
			}
Esempio n. 23
0
		/// <summary>
		/// Connects this link object with the WCF service. If it has been already connected, an exceptio will be thrown.
		/// </summary>
		/// <param name="endpoint">The endpoint to use to locate the WCF service.</param>
		/// <param name="package">The connection package to use by the service to modulate how it will connect with the real
		/// underlying database, if needed, or null if it is not needed.</param>
		public virtual void ProxyConnect( string endpoint, DeepObject package )
		{
			DEBUG.IndentLine( "\n-- KLinkWCF.ProxyConnect( EndPoint={0}, Package={1}", endpoint ?? "<null>", package == null ? "<null>" : package.ToString() );

			if( _Proxy != null ) throw new InvalidOperationException( "This link is already connected." );
			_EndPoint = endpoint.Validated( "EndPoint" );
			_Package = package;

			var channelFactory = new ChannelFactory<IKProxyWCF>( _EndPoint );
			_Proxy = channelFactory.CreateChannel();
			_ProxyId = _Proxy.ProxyConnect( _Package );

			DEBUG.WriteLine( "\n-- Connected with UID = {0}", _ProxyId.TagString() );
			DEBUG.Unindent();
		}
Esempio n. 24
0
		/// <summary>
		/// Disconnects this link from the WCF service, freeing all the server resources it might have been used. If it was
		/// not connected, this method merely returns.
		/// </summary>
		public virtual void ProxyDisconnect()
		{
			DEBUG.IndentLine( "\n-- [{0}] KLinkWCF.ProxyDisconnect()", _ProxyId.TagString() );

			if( _Proxy != null ) {
				_Proxy.ProxyDisconnect();
				_Proxy.Dispose();
				_Proxy = null;
				_ProxyId = Guid.Empty;

				_Package = null;
				_EndPoint = null;
				_DbCaseSensitiveNames = null;
			}

			DEBUG.Unindent();
		}
Esempio n. 25
0
		/// <summary>
		/// Connects to the underlying database, creating the actual link object, using the optional connection package that
		/// might have been provided. This connection package is a dynamic object, an instance of <see cref="DeepObject"/>,
		/// able to carry an arbitrary number of properties and values as needed.
		/// <para>If this proxy is already connected with an underlying database, an exception will be thrown.</para>
		/// </summary>
		/// <param name="package">The connection package to use, or null.</param>
		/// <returns>The Guid of this proxy.</returns>
		public virtual Guid ProxyConnect( DeepObject package )
		{
			DEBUG.IndentLine( "\n-- [{0}] KServerWCF.ProxyConnect( Package={1} )", _ProxyId.TagString(), package == null ? "<null>" : package.ToString() );
			try {
				if( _Link != null ) throw new InvalidOperationException( "WCF Server is already connected." );

				_Package = package;
				_Link = CreateLink();
				if( _Link == null ) throw new InvalidOperationException( "Cannot create WCF Server's surrogate Link instance." );

				_Link.TransactionMode = GetDefaultTransactionMode();
				return _ProxyId;
			}
			catch( Exception e ) { DEBUG.PrintException( e ); throw; }
			finally { DEBUG.Unindent(); }
		}