public void named_registrations_should_be_isolated_on_child_containers() { var container = new Container(_ => { }); //Parent Container doesnt contain the named instance var child1 = container.CreateChildContainer(); var child1Thing = Thing1.Build("child1"); child1.Configure(_ => _.For <IThing>().Add(child1Thing).Named("A")); child1.GetInstance <IThing>("A").ShouldBeTheSameAs(child1Thing); Debug.WriteLine(container.WhatDoIHave()); var child2 = container.CreateChildContainer(); var child2Thing = Thing1.Build("child2"); child2.Configure(_ => _.For <IThing>().Add(child2Thing).Named("A")); child2.GetInstance <IThing>("A") .ShouldBeTheSameAs(child2Thing); child2.TryGetInstance <IThing>("A") .ShouldBeTheSameAs(child2Thing); child2.TryGetInstance <IThing>("A") .ShouldBeTheSameAs(child2Thing); container.TryGetInstance <IThing>("A").ShouldBeNull(); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Propery1,Propery2,Propery3,Propery4")] Thing1 thing1) { if (id != thing1.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(thing1); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Thing1Exists(thing1.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(thing1)); }
public void EnumerableMap() { var definitions = new MapDefinitionCollection(() => new IMapDefinition[] { new MapperDefinition1() }); var mapper = new UmbracoMapper(definitions, _scopeProvider); var thing1A = new Thing1 { Value = "valueA" }; var thing1B = new Thing1 { Value = "valueB" }; Thing1[] thing1 = { thing1A, thing1B }; var thing2 = mapper.Map <IEnumerable <Thing1>, IEnumerable <Thing2> >(thing1).ToList(); Assert.IsNotNull(thing2); Assert.AreEqual(2, thing2.Count); Assert.AreEqual("valueA", thing2[0].Value); Assert.AreEqual("valueB", thing2[1].Value); thing2 = mapper.Map <IEnumerable <Thing2> >(thing1).ToList(); Assert.IsNotNull(thing2); Assert.AreEqual(2, thing2.Count); Assert.AreEqual("valueA", thing2[0].Value); Assert.AreEqual("valueB", thing2[1].Value); thing2 = mapper.MapEnumerable <Thing1, Thing2>(thing1).ToList(); Assert.IsNotNull(thing2); Assert.AreEqual(2, thing2.Count); Assert.AreEqual("valueA", thing2[0].Value); Assert.AreEqual("valueB", thing2[1].Value); }
public void SimpleMap() { var definitions = new MapDefinitionCollection(new IMapDefinition[] { new MapperDefinition1(), }); var mapper = new UmbracoMapper(definitions); var thing1 = new Thing1 { Value = "value" }; var 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); }
public async Task <IActionResult> Create([Bind("Id,Propery1,Propery2,Propery3,Propery4")] Thing1 thing1) { if (ModelState.IsValid) { _context.Add(thing1); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(thing1)); }
public void TestThingSetObjectSerialization() { SetTestPatternBuffer patternBuffer = new SetTestPatternBuffer(); Thing1 t1 = new Thing1(1, 2); Thing1 t2 = new Thing1(3, 4); Thing1SetObject o1 = new Thing1SetObject(new HashSet <Thing1>() { t1, t2 }); byte[] bytes = patternBuffer.Energize(o1); // 1 byte - Thing1SetObject type ID // 1 byte - set count // 8 bytes - 2 invariant ints // 8 bytes - 2 invariant ints Assert.That(bytes.Length, Is.EqualTo(19)); object o2 = patternBuffer.Energize(bytes); Thing1SetObject p1 = (Thing1SetObject)o2; Assert.That(p1 is Thing1SetObject, Is.True); Assert.That(p1.ThingSetValue.Count, Is.EqualTo(2)); Assert.That(o1 == p1, Is.False); }
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); }
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); }
public void named_registrations_should_be_isolated_on_nested_containers() { var container = new Container(_ => { }); //Parent Container doesnt contain the named instance var nested1 = container.GetNestedContainer(); var nested1Thing = Thing1.Build("child1"); nested1.Configure(_ => _.For <IThing>().Add(nested1Thing).Named("A")); var containerNested1Thing = nested1.GetInstance <IThing>("A"); containerNested1Thing.ShouldBe(nested1Thing); var nested2 = container.GetNestedContainer(); var nested2Thing = Thing1.Build("child2"); nested2.Configure(_ => _.For <IThing>().Add(nested2Thing).Named("A")); var nested2ContainerThing = nested2.TryGetInstance <IThing>("A"); nested2ContainerThing.ShouldBe(nested2Thing); var parentContainerThing = container.TryGetInstance <IThing>("A"); parentContainerThing.ShouldBeNull(); }
public void TestThingListObjectSerialization() { ListTestPatternBuffer patternBuffer = new ListTestPatternBuffer(); Thing1 t1 = new Thing1(1, 2); Thing1 t2 = new Thing1(3, 4); Thing1ListObject o1 = new Thing1ListObject(new List <Thing1>() { t1, t2 }); byte[] bytes = patternBuffer.Energize(o1); // 1 byte Thing1ListObject type ID // 1 byte null bits // 1 byte list count // 8 bytes Thing1 (2 invariant ints) // 8 bytes Thing1 (2 invariant ints) Assert.AreEqual(19, bytes.Length); object o2 = patternBuffer.Energize(bytes); Thing1ListObject p1 = (Thing1ListObject)o2; Assert.IsTrue(p1 is Thing1ListObject); Assert.AreEqual(2, p1.ThingListValue.Count); Assert.IsFalse(o1 == p1); }
public Thing2(Thing1 thing1) { Thing1 = thing1; }
public Thing2(Thing1 thing1) { }
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 } }
public static void Main(string[] args) { var thing1 = new Thing1(); var thing2 = new Thing2(); }
private void Map1(Thing1 source, Thing2 target, MapperContext context) => target.Value = source.Value;
public Thing2(Thing1 thing1) { Thing1 = thing1; }
public Thing3(Thing1 thing) { }