static async Task Main(string[] args) { var wrapper = UnityServiceContainerWrapper.GetInstance(); DependencyRegistration.RegisterDependencies(wrapper); var backupManager = wrapper.Resolve <IBackupManager>(); }
public static void InitializeContainer() { var builder = new ContainerBuilder(); DependencyRegistration.Register(builder, configuration); Container = builder.Build(); }
public IDependencyBindingScopeSetup BindToType(Type target, Type implementationType) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (implementationType == null) { throw new ArgumentNullException(nameof(implementationType)); } if (HasBindingFor(target)) { throw new InvalidOperationException($"Target type {target.Name} is already bound."); } DependencyRegistration reg = DependencyRegistration.BindToType(target, asImplementation: implementationType, scope: DependencyScope.Transient); mDependencyRegistrations.Add(reg); return(new StandardDependencyBindingScopeSetup(reg)); }
public void Test_CanLoad_AndResolve_BindToType(int nTestThreads) { StandardDependencyResolver resolver = new StandardDependencyResolver(); List <DependencyRegistration> dependencies = new List <DependencyRegistration>(); dependencies.Add(DependencyRegistration.BindToType(typeof(IAsSingletonSampleDependency), typeof(AsSingletonSampleDependencyImpl), DependencyScope.Singleton)); dependencies.Add(DependencyRegistration.BindToType(typeof(IAsThreadSingletonSampleDependency), typeof(AsThreadSingletonSampleDependencyImpl), DependencyScope.Thread)); dependencies.Add(DependencyRegistration.BindToType(typeof(IAsTransientSampleDependency), typeof(AsTransientSampleDependencyImpl), DependencyScope.Transient)); resolver.Load(dependencies); Assert_DependenciesCanBeResolved(resolver); Assert_DependenciesCorrectlyResolved(nTestThreads, resolver); }
public static TinyIoCContainer.RegisterOptions WithScopeFromRegistrationInfo(this TinyIoCContainer.RegisterOptions regOpts, DependencyRegistration registration) { if (regOpts == null) { throw new ArgumentNullException(nameof(regOpts)); } if (registration == null) { throw new ArgumentNullException(nameof(registration)); } switch (registration.Scope) { case DependencyScope.Singleton: regOpts.AsSingleton(); break; case DependencyScope.Thread: regOpts.AsPerRequestSingleton(); break; case DependencyScope.Transient: if (!registration.IsProviderRegistration) { regOpts.AsMultiInstance(); } break; } return(regOpts); }
public IDependencySetup BindToType(Type target, Type implementationType, DependencyScope scope) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (implementationType == null) { throw new ArgumentNullException(nameof(implementationType)); } if (HasBindingFor(target)) { throw new InvalidOperationException($"Target type {target.Name} is already bound."); } mDependencyRegistrations.Add(DependencyRegistration.BindToType(target, asImplementation: implementationType, scope: scope)); return(this); }
private ImmutableArray <ITypeSymbol> GetTypesToInspect(DependencyRegistration registration) { // if we have a concrete type, use it if (!registration.ConcreteType.IsNullOrErrorType()) { return(ImmutableArray.Create(registration.ConcreteType)); } // if we have a dynamically generated objectfactory, use its constructor arguments if (!registration.DynamicObjectFactoryType.IsNullOrErrorType()) { if (registration.ObjectScope != ObjectScope.Singleton) { // non-singleton dynamic object factories cannot be marked [Singleton] // and it is ok for them to depend on singletons so nothing to check return(ImmutableArray <ITypeSymbol> .Empty); } ImmutableArray <ITypeSymbol> dependencies; if (TryGetDependenciesFromConstructor(registration.DynamicObjectFactoryType, out dependencies)) { return(dependencies); } // this is a dynamic object factory, but either // (1) there is no public constructor, or // (2) one of the parameter's types doesn't exist // in all cases, fall back to dependency type } // other use the dependency type return(ImmutableArray.Create(registration.DependencyType)); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { DependencyRegistration.ConfigureServices(services); services.AddTransient <ITokenHelper, JwtHelper>(); services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "SampleBank.API", Version = "v1" }); }); var tokenOptions = Configuration.GetSection("TokenOptions").Get <TokenOptions>(); services.AddAuthentication(option => { option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = false, ValidateIssuerSigningKey = true, ValidIssuer = tokenOptions.Issuer, ValidAudience = tokenOptions.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenOptions.SecurityKey)) }; }); }
public void Register() { var dependencyContainer = new Mock <IDependencyContainer>(); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(dependencyContainer.Object); dependencyContainer.Verify(mock => mock.Bind <IMasterIndexService, MasterIndexService>(), Times.Once); }
protected override void AddDependencyInstanceCore(Type serviceType, object instance, DependencyLifetime lifetime) { var instanceType = instance.GetType(); var registration = new DependencyRegistration(serviceType, instanceType, _lifetimeManagers[lifetime], instance); Registrations.Add(registration); }
// Register dependencies directly to autofac public void ConfigureContainer(ContainerBuilder builder) { // Register your own things directly with Autofac here. Don't // call builder.Populate(), that happens in AutofacServiceProviderFactory // for you. builder.RegisterAutoMapper(typeof(NodeProfile).Assembly); DependencyRegistration.Register(builder, Configuration); }
public void Configuration(IAppBuilder app) { ConfigureAuth(app); var container = new Container(); DependencyRegistration.RegisterDependencies(container); }
public static void Main() { DependencyRegistration.Register(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); WindsorContainer.Dispose(); }
public void Register() { var dependencyContainer = new Mock <IDependencyContainer>(); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(dependencyContainer.Object); dependencyContainer.Verify(mock => mock.Bind <ITemplateProvider, TemplateProvider>(), Times.Once); }
public static List <DependencyRegistration> GetAll() { return(new List <DependencyRegistration>() { DependencyRegistration.BindToInstance(typeof(ISampleExecutorDependency), new SampleExecutorDependencyImpl()) }); }
public override object Resolve(ResolveContext context, DependencyRegistration registration) { if (registration.RegistrationKind == RegistrationKind.FactoryFunction) { return registration.FactoryFunction(context.Resolver); } return context.Builder.CreateObject(registration); }
public void Register() { var dependencyContainer = new Mock <IDependencyContainer>(); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(dependencyContainer.Object); //dependencyContainer.Verify(mock => mock.Bind<IServiceIdentityProvider, ApplicationServiceIdentityProvider>()); dependencyContainer.Verify(mock => mock.Bind <IServiceAuthorizationPolicy, ApplicationServiceAuthorizationPolicy>()); dependencyContainer.Verify(mock => mock.Bind <EmailNotificationService, EmailNotificationService>()); }
public void Register() { var dependencyContainer = new Mock <IDependencyContainer>(); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(dependencyContainer.Object); dependencyContainer.Verify(mock => mock.Bind <ISummaryQueueService, SummaryQueueService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IReportQueueService, ReportQueueService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IWorkflowAuditService, WorkflowAuditService>(), Times.Once); }
public IDependencySetup BindToInstance <T> (T instance) { if (HasBindingFor <T>()) { throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound."); } mDependencyRegistrations.Add(DependencyRegistration.BindToInstance(typeof(T), asInstance: instance)); return(this); }
protected override void RegisterDependencies(IServiceCollection collection) { DependencyRegistration.ConfigureStaticDependencies(collection); // Mocked external dependencies, the setup should // come later according to the scenarios repository = Substitute.For <IMenuRepository>(); applicationEventPublisher = Substitute.For <IApplicationEventPublisher>(); collection.AddTransient(IoC => repository); collection.AddTransient(IoC => applicationEventPublisher); }
private void InspectRegistration(DependencyRegistration registration, SyntaxNodeAnalysisContext ctx) { if (registration.ObjectScope == ObjectScope.Singleton) { var typesToInspect = GetTypesRequiredToBeImmutableForSingletonRegistration(registration); foreach (var type in typesToInspect) { // We require full immutability here, // because we don't know if it's a concrete type // // TODO: could make this better by exposing the minimum // scope required for each type var immutabilityScope = type.GetImmutabilityScope(); if (!type.IsNullOrErrorType() && immutabilityScope != ImmutabilityScope.SelfAndChildren) { var diagnostic = GetUnsafeSingletonDiagnostic( ctx.Compilation.Assembly, ctx.Node, type ); ctx.ReportDiagnostic(diagnostic); } } } // ensure webrequest isn't marked Singleton var isMarkedSingleton = registration.DependencyType.IsTypeMarkedSingleton(); if (isMarkedSingleton && registration.ObjectScope != ObjectScope.Singleton) { var diagnostic = Diagnostic.Create( Diagnostics.AttributeRegistrationMismatch, ctx.Node.GetLocation(), registration.DependencyType.GetFullTypeNameWithGenericArguments() ); ctx.ReportDiagnostic(diagnostic); } if (TryGetInstantiatedTypeForRegistration(registration, out ITypeSymbol instantiatedType)) { if (!TryGetInjectableConstructor(instantiatedType, out IMethodSymbol injectableConstructor)) { var diagnostic = Diagnostic.Create( Diagnostics.DependencyRegistraionMissingPublicConstructor, ctx.Node.GetLocation(), instantiatedType.GetFullTypeNameWithGenericArguments() ); ctx.ReportDiagnostic(diagnostic); } } }
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService <Worker>(); var builder = new ConfigurationBuilder().SetBasePath(Path.Combine(AppContext.BaseDirectory)) .AddJsonFile("appsettings.json", optional: true); var configuration = builder.Build(); DependencyRegistration.Register(configuration, services); });
public IDependencySetup BindToType <T, TImplementation> (DependencyScope scope) where TImplementation : T { if (HasBindingFor <T>()) { throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound."); } mDependencyRegistrations.Add(DependencyRegistration.BindToType(typeof(T), asImplementation: typeof(TImplementation), scope: scope)); return(this); }
public IDependencySetup BindToProviderInstance <T, TImplementation> (IDependencyProvider <TImplementation> implementationProvider, DependencyScope scope) where TImplementation : T { if (HasBindingFor <T>()) { throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound."); } mDependencyRegistrations.Add(DependencyRegistration.BindToProviderInstance(typeof(T), asProviderInstance: implementationProvider, scope: scope)); return(this); }
public IDependencyBindingScopeSetup BindToType <T, TImplementation> () where TImplementation : T { if (HasBindingFor <T>()) { throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound."); } DependencyRegistration reg = DependencyRegistration.BindToType(typeof(T), asImplementation: typeof(TImplementation), scope: DependencyScope.Transient); mDependencyRegistrations.Add(reg); return(new StandardDependencyBindingScopeSetup(reg)); }
public void Register() { var dependencyContainer = new Mock <IDependencyContainer>(); var dependencyRegistration = new DependencyRegistration(); dependencyRegistration.Register(dependencyContainer.Object); dependencyContainer.Verify(mock => mock.Bind <IArrestReportQueryService, ArrestReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IArrestReportCommandService, ArrestReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IArrestSummaryQueryService, ArrestSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IArrestSummaryCommandService, ArrestSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IFieldInterviewSummaryQueryService, FieldInterviewSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IFieldInterviewSummaryCommandService, FieldInterviewSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IFieldInterviewReportQueryService, FieldInterviewReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IFieldInterviewReportCommandService, FieldInterviewReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IIncidentReportQueryService, IncidentReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IIncidentReportCommandService, IncidentReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IIncidentSummaryQueryService, IncidentSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IIncidentSummaryCommandService, IncidentSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICitationReportQueryService, CitationReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICitationReportCommandService, CitationReportCommandServiceBase>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICitationSummaryQueryService, CitationSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICitationSummaryCommandService, CitationSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICallForServiceReportQueryService, CallForServiceReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICallForServiceReportCommandService, CallForServiceReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICallForServiceSummaryQueryService, CallForServiceSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICallForServiceSummaryCommandService, CallForServiceSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IOtherEventReportQueryService, OtherEventReportQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IOtherEventReportCommandService, OtherEventReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IOtherEventSummaryQueryService, OtherEventSummaryQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IOtherEventSummaryCommandService, OtherEventSummaryCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICodeValueQueryService, CodeValueQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICaseQueryService, CaseQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICaseCommandService, CaseCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <ICaseNavigationService, CaseNavigationService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IReportWorkflowCommandService, ReportWorkflowCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IReportCommentCommandService, ReportCommentCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IReportCommentQueryService, ReportCommentQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IAttachmentCommandService, AttachmentCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IAttachmentQueryService, AttachmentQueryService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <INumberControlService, NumberControlService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IReportQueryService, ReportQueryService>(), Times.AtLeastOnce); dependencyContainer.Verify(mock => mock.Bind <ISummaryQueryService, SummaryQueryService>(), Times.AtLeastOnce); dependencyContainer.Verify(mock => mock.Bind <IRelatedToService, RelatedToService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IDeleteService, DeleteService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IEmailAttachmentGenerator, SSRSEmailAttachment>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IAttachmentReportCommandService, AttachmentReportCommandService>(), Times.Once); dependencyContainer.Verify(mock => mock.Bind <IDocumentService, DocumentService>(), Times.Once); }
public IDependencyBindingScopeSetup BindToProvider <T, TImplementation, TProvider> (TImplementation instance) where TImplementation : T where TProvider : IDependencyProvider <TImplementation> { if (HasBindingFor <T>()) { throw new InvalidOperationException($"Target type {typeof( T ).Name} is already bound."); } DependencyRegistration reg = DependencyRegistration.BindToProvider(typeof(T), asProvider: typeof(TProvider), scope: DependencyScope.Transient); mDependencyRegistrations.Add(reg); return(new StandardDependencyBindingScopeSetup(reg)); }
public static void Main(string[] args) { try { DependencyRegistration.Register(); Console.WriteLine("Press {enter} to close server."); Console.ReadLine(); } catch (Exception exception) { Console.WriteLine( $"An exception was thrown. Details:{Environment.NewLine}{Environment.NewLine}{exception}{Environment.NewLine}"); Console.WriteLine("Press {enter} to close server."); Console.ReadLine(); } }
/// <summary> /// Gets the type that will be actually instantiated by DI for the registration. /// This is the factory type for registrations like RegisterFactory, or the concrete type otherwise /// </summary> /// <param name="registration"></param> /// <param name="instantiatedType"></param> /// <returns></returns> private bool TryGetInstantiatedTypeForRegistration(DependencyRegistration registration, out ITypeSymbol instantiatedType) { if (registration.IsInstanceRegistration) { instantiatedType = null; return(false); } ITypeSymbol type = registration.FactoryType ?? registration.ConcreteType; if (type.IsNullOrErrorType()) { instantiatedType = null; return(false); } // hacks // handle RegisterSubInterface<IFoo, IFooBar>(): // IFooBar is in the registration as "concrete type" and don't want to deal with that refactoring if (type.TypeKind == TypeKind.Interface) { instantiatedType = null; return(false); } // ignore registry usage in registry extensions we don't know about // public static void Foo<T, U>( this IDependencyRegistry @this, ObjectScope scope ) where U : T { // // Can't find constructor for U! // @this.Register<T, U>( scope ); // } if (type.TypeKind == TypeKind.TypeParameter) { instantiatedType = null; return(false); } // Register( typof( IFoo<> ), typeof( Foo<> ), ObjectScope.Singleton ) // Turn Foo<> into Foo<T> if (type is INamedTypeSymbol namedType) { type = namedType.ConstructedFrom; } instantiatedType = type; return(true); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMemoryCache(); services.AddDbContext <PgsKanbanContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); IdentityConfiguration.AddIdentity(services); SwaggerConfiguration.ConfigureSwagger(services); SignalRConfiguration.AddSignalR(services); MvcConfiguration.AddMvc(services); IdentityConfiguration.ConfigureIdentity(services); CorsConfiguration.AddCors(services); OptionsRegistration.Configure(Configuration, services); services.AddSingleton <IMapper>(sp => AutoMapperConfig.Initialize()); JwtConfiguration.AddJwtAuthorization(Configuration, services); return(DependencyRegistration.Register(Configuration, services)); }
/// <summary> /// Returns the concrete implementation of the interface type represented by the generic type parameter T. /// </summary> /// <typeparam name="T">The type of object to instantiate and return.</typeparam> /// <returns>The type of object represented by the generic type parameter T.</returns> public T Resolve <T>() where T : class { DependencyRegistration registration = registeredDependencies.GetRegistration <T>(); if (registration == null) { return(null); } ConcreteImplementation concreteImplementation = registration.ConcreteImplementations.First(); if (concreteImplementation.ConstructorParameters == null) { return(Activator.CreateInstance(concreteImplementation.Type) as T); } else { return(Activator.CreateInstance(concreteImplementation.Type, concreteImplementation.ConstructorParameters) as T); } }
public wire_up_the_data_access_components_into_the(DependencyRegistration registry) { register = registry; }
public wire_up_the_container(DependencyRegistration registry) { this.registry = registry; }
public auto_wire_components_in_to_the(DependencyRegistration registrar) : this(registrar, new ComponentExclusionSpecificationImplementation()) {}
public auto_wire_components_in_to_the(DependencyRegistration registration, ComponentExclusionSpecification exclusion_policy) { registrar = registration; this.exclusion_policy = exclusion_policy; }
public bool Equals(DependencyRegistration other) { return string.Equals(Property, other.Property) && string.Equals(DependsOn, other.DependsOn); }
public wire_up_the_essential_services_into_the(DependencyRegistration registration) { this.registration = registration; }
public wire_up_the_reports_in_to_the(DependencyRegistration registry) { this.registry = registry; }
public wire_up_the_views_in_to_the(DependencyRegistration registry) { register = registry; }
public wire_up_the_services_in_to_the(DependencyRegistration registry) { this.registry = registry; }
public wire_up_the_infrastructure_in_to_the(DependencyRegistration registry) { this.registry = registry; }