コード例 #1
0
        private async Task <IActionResult> Edit(int id, [Bind("Id,Propery1,Propery2,Propery3,Propery4")] Thing2 thing2)
        {
            if (id != thing2.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(thing2);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!Thing2Exists(thing2.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(thing2));
        }
コード例 #2
0
        public void InheritedMap()
        {
            var definitions = new MapDefinitionCollection(new IMapDefinition[]
            {
                new MapperDefinition1(),
            });
            var mapper = new UmbracoMapper(definitions);

            var thing3 = new Thing3 {
                Value = "value"
            };
            var thing2 = mapper.Map <Thing3, Thing2>(thing3);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = mapper.Map <Thing2>(thing3);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = new Thing2();
            mapper.Map(thing3, thing2);
            Assert.AreEqual("value", thing2.Value);
        }
コード例 #3
0
        public void SimpleMap()
        {
            var definitions = new MapDefinitionCollection(() => new IMapDefinition[]
            {
                new MapperDefinition1(),
            });
            var mapper = new UmbracoMapper(definitions, _scopeProvider);

            var thing1 = new Thing1 {
                Value = "value"
            };
            Thing2 thing2 = mapper.Map <Thing1, Thing2>(thing1);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = mapper.Map <Thing2>(thing1);

            Assert.IsNotNull(thing2);
            Assert.AreEqual("value", thing2.Value);

            thing2 = new Thing2();
            mapper.Map(thing1, thing2);
            Assert.AreEqual("value", thing2.Value);
        }
コード例 #4
0
    static void Main()
    {
        var x = new Thing();
        var y = new Thing2();

        new List <IThing <IEnumerable <int> > > {
            x, y
        };
    }
コード例 #5
0
        public async Task <IActionResult> Create([Bind("Id,Propery1,Propery2,Propery3,Propery4")] Thing2 thing2)
        {
            if (ModelState.IsValid)
            {
                _context.Add(thing2);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(thing2));
        }
コード例 #6
0
        public void WriteEntitiesToDb()
        {
            var thing2s = m_Accessor.ReadEntities <Thing2>("SELECT * FROM [Thing2]")
                          .ToList();

            thing2s[0].Value = "one";
            thing2s[2].Entity.Delete();

            var gnu2 = new Thing2()
            {
                Thing1Id = 1, Name = "New Thing 2", Value = "Priceless"
            };

            thing2s.Add(gnu2);

            m_Accessor.WriteEntities(thing2s);

            //Console.WriteLine("Gnu2 Id = '{0}'", gnu2.Id);
        }
コード例 #7
0
        public void TestAbstractThingSetObjectSerialization()
        {
            SetTestPatternBuffer   patternBuffer = new SetTestPatternBuffer();
            AbstractThing          t1            = new Thing1(1, 2);
            AbstractThing          t2            = new Thing2(3, 4);
            AbstractThingSetObject o1            = new AbstractThingSetObject(new HashSet <AbstractThing>()
            {
                t1, t2
            });

            byte[] bytes = patternBuffer.Energize(o1);
            // 1 byte       AbstractThingSetObject type ID
            // 1 byte       null bits
            // 1 byte       set count
            // 1 byte       Thing1 type ID
            // 8 bytes      Thing1 2 invariant ints
            // 1 byte       Thing2 type ID
            // 8 bytes      Thing2 2 invariant ints
            Assert.That(bytes.Length, Is.EqualTo(21));
            object o2 = patternBuffer.Energize(bytes);
            AbstractThingSetObject p1 = (AbstractThingSetObject)o2;

            Assert.That(p1 is AbstractThingSetObject, Is.True);
            Assert.That(p1.AbstractThingSetValue.Count, Is.EqualTo(2));
            HashSet <int> expected = new HashSet <int>()
            {
                1, 3
            };

            foreach (AbstractThing t in p1.AbstractThingSetValue)
            {
                Console.WriteLine(t.IntValue1);
                expected.Remove(t.IntValue1);
            }
            Assert.That(expected.Count, Is.EqualTo(0));
            Assert.That(o1 == p1, Is.False);
        }
コード例 #8
0
        public void TestAbstractThingListObjectSerialization()
        {
            ListTestPatternBuffer   patternBuffer = new ListTestPatternBuffer();
            AbstractThing           t1            = new Thing1(1, 2);
            AbstractThing           t2            = new Thing2(3, 4);
            AbstractThingListObject o1            = new AbstractThingListObject(new List <AbstractThing>()
            {
                t1, t2
            });

            byte[] bytes = patternBuffer.Energize(o1);
            // 1 byte       AbstractThingListObject type ID
            // 1 byte       null bits
            // 1 byte       list count
            // 1 byte       Thing1 type ID
            // 8 bytes      Thing1 2 invariant ints
            // 1 byte       Thing2 type ID
            // 8 bytes      Thing2 2 invariant ints
            Assert.AreEqual(21, bytes.Length);
            object o2 = patternBuffer.Energize(bytes);
            AbstractThingListObject p1 = (AbstractThingListObject)o2;

            Assert.IsTrue(p1 is AbstractThingListObject);
            Assert.AreEqual(2, p1.AbstractThingListValue.Count);
            HashSet <int> expected = new HashSet <int>()
            {
                1, 3
            };

            foreach (AbstractThing t in p1.AbstractThingListValue)
            {
                Console.WriteLine(t.IntValue1);
                expected.Remove(t.IntValue1);
            }
            Assert.AreEqual(0, expected.Count);
            Assert.IsFalse(o1 == p1);
        }
コード例 #9
0
 public Thing3(Thing2 thing)
 {
 }
コード例 #10
0
        public async Task GetRequiredService_UsesSingletonAndLazyLocks_NoDeadlock()
        {
            using (var mreForThread1 = new ManualResetEvent(false))
                using (var mreForThread2 = new ManualResetEvent(false))
                {
                    // Thread 1: Thing1 (transient) -> Thing0 (singleton)
                    // Thread 2: Thing2 (singleton) -> Thing1 (transient) -> Thing0 (singleton)

                    // 1. Thread 1 resolves the Thing1 which is a transient service
                    // 2. In parallel, Thread 2 resolves Thing2 which is a singleton
                    // 3. Thread 1 enters the factory callback for Thing1 and takes the lazy lock
                    // 4. Thread 2 takes callsite for Thing2 as a singleton lock when it resolves Thing2
                    // 5. Thread 2 enters the factory callback for Thing1 and waits on the lazy lock
                    // 6. Thread 1 calls GetRequiredService<Thing0> on the service provider, takes callsite for Thing0 causing no deadlock
                    // (rather than taking the locks that are already taken - either the lazy lock or the Thing2 callsite lock)

                    Thing0           thing0 = null;
                    Thing1           thing1 = null;
                    Thing2           thing2 = null;
                    IServiceProvider sp     = null;
                    var sb = new StringBuilder();

                    // Arrange
                    var services = new ServiceCollection();

                    var lazy = new Lazy <Thing1>(() =>
                    {
                        sb.Append("3");
                        mreForThread2.Set(); // Now that thread 1 holds lazy lock, allow thread 2 to continue

                        // by this time, Thread 2 is holding a singleton lock for Thing2,
                        // and Thread one holds the lazy lock
                        // the call below to resolve Thing0 does not hang
                        // since singletons do not share the same lock upon resolve anymore.
                        thing0 = sp.GetRequiredService <Thing0>();
                        return(new Thing1(thing0));
                    });

                    services.AddSingleton <Thing0>();
                    services.AddTransient(sp =>
                    {
                        if (ThreadId == 2)
                        {
                            sb.Append("1");
                            mreForThread1.Set();     // [b] Allow thread 1 to continue execution and take the lazy lock
                            mreForThread2.WaitOne(); // [c] Wait until thread 1 takes the lazy lock

                            sb.Append("4");
                        }

                        // Let Thread 1 over take Thread 2
                        Thing1 value = lazy.Value;
                        return(value);
                    });
                    services.AddSingleton <Thing2>();

                    sp = services.BuildServiceProvider();

                    var t1 = Task.Run(() =>
                    {
                        ThreadId         = 1;
                        using var scope1 = sp.CreateScope();
                        mreForThread1.WaitOne(); // [a] Waits until thread 2 reaches the transient call to ensure it holds Thing2 singleton lock

                        sb.Append("2");
                        thing1 = scope1.ServiceProvider.GetRequiredService <Thing1>();
                    });

                    var t2 = Task.Run(() =>
                    {
                        ThreadId         = 2;
                        using var scope2 = sp.CreateScope();
                        thing2           = scope2.ServiceProvider.GetRequiredService <Thing2>();
                    });

                    // Act
                    await t1;
                    await t2;

                    // Assert
                    Assert.NotNull(thing0);
                    Assert.NotNull(thing1);
                    Assert.NotNull(thing2);
                    Assert.Equal("1234", sb.ToString()); // Expected order of execution
                }
        }
コード例 #11
0
 private void Map1(Thing1 source, Thing2 target, MapperContext context) =>
 target.Value = source.Value;
コード例 #12
0
        public void ConcurrentMap()
        {
            var definitions = new MapDefinitionCollection(() => new IMapDefinition[]
            {
                new MapperDefinition1(),
                new MapperDefinition3(),
            });
            var mapper = new UmbracoMapper(definitions, _scopeProvider);

            // the mapper currently has a map from Thing1 to Thing2
            // because Thing3 inherits from Thing1, it will map a Thing3 instance,
            // and register a new map from Thing3 to Thing2,
            // thus modifying its internal dictionaries

            // if timing is good, and mapper does have non-concurrent dictionaries, it fails
            // practically, to reproduce, one needs to add a 1s sleep in the mapper's loop
            // hence, this test is explicit
            var thing3 = new Thing3 {
                Value = "value"
            };
            var       thing4 = new Thing4();
            Exception caught = null;

            void ThreadLoop()
            {
                // keep failing at mapping - and looping through the maps
                for (var i = 0; i < 10; i++)
                {
                    try
                    {
                        mapper.Map <Thing2>(thing4);
                    }
                    catch (Exception e)
                    {
                        caught = e;
                        Console.WriteLine($"{e.GetType().Name} {e.Message}");
                    }
                }

                Console.WriteLine("done");
            }

            var thread = new Thread(ThreadLoop);

            thread.Start();
            Thread.Sleep(1000);

            try
            {
                Console.WriteLine($"{DateTime.Now:O} mapping");
                Thing2 thing2 = mapper.Map <Thing2>(thing3);
                Console.WriteLine($"{DateTime.Now:O} mapped");

                Assert.IsNotNull(thing2);
                Assert.AreEqual("value", thing2.Value);
            }
            finally
            {
                thread.Join();
            }
        }
コード例 #13
0
 public static void Main(string[] args)
 {
     var thing1 = new Thing1();
     var thing2 = new Thing2();
 }