static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete <Hello>(Lifetime.Singleton); // will register as the implemented interfaces var container = registrar.Build(); // will get var instance1 = container.Resolve <IPrintable>(); var instance2 = container.Resolve <IInvokable>(); // Will get same instance // since both is implemented by the same class. instance1.Print(null); instance2.DoSomeWork(); try { container.Resolve <Hello>(); } catch (ServiceNotRegisteredException) { // throws exception as we've implemented // non .NET specific interfaces. // // since we should not depend on concretes. // (design choice in griffin.container) } Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
private static async Task <int> Main(string[] args) { // TODO: load the list of assemblies from the app working directory, later we might load from nuget var assemblies = new[] { typeof(CommonModule).Assembly, typeof(NormalizeModule).Assembly, typeof(CalculateModule).Assembly, typeof(ConfigModule).Assembly, typeof(OutputModule).Assembly, typeof(CliModule).Assembly }; var gitVersionModules = assemblies .SelectMany(a => a.GetTypes().Where(TypeIsGitVersionModule)) .Select(t => (IGitVersionModule)Activator.CreateInstance(t) !) .ToList(); using var serviceProvider = new ContainerRegistrar() .RegisterModules(gitVersionModules) .AddLogging(args) .Build(); var app = serviceProvider.GetService <GitVersionApp>(); var result = await app.RunAsync(args); if (!Console.IsInputRedirected) { Console.ReadKey(); } return(result); }
public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore) { var builder = new ContainerRegistrar(); //need to invoke first to allow plug-ins to override default behavior. action(builder); builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); builder.RegisterService(CreateUnitOfWork, Lifetime.Scoped); builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton); builder.RegisterService(CreateConnection, Lifetime.Transient); RegisterBuiltInComponents(builder); RegisterQueues(builder); builder.RegisterService(x => Container, Lifetime.Singleton); builder.RegisterService(x => x); builder.RegisterService(CreateAnalysisDbContext); builder.RegisterInstance(configStore); builder.RegisterType(typeof(IConfiguration <>), typeof(ConfigWrapper <>), Lifetime.Transient); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterService(x => configStore.Load <BaseConfiguration>()); var ioc = builder.Build(); DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc)); GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc); Container = new GriffinContainerAdapter(ioc); }
void IExtensionApplication.Initialize() { ContainerRegistrar.SetupContainer(); this.logger = ContainerRegistrar.Container.Resolve <IWebTracker>(); AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException; Application.DocumentManager.MdiActiveDocument.CommandWillStart += logger.TrackCommandUsage; var uiGenerator = ContainerRegistrar.Container.Resolve <IUiGenerator>(); try { uiGenerator.GenerateUi(false); } catch (Exception exception) { logger.TrackException(exception); } ContainerRegistrar.Container.Release(uiGenerator); IKojtoCadUpdater updater; try { updater = ContainerRegistrar.Container.Resolve <IKojtoCadUpdater>(); updater.UpdateKojtoCad(); } catch (Exception exception) { this.logger.TrackException(exception); } }
private static void AddCakeCoreModules(ContainerBuilder containerBuilder) { var cakeRegistrar = new ContainerRegistrar(containerBuilder); cakeRegistrar.RegisterModule(new CoreModule()); cakeRegistrar.RegisterModule(new CakeExtModule()); }
public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore) { var builder = new ContainerRegistrar(); builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); builder.RegisterService(CreateConnection, Lifetime.Scoped); builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton); builder.RegisterInstance(Startup.ConnectionFactory); action(builder); RegisterBuiltInComponents(builder); RegisterQueues(builder); builder.RegisterService(x => Container, Lifetime.Singleton); builder.RegisterService(x => x); builder.RegisterConcrete <AnalysisDbContext>(); builder.RegisterInstance(configStore); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var ioc = builder.Build(); DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc)); GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc); Container = new GriffinContainerAdapter(ioc); }
private void RegisterBuiltInComponents(ContainerRegistrar builder) { builder.RegisterComponents(Lifetime.Scoped, typeof(AppType).Assembly); builder.RegisterComponents(Lifetime.Scoped, typeof(UserRepository).Assembly); builder.RegisterComponents(Lifetime.Scoped, typeof(ReportAnalyzer.Handlers.Reports.ReportAnalyzer).Assembly); }
private static Container ConfigureGriffinContainer() { var registrar = new ContainerRegistrar(Lifetime.Scoped); registrar.RegisterComponents(Lifetime.Default, Assembly.GetExecutingAssembly()); var container = registrar.Build(); return container; }
static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete <Hello>(Lifetime.Transient); // <-- transient var container = registrar.Build(); // will get var instance1 = container.Resolve <Hello>(); instance1.World(); // a new instance var instance2 = container.Resolve <Hello>(); instance2.World(); // every time = transient var instance3 = container.Resolve <Hello>(); instance3.World(); Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
public void Build(Action <ContainerRegistrar> action) { var builder = new ContainerRegistrar(); builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); builder.RegisterService(CreateConnection, Lifetime.Scoped); builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton); action(builder); builder.RegisterComponents(Lifetime.Scoped, typeof(ValidateNewLoginHandler).Assembly); builder.RegisterComponents(Lifetime.Scoped, typeof(UserRepository).Assembly); builder.RegisterComponents(Lifetime.Scoped, typeof(ScanForNewErrorReports).Assembly); builder.RegisterComponents(Lifetime.Scoped, typeof(QueueProvider).Assembly); builder.RegisterService <IContainer>(x => Container, Lifetime.Singleton); builder.RegisterService <IServiceLocator>(x => x); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(Assembly.GetExecutingAssembly()); var ioc = builder.Build(); DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc)); GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc); Container = new GriffinContainerAdapter(ioc); }
public void TwoChildContainers() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete <MySelf>(); var builder = new ContainerBuilder(); var container = builder.Build(registrar); MySelf instance1, instance2; var childContainer1 = container.CreateChildContainer(); var childContainer2 = container.CreateChildContainer(); instance1 = childContainer1.Resolve <MySelf>(); instance2 = childContainer2.Resolve <MySelf>(); Assert.False(instance1.IsDisposed); Assert.False(instance2.IsDisposed); childContainer1.Dispose(); Assert.True(instance1.IsDisposed); Assert.False(instance2.IsDisposed); childContainer2.Dispose(); Assert.True(instance2.IsDisposed); }
static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Hello>(Lifetime.Singleton); // will register as the implemented interfaces var container = registrar.Build(); // will get var instance1 = container.Resolve<IPrintable>(); var instance2 = container.Resolve<IInvokable>(); // Will get same instance // since both is implemented by the same class. instance1.Print(null); instance2.DoSomeWork(); try { container.Resolve<Hello>(); } catch(ServiceNotRegisteredException) { // throws exception as we've implemented // non .NET specific interfaces. // // since we should not depend on concretes. // (design choice in griffin.container) } Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
/// <summary> /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address. /// </summary> /// <param name="serviceType">Specifies the type of service to host.</param> /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param> /// <returns> /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address. /// </returns> protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { if (serviceType == null) { throw new ArgumentNullException("serviceType"); } ServiceBehaviorAttribute attribute; var lifetime = GetLifetime(serviceType, out attribute); if (_container == null) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete(serviceType, lifetime); registrar.RegisterModules(serviceType.Assembly); _container = registrar.Build(); } var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses); if (ConfigurationCallback != null) { ConfigurationCallback(host); } return(host); }
/// <summary> /// Builds the required services and an <see cref="ICakeHost" /> using the specified options. /// </summary> /// <returns>The built <see cref="ICakeHost" />.</returns> public ICakeHost Build() { try { // Create the "base" container with the minimum // stuff registered to run Cake at all. var registrar = new ContainerRegistrar(); registrar.RegisterModule(new CoreModule()); registrar.RegisterModule(new FrostingModule()); var container = registrar.Build(); // Add custom registrations to the container. AddCustomRegistrations(container); // Find and register tasks with the container. RegisterTasks(container); // Resolve the application and run it. return(container.Resolve <ICakeHost>()); } catch (Exception exception) { return(new ErrorCakeHost(exception)); } }
public override void PrepareBasic() { var registrar = new ContainerRegistrar(); RegisterBasic(registrar); this.container = registrar.Build(); }
public void RegistrarRegistered( ContainerRegistrar registrar) { if (IsEnabled(EventLevel.Verbose, Keywords.vmAspects | Keywords.DI)) { RegistrarRegistered(registrar.GetType().FullName); } }
/// <summary> /// The application entry point. /// </summary> /// <returns>The application exit code.</returns> public static int Main() { ICakeLog log = null; try { // Parse arguments. var args = QuoteAwareStringSplitter .Split(Environment.CommandLine) .Skip(1) // Skip executable. .ToArray(); var builder = new ContainerRegistrar(); builder.RegisterModule(new CakeModule()); builder.RegisterModule(new CoreModule()); builder.RegisterModule(new CommonModule()); // Build the container. using (var container = builder.Build()) { // Resolve the log. log = container.Resolve <ICakeLog>(); // Parse the options. var parser = container.Resolve <IArgumentParser>(); var options = parser.Parse(args); // Set verbosity. log.Verbosity = options.Verbosity; // Rebuild the container. builder = new ContainerRegistrar(); var provider = container.Resolve <CakeConfigurationProvider>(); builder.RegisterModule(new ConfigurationModule(provider, options)); builder.RegisterModule(new ArgumentsModule(options)); builder.RegisterModule(new ScriptingModule(options, log)); builder.Update(container); // Register the NuGetModule builder = new ContainerRegistrar(); var configuration = container.Resolve <ICakeConfiguration>(); builder.RegisterModule(new NuGetModule(configuration)); builder.Update(container); // Load all modules. var loader = container.Resolve <ModuleLoader>(); loader.LoadModules(container, options); // Resolve and run the application. var application = container.Resolve <CakeApplication>(); return(application.Run(options)); } } catch (Exception ex) { return(LogException(log, ex)); } }
public void TestAttribute() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterUsingAttribute<ContainerServiceAttribute>(Assembly.GetExecutingAssembly()); var container = registrar.Build(); Assert.NotNull(container.Resolve<SomeService>()); }
private static Container ConfigureGriffinContainer() { var registrar = new ContainerRegistrar(Lifetime.Scoped); registrar.RegisterComponents(Lifetime.Default, Assembly.GetExecutingAssembly()); var container = registrar.Build(); return(container); }
public void ResolveFromModules() { var registrar = new ContainerRegistrar(); registrar.RegisterModules(Assembly.GetExecutingAssembly()); var builder = new ContainerBuilder(); var container = builder.Build(registrar); container.Resolve<MySelf>(); }
public void ResolveFromComponents() { var registrar = new ContainerRegistrar(); registrar.RegisterComponents(Lifetime.Singleton, Assembly.GetExecutingAssembly()); var builder = new ContainerBuilder(); var container = builder.Build(registrar); container.Resolve<OneDepencency>(); }
public void TestAttribute() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterUsingAttribute <ContainerServiceAttribute>(Assembly.GetExecutingAssembly()); var container = registrar.Build(); Assert.NotNull(container.Resolve <SomeService>()); }
public void Test() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete <Service1>(); var builder = new ContainerBuilder(); Assert.Throws <ConcreteDependencyMissingException>(() => builder.Build(registrar)); }
public void DispatchReal() { var handler = Substitute.For<IExecuteQuery<FakeQuery, string>>(); var registrar = new ContainerRegistrar(); registrar.RegisterInstance(handler); var dispatcher = new QueryDispatcher(registrar.Build()); dispatcher.Execute(new FakeQuery()); }
private static Container CreateContainer() { var registrar = new ContainerRegistrar(); registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); // yay, dispatch those queries. registrar.DispatchQueries(); return registrar.Build(); }
public void Test() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Service1>(); var builder = new ContainerBuilder(); Assert.Throws<ConcreteDependencyMissingException>(() =>builder.Build(registrar)); }
public void TestDelegateFactory() { var registrar = new ContainerRegistrar(); registrar.RegisterService <MySelf>(ctnr => new MySelf(), Lifetime.Transient); registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton); var container = registrar.Build(); container.Resolve <OneDepencency>(); }
public void ResolveFromModules() { var registrar = new ContainerRegistrar(); registrar.RegisterModules(Assembly.GetExecutingAssembly()); var builder = new ContainerBuilder(); var container = builder.Build(registrar); container.Resolve <MySelf>(); }
public void ResolveFromComponents() { var registrar = new ContainerRegistrar(); registrar.RegisterComponents(Lifetime.Singleton, Assembly.GetExecutingAssembly()); var builder = new ContainerBuilder(); var container = builder.Build(registrar); container.Resolve <OneDepencency>(); }
public void TestSingleton() { var registrar = new ContainerRegistrar(); registrar.RegisterInstance <MySelf>(new MySelf()); registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton); var container = registrar.Build(); container.Resolve <OneDepencency>(); }
public void DispatchReal() { var handler = Substitute.For <IExecuteQuery <FakeQuery, string> >(); var registrar = new ContainerRegistrar(); registrar.RegisterInstance(handler); var dispatcher = new QueryDispatcher(registrar.Build()); dispatcher.Execute(new FakeQuery()); }
public void NoRegisteredConcretes() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterConcrete<NoInstancesSubject>(); var c = registrar.Build(); var actual = c.Resolve<NoInstancesSubject>(); Assert.NotNull(actual); Assert.IsAssignableFrom<NoInstancesSubject>(actual); }
private static void RegisterComplex(ContainerRegistrar registrar) { registrar.RegisterType<IFirstService, FirstService>(Lifetime.Singleton); registrar.RegisterType<ISecondService, SecondService>(Lifetime.Singleton); registrar.RegisterType<IThirdService, ThirdService>(Lifetime.Singleton); registrar.RegisterType<ISubObjectOne, SubObjectOne>(Lifetime.Transient); registrar.RegisterType<ISubObjectTwo, SubObjectTwo>(Lifetime.Transient); registrar.RegisterType<ISubObjectThree, SubObjectThree>(Lifetime.Transient); registrar.RegisterType<IComplex, Complex>(Lifetime.Transient); }
private static void RegisterComplex(ContainerRegistrar registrar) { registrar.RegisterType <IFirstService, FirstService>(Lifetime.Singleton); registrar.RegisterType <ISecondService, SecondService>(Lifetime.Singleton); registrar.RegisterType <IThirdService, ThirdService>(Lifetime.Singleton); registrar.RegisterType <ISubObjectOne, SubObjectOne>(Lifetime.Transient); registrar.RegisterType <ISubObjectTwo, SubObjectTwo>(Lifetime.Transient); registrar.RegisterType <ISubObjectThree, SubObjectThree>(Lifetime.Transient); registrar.RegisterType <IComplex, Complex>(Lifetime.Transient); }
public void should_Be_able_To_Register_a_service_using_a_factory_method() { var reg = new ContainerRegistrar(); reg.RegisterService(CreateFirstService, Lifetime.Scoped); reg.RegisterService(CreateMyService, Lifetime.Singleton); var container = reg.Build(); var actual = container.Resolve <ISomeServiceInterface>(); Assert.NotNull(actual); }
public void RegisterServiceAsInstance() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterInstance<IThinkNot>(new Will()); registrar.RegisterConcrete<InstanceSubject>(); var c = registrar.Build(); var actual = c.Resolve<InstanceSubject>(); Assert.NotNull(actual); Assert.IsAssignableFrom<InstanceSubject>(actual); }
private static void RegisterStandard(ContainerRegistrar registrar) { registrar.RegisterType <ISingleton1, Singleton1>(Lifetime.Singleton); registrar.RegisterType <ISingleton2, Singleton2>(Lifetime.Singleton); registrar.RegisterType <ISingleton3, Singleton3>(Lifetime.Singleton); registrar.RegisterType <ITransient1, Transient1>(Lifetime.Transient); registrar.RegisterType <ITransient2, Transient2>(Lifetime.Transient); registrar.RegisterType <ITransient3, Transient3>(Lifetime.Transient); registrar.RegisterType <ICombined1, Combined1>(Lifetime.Transient); registrar.RegisterType <ICombined2, Combined2>(Lifetime.Transient); registrar.RegisterType <ICombined3, Combined3>(Lifetime.Transient); }
static void Main(string[] args) { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterComponents(Lifetime.Transient, Assembly.GetExecutingAssembly()); registrar.RegisterService<ICommandDispatcher>(f => new ContainerDispatcher(f)); var container = registrar.Build(); var cmd = new CreateUser("arne", "King Arne"); // the exception is thrown on purpose, read it. container.Resolve<ICommandDispatcher>().Dispatch(cmd); }
public void Should_be_able_To_resolve_open_generics_which_are_type_restricted() { var registrar = new ContainerRegistrar(); registrar.RegisterType(typeof(IRestrictedGeneric <>), typeof(RestrictedGeneric <>), Lifetime.Singleton); registrar.RegisterConcrete <Word>(Lifetime.Transient); var container = registrar.Build(); var actual = container.Resolve <IRestrictedGeneric <SomeClass> >(); Assert.NotNull(actual); }
public void NoRegisteredConcretes() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterConcrete <NoInstancesSubject>(); var c = registrar.Build(); var actual = c.Resolve <NoInstancesSubject>(); Assert.NotNull(actual); Assert.IsAssignableFrom <NoInstancesSubject>(actual); }
public void InterfaceToConcrete() { var registrar = new ContainerRegistrar(); registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Transient); registrar.RegisterConcrete <Word>(Lifetime.Transient); var container = registrar.Build(); var actual = container.Resolve <ISomeDude <string> >(); Assert.NotNull(actual); }
public void Should_be_able_To_resolve_open_generics_in_singleton_scope() { var registrar = new ContainerRegistrar(); registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Singleton); registrar.RegisterConcrete <Word>(Lifetime.Transient); var container = registrar.Build(); var actual = container.Resolve <ISomeDude <string> >(); Assert.NotNull(actual); }
public void ResolveAllStartable() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Startable1>(Lifetime.Singleton); registrar.RegisterConcrete<Startable2>(Lifetime.Singleton); var builder = new ContainerBuilder(); var container = builder.Build(registrar); var instances = container.ResolveAll<ISingletonStartable>(); Assert.Equal(2, instances.Count()); }
public void SimpleRegistrationTransient() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<MySelf>(Lifetime.Transient); var builder = new ContainerBuilder(); var container = builder.Build(registrar); var instance1 = container.Resolve<MySelf>(); var instance2 = container.Resolve<MySelf>(); Assert.NotSame(instance1, instance2); }
private static void RegisterDummies(ContainerRegistrar registrar) { registrar.RegisterType<IDummyOne, DummyOne>(Lifetime.Transient); registrar.RegisterType<IDummyTwo, DummyTwo>(Lifetime.Transient); registrar.RegisterType<IDummyThree, DummyThree>(Lifetime.Transient); registrar.RegisterType<IDummyFour, DummyFour>(Lifetime.Transient); registrar.RegisterType<IDummyFive, DummyFive>(Lifetime.Transient); registrar.RegisterType<IDummySix, DummySix>(Lifetime.Transient); registrar.RegisterType<IDummySeven, DummySeven>(Lifetime.Transient); registrar.RegisterType<IDummyEight, DummyEight>(Lifetime.Transient); registrar.RegisterType<IDummyNine, DummyNine>(Lifetime.Transient); registrar.RegisterType<IDummyTen, DummyTen>(Lifetime.Transient); }
public void Test() { var registrar = new ContainerRegistrar(Lifetime.Transient); registrar.RegisterConcrete<Will>(); registrar.RegisterConcrete<Wont>(); registrar.RegisterConcrete<Subject>(); var c = registrar.Build(); var actual = c.Resolve<Subject>(); Assert.NotNull(actual); Assert.IsAssignableFrom<Subject>(actual); }
public void OneDependencySingleton() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<MySelf>(Lifetime.Transient); registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton); var builder = new ContainerBuilder(); var container = builder.Build(registrar); var instance1 = container.Resolve<OneDepencency>(); var instance2 = container.Resolve<OneDepencency>(); Assert.Same(instance1, instance2); }
public void ChildContainer() { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<MySelf>(); var builder = new ContainerBuilder(); var container = builder.Build(registrar); MySelf instance; using (var childContainer = container.CreateChildContainer()) { instance = childContainer.Resolve<MySelf>(); } Assert.True(instance.IsDisposed); }
private Container ConfigureGriffinContainer() { var registrar = new ContainerRegistrar(); // Repositories registrar.RegisterConcrete<RaceMemoryRepository>(Lifetime.Singleton); registrar.RegisterConcrete<UserMemoryRepository>(Lifetime.Singleton); registrar.RegisterConcrete<ResultMemoryRepository>(Lifetime.Singleton); registrar.RegisterConcrete<ApplicationStateMemoryRepository>(Lifetime.Singleton); registrar.RegisterComponents(Lifetime.Default, typeof(RfidTagRegistrationHandler).Assembly); registrar.RegisterComponents(Lifetime.Singleton, typeof(DebugUserControlViewModel).Assembly); return registrar.Build(); }
private static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); registrar.RegisterService<ICommandDispatcher>(x => new ContainerDispatcher(x), Lifetime.Scoped); var container = registrar.Build(); using (var scope = container.CreateChildContainer()) { var createUser = new CreateUser("arne", "Arne Eriksson"); scope.Resolve<ICommandDispatcher>().Dispatch(createUser); } Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
public override void Prepare() { var registrar = new ContainerRegistrar(); registrar.RegisterType<ISingleton, Singleton>(Lifetime.Singleton); registrar.RegisterType<ITransient, Transient>(Lifetime.Transient); registrar.RegisterType<ICombined, Combined>(Lifetime.Transient); this.container = registrar.Build(); registrar = new ContainerRegistrar(); registrar.RegisterType<ICalculator, Calculator>(Lifetime.Transient); var containerWithLoggingInterception = registrar.Build(); containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator()); this.containerWithLoggingInterception = containerWithLoggingInterception; }
public override void Prepare() { var registrar = new ContainerRegistrar(); RegisterDummies(registrar); RegisterStandard(registrar); RegisterComplex(registrar); this.container = registrar.Build(); registrar = new ContainerRegistrar(); registrar.RegisterType<ICalculator, Calculator>(Lifetime.Transient); var containerWithLoggingInterception = registrar.Build(); containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator()); this.containerWithLoggingInterception = containerWithLoggingInterception; }
public void TestLoggingDecorator() { // register services var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<TotalFailure>(Lifetime.Transient); var container = registrar.Build(); // only log transient services var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient); var decorator = new ExceptionLoggerDecorator(this, filter); container.AddDecorator(decorator); // exception will be logged. var tmp = container.Resolve<TotalFailure>(); Assert.Throws<InvalidOperationException>(() => tmp.Fail("Big!")); Assert.IsType<InvalidOperationException>(_exception); }
public void TestDecorateHiearchy() { // register services var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Decorated1>(Lifetime.Transient); registrar.RegisterConcrete<Decorated2>(Lifetime.Transient); registrar.RegisterConcrete<Decorated3>(Lifetime.Transient); var container = registrar.Build(); // only log transient services var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient); var decorator = new ExceptionLoggerDecorator(this, filter); container.AddDecorator(decorator); // exception will be logged. var all = container.ResolveAll<IShouldBeDecorated>(); Assert.True(all.All(x => x.GetType().Name.Contains("Proxy"))); }
private static void Main(string[] args) { var registrar = new ContainerRegistrar(); // working with a "db". Let's scope everything per default. registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly()); var container = registrar.Build(); using (var scope = container.CreateChildContainer()) { var storage = scope.Resolve<IUserStorage>(); storage.Create("Arne"); } Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Hello>(Lifetime.Transient); registrar.RegisterConcrete<OurDependency>(Lifetime.Singleton); var container = registrar.Build(); // gets a new OurDependency var obj1 = container.Resolve<Hello>(); obj1.World(); // New Hello instance, but with the same OurDependency. var obj2 = container.Resolve<Hello>(); obj2.World(); Console.WriteLine(); Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
public override void Prepare() { var registrar = new ContainerRegistrar(); RegisterBasic(registrar); RegisterPropertyInjection(registrar); this.container = registrar.Build(); registrar = new ContainerRegistrar(); registrar.RegisterType<ICalculator1, Calculator1>(Lifetime.Transient); registrar.RegisterType<ICalculator2, Calculator2>(Lifetime.Transient); registrar.RegisterType<ICalculator3, Calculator3>(Lifetime.Transient); var containerWithLoggingInterception = registrar.Build(); containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator()); this.containerWithLoggingInterception = containerWithLoggingInterception; }
static void Main(string[] args) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete<Hello>(Lifetime.Singleton); //<-- singleton var container = registrar.Build(); // Will get var instance1 = container.Resolve<Hello>(); instance1.World(); // the same instance var instance2 = container.Resolve<Hello>(); instance2.World(); // every time = singleton var instance3 = container.Resolve<Hello>(); instance3.World(); Console.WriteLine("Press enter to quit"); Console.ReadLine(); }
/// <summary> /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address. /// </summary> /// <param name="serviceType">Specifies the type of service to host.</param> /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param> /// <returns> /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address. /// </returns> protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { if (serviceType == null) throw new ArgumentNullException("serviceType"); ServiceBehaviorAttribute attribute; var lifetime = GetLifetime(serviceType, out attribute); if (_container == null) { var registrar = new ContainerRegistrar(); registrar.RegisterConcrete(serviceType, lifetime); registrar.RegisterModules(serviceType.Assembly); _container = registrar.Build(); } var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses); if (ConfigurationCallback != null) ConfigurationCallback(host); return host; }