本文整理汇总了C#中Container.GetAllInstances方法的典型用法代码示例。如果您正苦于以下问题:C# Container.GetAllInstances方法的具体用法?C# Container.GetAllInstances怎么用?C# Container.GetAllInstances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.GetAllInstances方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Scenario1
public void Scenario1()
{
// Arrange
var container = new Container();
var plugins = new KeyedRegistrations<string, IPlugin>(container);
container.Options.DependencyInjectionBehavior = new NamedDependencyInjectionBehavior(
container.Options.DependencyInjectionBehavior,
(serviceType, name) => plugins.GetRegistration(name));
plugins.Register(typeof(Plugin1), "1");
plugins.Register<Plugin2>("2");
plugins.Register(typeof(Plugin3), "3", Lifestyle.Singleton);
plugins.Register(() => new Plugin("4"), "4");
plugins.Register(() => new Plugin("5"), "5", Lifestyle.Singleton);
container.RegisterCollection<IPlugin>(plugins);
container.RegisterDecorator(typeof(IPlugin), typeof(PluginDecorator), Lifestyle.Singleton,
context => context.ImplementationType == typeof(Plugin3));
container.RegisterSingleton<Func<string, IPlugin>>(key => plugins.GetInstance(key));
container.Verify();
// Act
var actualPlugins1 = container.GetAllInstances<IPlugin>().ToArray();
var actualPlugins2 = container.GetAllInstances<IPlugin>().ToArray();
var factory = container.GetInstance<Func<string, IPlugin>>();
var consumer = container.GetInstance<NamedPluginConsumer>();
// Assert
AssertThat.IsInstanceOfType(typeof(Plugin1), actualPlugins1[0]);
AssertThat.IsInstanceOfType(typeof(Plugin2), actualPlugins1[1]);
AssertThat.IsInstanceOfType(typeof(PluginDecorator), actualPlugins1[2]);
AssertThat.IsInstanceOfType(typeof(Plugin), actualPlugins1[3]);
AssertThat.IsInstanceOfType(typeof(Plugin), actualPlugins1[4]);
AssertThat.IsInstanceOfType(typeof(Plugin1), factory("1"));
AssertThat.IsInstanceOfType(typeof(Plugin2), factory("2"));
AssertThat.IsInstanceOfType(typeof(PluginDecorator), factory("3"));
AssertThat.IsInstanceOfType(typeof(Plugin), factory("4"));
AssertThat.IsInstanceOfType(typeof(Plugin), factory("5"));
Assert.AreNotSame(actualPlugins1[0], actualPlugins2[0]);
Assert.AreNotSame(actualPlugins1[1], actualPlugins2[1]);
Assert.AreSame(actualPlugins1[2], actualPlugins2[2]);
Assert.AreNotSame(actualPlugins1[3], actualPlugins2[3]);
Assert.AreSame(actualPlugins1[4], actualPlugins2[4]);
AssertThat.IsInstanceOfType(typeof(Plugin1), consumer.Plugin1);
AssertThat.IsInstanceOfType(typeof(Plugin2), consumer.Plugin2);
AssertThat.IsInstanceOfType(typeof(PluginDecorator), consumer.Plugin3);
AssertThat.IsInstanceOfType(typeof(Plugin), consumer.Plugin4);
}
示例2: eject_and_remove_a_single_instance
public void eject_and_remove_a_single_instance()
{
var container = new Container(x =>
{
x.For<DisposedGuy>().Singleton().AddInstances(guys =>
{
guys.Type<DisposedGuy>().Named("A");
guys.Type<DisposedGuy>().Named("B");
guys.Type<DisposedGuy>().Named("C");
});
});
// Fetch all the SingletonThing objects
var guyA = container.GetInstance<DisposedGuy>("A");
var guyB = container.GetInstance<DisposedGuy>("B");
var guyC = container.GetInstance<DisposedGuy>("C");
// Eject *only* guyA
container.Model.For<DisposedGuy>().EjectAndRemove("A");
// Only guyA should be disposed
guyA.WasDisposed.ShouldBeTrue();
guyB.WasDisposed.ShouldBeFalse();
guyC.WasDisposed.ShouldBeFalse();
// Now, see that guyA is really gone
container.GetAllInstances<DisposedGuy>()
.ShouldHaveTheSameElementsAs(guyB, guyC);
}
示例3: ejecting_all_from_a_family
public void ejecting_all_from_a_family()
{
var container = new Container(x =>
{
x.For<DisposedGuy>().Singleton().AddInstances(guys =>
{
guys.Type<DisposedGuy>().Named("A");
guys.Type<DisposedGuy>().Named("B");
guys.Type<DisposedGuy>().Named("C");
});
});
// Fetch all the SingletonThing objects
var guyA = container.GetInstance<DisposedGuy>("A");
var guyB = container.GetInstance<DisposedGuy>("B");
var guyC = container.GetInstance<DisposedGuy>("C");
container.Model.EjectAndRemove<DisposedGuy>();
// All the SingletonThing instances should be disposed
// as they are removed from the Container
guyA.WasDisposed.ShouldBeTrue();
guyB.WasDisposed.ShouldBeTrue();
guyC.WasDisposed.ShouldBeTrue();
// And now the container no longer has any
// registrations for DisposedGuy
container.GetAllInstances<DisposedGuy>()
.Any().ShouldBeFalse();
}
示例4: register_with_basic_authentication_disabled
public void register_with_basic_authentication_disabled()
{
var registry = new FubuRegistry();
registry.Import<Saml2Extensions>();
registry.Import<ApplyAuthentication>();
var samlCertificateRepository = MockRepository.GenerateMock<ISamlCertificateRepository>();
samlCertificateRepository.Stub(x => x.AllKnownCertificates()).Return(new SamlCertificate[0]);
registry.Services(x =>
{
x.SetServiceIfNone<IPrincipalBuilder>(MockRepository.GenerateMock<IPrincipalBuilder>());
x.AddService<ISamlResponseHandler>(MockRepository.GenerateMock<ISamlResponseHandler>());
x.SetServiceIfNone<ISamlCertificateRepository>(samlCertificateRepository);
});
registry.AlterSettings<AuthenticationSettings>(x => {
x.MembershipEnabled = MembershipStatus.Disabled;
});
var container = new Container();
var runtime = FubuApplication.For(registry).StructureMap(container).Bootstrap();
var strategies = container.GetAllInstances<IAuthenticationStrategy>();
strategies.Single().ShouldBeOfType<SamlAuthenticationStrategy>();
}
示例5: AutomaticallyFindRegistryFromAssembly
public void AutomaticallyFindRegistryFromAssembly()
{
var container = new Container(x =>
{
x.Scan(s =>
{
s.AssemblyContainingType<RedGreenRegistry>();
s.LookForRegistries();
});
});
var colors = new List<string>();
foreach (var widget in container.GetAllInstances<IWidget>())
{
if (!(widget is ColorWidget))
{
continue;
}
var color = (ColorWidget) widget;
colors.Add(color.Color);
}
colors.Sort();
colors.ShouldHaveTheSameElementsAs("Black", "Blue", "Brown", "Green", "Red", "Yellow");
}
示例6: hydrate_through_container_facility_smoke_test
public void hydrate_through_container_facility_smoke_test()
{
var container = new Container(x =>
{
x.For<IStreamingData>().Use(MockRepository.GenerateMock<IStreamingData>());
x.For<IHttpWriter>().Use(new NulloHttpWriter());
x.For<ICurrentChain>().Use(new CurrentChain(null, null));
x.For<ICurrentHttpRequest>().Use(new StubCurrentHttpRequest{
TheApplicationRoot = "http://server"
});
x.For<IResourceHash>().Use(MockRepository.GenerateMock<IResourceHash>());
});
FubuApplication.For(() => registry).StructureMap(container).Bootstrap();
container.Model.InstancesOf<IActionBehavior>().Count().ShouldBeGreaterThan(3);
// The InputBehavior is first
container.GetAllInstances<IActionBehavior>().Each(x =>
{
// Don't mess with the asset content chain
if (x is OutputCachingBehavior) return;
if (x.GetType().Closes(typeof (InputBehavior<>)))
{
x.As<BasicBehavior>().InsideBehavior.ShouldBeOfType<FakeUnitOfWorkBehavior>().Inner.ShouldNotBeNull();
}
else
{
x.ShouldBeOfType<FakeUnitOfWorkBehavior>().Inner.ShouldNotBeNull();
}
});
}
示例7: Application_Start
protected void Application_Start()
{
// I'm not using areas... no need to register.
// AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
// create a container just to pull in tenants
var topContainer = new Container();
topContainer.Configure(config =>
{
config.Scan(scanner =>
{
// scan the tenants folder
// (For some reason just passing "Tenants" doesn't seem to work, which it should according to the docs)
scanner.AssembliesFromPath(Path.Combine(Server.MapPath("~/"), "Tenants"));
// pull in all the tenant types
scanner.AddAllTypesOf<IApplicationTenant>();
});
});
// create selectors
var tenantSelector = new DefaultTenantSelector(topContainer.GetAllInstances<IApplicationTenant>());
var containerSelector = new TenantContainerResolver(tenantSelector);
// clear view engines, we don't want anything other than spark
ViewEngines.Engines.Clear();
// set view engine
ViewEngines.Engines.Add(new TenantViewEngine(tenantSelector));
// set controller factory
ControllerBuilder.Current.SetControllerFactory(new ContainerControllerFactory(containerSelector));
}
示例8: AddInstanceWithNameOnlyAddsOneInstanceToStructureMap
public void AddInstanceWithNameOnlyAddsOneInstanceToStructureMap()
{
var container = new Container(x => { x.For<Something>().Add<RedSomething>().Named("Red"); });
container.GetAllInstances<Something>()
.Count().ShouldBe(1);
}
示例9: Add_an_instance_by_lambda
public void Add_an_instance_by_lambda()
{
var container = new Container(r => { r.For<IWidget>().Add(c => new AWidget()); });
container.GetAllInstances<IWidget>()
.First()
.ShouldBeOfType<AWidget>();
}
示例10: add_an_instance_by_literal_object
public void add_an_instance_by_literal_object()
{
var aWidget = new AWidget();
var container = new Container(x => { x.For<IWidget>().Use(aWidget); });
container.GetAllInstances<IWidget>().First().ShouldBeTheSameAs(aWidget);
}
示例11: get_registrations_from_all_included_registries
public void get_registrations_from_all_included_registries()
{
var container = new Container(new ARegistry());
container.GetAllInstances<IWidget>().OrderBy(x => x.GetType().Name)
.Select(x => x.GetType())
.ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget), typeof(DefaultWidget));
}
示例12: AddInstanceWithNameOnlyAddsOneInstanceToStructureMap
public void AddInstanceWithNameOnlyAddsOneInstanceToStructureMap()
{
IContainer container =
new Container(
registry => registry.For<Something>().Add<RedSomething>().WithName("Red"));
IList<Something> instances = container.GetAllInstances<Something>();
Assert.AreEqual(1, instances.Count);
}
示例13: ConstructSomethingNotByDefault
public void ConstructSomethingNotByDefault()
{
var concretion = new Concretion();
var container = new Container(r => { r.For<Abstraction>().Add(c => concretion); });
container.GetAllInstances<Abstraction>().First().ShouldBeTheSameAs(concretion);
}
示例14: ApiAcceptanceTests
public ApiAcceptanceTests()
{
var currentUser = new Operator
{
Key = "andy-dote",
Name = "Andy Dote",
CanCreatePermissions = true,
CanCreateRoles = true,
CanCreateUsers = true
};
var config = new MagistrateConfiguration
{
EventStore = new InMemoryEventStore(),
User = () => currentUser
};
var container = new Container(new MagistrateRegistry(config.EventStore));
container.GetInstance<Boot>().Load();
var ps = container.GetInstance<PermissionService>();
var rs = container.GetInstance<RoleService>();
var us = container.GetInstance<UserService>();
var p1 = Permission.Create(ps, currentUser, new PermissionKey("perm-one"), "first", "first permission");
var p2 = Permission.Create(ps, currentUser, new PermissionKey("perm-two"), "second", "second permission");
var p3 = Permission.Create(ps, currentUser, new PermissionKey("perm-three"), "third", "third permission");
var r1 = Role.Create(rs, currentUser, new RoleKey("role-one"), "first", "first role");
r1.AddPermission(currentUser, p1.ID);
var r2 = Role.Create(rs, currentUser, new RoleKey("role-two"), "second", "second role");
r2.AddPermission(currentUser, p2.ID);
var u1 = User.Create(us, currentUser, new UserKey("user-one"), "first");
u1.AddRole(currentUser, r1.ID);
u1.AddInclude(currentUser, p2.ID);
u1.AddRevoke(currentUser, p3.ID);
var store = container.GetInstance<AggregateStore<Guid>>();
store.Save(MagistrateSystem.MagistrateStream, p1);
store.Save(MagistrateSystem.MagistrateStream, p2);
store.Save(MagistrateSystem.MagistrateStream, p3);
store.Save(MagistrateSystem.MagistrateStream, r1);
store.Save(MagistrateSystem.MagistrateStream, r2);
store.Save(MagistrateSystem.MagistrateStream, u1);
_server = TestServer.Create(app =>
{
app.Use<MagistrateOperatorMiddleware>(config);
container
.GetAllInstances<IController>()
.ForEach(controller => controller.Configure(app));
});
}
示例15: AddInstanceByNameOnlyAddsOneInstanceToStructureMap
public void AddInstanceByNameOnlyAddsOneInstanceToStructureMap()
{
var container = new Container(r =>
{
r.For<Something>().Add<RedSomething>().WithName("Red");
});
container.GetAllInstances<Something>().Count.ShouldEqual(1);
}