Add() public méthode

Registers a service associated to a callback (and an optional callback that will be called when the service will be removed). The serviceInstance is called as long as no service has been obtained (serviceInstance returns null). Once the actual service has been obtained, it is kept and serviceInstance is not called anymore.
public Add ( Type serviceType, Func serviceInstance, Action onRemove ) : ISimpleServiceContainer
serviceType System.Type Service type to register. It must not already exist in this container otherwise an exception is thrown.
serviceInstance Func Delegate to call when needed. Can not be null.
onRemove Action Optional action that will be called whenever , or /// is called and a service as been successfully obtained.
Résultat ISimpleServiceContainer
Exemple #1
0
        MiniContext( string name )
        {
            ServiceContainer = new SimpleServiceContainer();
            ServiceContainer.Add( RequirementLayerSerializer.Instance );
            ServiceContainer.Add( SimpleTypeFinder.Default );

            ConfigContainer = SharedDictionary.Create( ServiceContainer );
            ConfigManager = ConfigurationManager.Create( ConfigContainer ).ConfigManager;

        }
        static SharedDicTestContext()
        {
            _allPlugins = new List<INamedVersionedUniqueId>();
            _allPluginsEx = new ReadOnlyListOnIList<INamedVersionedUniqueId>( _allPlugins );
            EnsurePlugins( 10 );

            var c = new SimpleServiceContainer();
            c.Add<ISimpleTypeFinder>( SimpleTypeFinder.Default );
            _serviceProvider = c;
        }
Exemple #3
0
        MiniContext( string name )
        {
            ServiceContainer = new SimpleServiceContainer();

            ContextObject = new object();
            ConfigContainer = SharedDictionary.Create( ServiceContainer );
            ConfigManager = ConfigurationManager.Create( ConfigContainer ).ConfigManager;
            PluginRunner = new PluginRunner( ServiceContainer, ConfigManager );
            PluginRunner.Initialize( ContextObject );
            ServiceContainer.Add<IConfigContainer>( ConfigContainer );
        }
        public void EmptyFile()
        {
            string test = TestBase.GetTestFilePath( "Storage", "EmptyFile" );
            using( Stream wrt = new FileStream( test, FileMode.Create ) )
            {
                IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() );
                writer.Dispose();
            }

            Assert.That( File.Exists( test ) );

            using( Stream str = new FileStream( test, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.StorageVersion, Is.GreaterThanOrEqualTo( new Version( 2, 5, 0 ) ) );
                }
            }
        }
 public void BugUnexisting()
 {
     string xmlPath = TestBase.GetTestFilePath( "Storage", "BugUnexistingEnum" );
     using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", UnexistingTestEnumValues.First );
             writer.WriteObjectElement( "After", 3712 * 2 );
             writer.WriteObjectElement( "data", new MayBeUnexistingButValidXmlObject() );
             writer.WriteObjectElement( "After2", 3712 * 3 );
         }
     }
     TestBase.DumpFileToConsole( xmlPath );
     using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add<ISimpleTypeFinder>( new UnexistingTypeFinder() );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             Assert.Throws<TypeLoadException>( () => reader.ReadObjectElement( "data" ) );
             // An exception does not break the reader.
             Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
             Assert.Throws<TypeLoadException>( () => reader.ReadObjectElement( "data" ) );
             // An exception does not break the reader.
             Assert.That( reader.ReadObjectElement( "After2" ), Is.EqualTo( 3712 * 3 ) );
         }
     }
 }
        public void StructuredSerializedObjectTest()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "FakeStructuredSerializedObject" );
            StructuredSerializableObject original = new StructuredSerializableObject() { OneInteger = 43, OneString = "Let's go..." };
            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.ObjectWriteExData += ( s, e ) =>
                        {
                            if( e.Obj == original )
                            {
                                e.Writer.Xml.WriteStartElement( "ExtraData" );
                                e.Writer.Xml.WriteAttributeString( "OneAtrribute", "23" );
                                e.Writer.Xml.WriteElementString( "SubValue", "string in element..." );
                                e.Writer.Xml.WriteEndElement();
                            }
                        };
                    writer.WriteObjectElement( "data", original );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            TestBase.DumpFileToConsole( xmlPath );
            // Reads without reading ExtraData element.
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    object read = reader.ReadObjectElement( "data" );
                    Assert.That( read, Is.TypeOf( typeof( StructuredSerializableObject ) ) );
                    StructuredSerializableObject newOne = read as StructuredSerializableObject;
                    Assert.That( newOne.OneString, Is.EqualTo( original.OneString ) );
                    Assert.That( newOne.OneInteger, Is.EqualTo( original.OneInteger ) );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
            // Reads ExtraData element.
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    reader.ObjectReadExData += ( source, e ) =>
                        {
                            Assert.That( e.Reader.Xml.IsStartElement( "ExtraData" ) );
                            Assert.That( e.Reader.Xml.GetAttributeInt( "OneAtrribute", -12 ), Is.EqualTo( 23 ) );
                            e.Reader.Xml.Read();
                            Assert.That( e.Reader.Xml.ReadElementContentAsString(), Is.EqualTo( "string in element..." ) );
                            // Forget to read the end element.
                            Assert.That( e.Reader.Xml.NodeType == XmlNodeType.EndElement );
                        };

                    object read = reader.ReadObjectElement( "data" );
                    Assert.That( read, Is.TypeOf( typeof( StructuredSerializableObject ) ) );
                    StructuredSerializableObject newOne = read as StructuredSerializableObject;
                    Assert.That( newOne.OneString, Is.EqualTo( original.OneString ) );
                    Assert.That( newOne.OneInteger, Is.EqualTo( original.OneInteger ) );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
        public void BugBinarySizeDiffer()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "BugBinarySizeDiffer" );
            SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 };
            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "data", original );
                }
            }
            LoadAndModifyXml( xmlPath, d => d.Root.Element( "data" ).Attribute( "size" ).SetValue( "1" ) );

            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.Throws<CKException>( () => reader.ReadObjectElement( "data" ) );
                }
            }
        }
 public void BugBinaryTooBigContent()
 {
     string xmlPath = TestBase.GetTestFilePath( "Storage", "BugBinaryTooBigContent" );
     SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 };
     using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", original );
         }
     }
     LoadAndModifyXml( xmlPath, d =>
     {
         var e = d.Root.Element( "data" );
         e.SetValue( e.Value.Insert( e.Value.Length / 2, "00FF00FF" ) );
     } );
     using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             object obj = reader.ReadObjectElement( "data" );
         }
     }
 }
 private void DoTestEnum( Action<XDocument> docModifier )
 {
     string test = TestBase.GetTestFilePath( "Storage", "TestEnum" );
     using( Stream wrt = new FileStream( test, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", TestEnumValues.First );
             writer.WriteObjectElement( "data", TestEnumValues.Second );
             writer.WriteObjectElement( "After", 3712 * 2 );
         }
     }
     LoadAndModifyXml( test, docModifier );
     using( Stream str = new FileStream( test, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             TestEnumValues value1 = (TestEnumValues)reader.ReadObjectElement( "data" );
             TestEnumValues value2 = (TestEnumValues)reader.ReadObjectElement( "data" );
             Assert.That( value1, Is.EqualTo( TestEnumValues.First ) );
             Assert.That( value2, Is.EqualTo( TestEnumValues.Second ) );
             Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
         }
     }
 }
        public void ArrayListWithSerializableObjects()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestGenericListOfString" );
            ArrayList list = new ArrayList();
            SerializableObject firstObject = new SerializableObject() { Name = "Albert", Power = 34 };
            list.Add( firstObject );
            list.Add( new DateTime( 2009, 01, 11 ) );
            list.Add( "Franchement, les mecs, vous trouvez que c'est normal que ce soit Spi qui se cogne tous les tests unitaires ?" );

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "data", list );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    ArrayList list2 = (ArrayList)reader.ReadObjectElement( "data" );
                    Assert.AreEqual( ((SerializableObject)list2[0]).Name, ((SerializableObject)list[0]).Name );
                    Assert.AreEqual( ((SerializableObject)list2[0]).Power, ((SerializableObject)list[0]).Power );
                    CheckExactTypeAndValue( typeof( DateTime ), list[1], list2[1] );
                    CheckExactTypeAndValue( typeof( string ), list[2], list2[2] );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
        public void GenericListOfString()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestGenericListOfString" );
            List<string> list = new List<string>();
            list.Add( "content1" );
            list.Add( "content2" );
            list.Add( "content3" );

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "Before", 3712 );
                    writer.WriteObjectElement( "data", list );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.ReadObjectElement( "Before" ), Is.EqualTo( 3712 ) );
                    CheckExactTypeAndValue( typeof( List<string> ), list, reader.ReadObjectElement( "data" ) );
                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
        public void BinarySerializableObject()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestBinarySerializableObject" );
            SerializableObject o = new SerializableObject();
            o.Name = "TestName";
            o.Power = 20;

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "Before", 3712 );
                    writer.WriteObjectElement( "data", o );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.ReadObjectElement( "Before" ), Is.EqualTo( 3712 ) );
                    
                    SerializableObject o2 = (SerializableObject)reader.ReadObjectElement( "data" );
                    Assert.AreEqual( o.Name, o2.Name );
                    Assert.AreEqual( o.Power, o2.Power );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }