static void Main(string[] args) { //Instantiate UnityContainer IUnityContainer container = new Unity.UnityContainer(); //Register Type with Unity container.RegisterType <Language>(new InjectionConstructor("pt-br")); container.RegisterType <MovieLister>(new InjectionMethod("ConfigLanguage", container.Resolve <Language>())); //Resolve Language language = container.Resolve <Language>(); //Register Type with Unity container.RegisterType <IMovieFinder, ColonDelimitedMovieFinder>( new InjectionProperty("Path", "movies1.txt")); container.RegisterType <IMovieFinder, MemoryMovieFinder>("memory"); container.RegisterType <MovieLister>("default", new InjectionConstructor(container.Resolve <IMovieFinder>("memory"))); //Resolve MovieLister movieLister = container.Resolve <MovieLister>("default"); Console.WriteLine($"Language {language.Type}"); string director = "Guillermo del Toro"; foreach (Movie movie in movieLister.MoviesDirectedBy(director)) { Console.WriteLine($"Movie {movie.Title}"); } }
private static IUnityContainer InitialiseUnityContainer() { var container = new Unity.UnityContainer(); RegisterServiceClients(container); RegisterTelemetryClient(container); container.RegisterType <IRecommendedService, RecommendedService>(); container.RegisterType <IDealerService, DealerService>( new InjectionConstructor(container.Resolve <IRestfulClient>(DealerConfigurationApiClient))); container.RegisterType <ICDKAutolineService, CDKAutolineService>( new InjectionConstructor(container.Resolve <IRestfulClient>(CdkAutolineApiClient), container.Resolve <TelemetryClient>())); return(container); }
public void IfAServiceIsNotRegisteredTryUsingAlternativeContainer() { UnityContainer container = new UnityContainer(); container.AlternativeServiceProvider = defaultServiceProvider; Assert.NotNull(defaultContainer.Resolve<ISample>()); Assert.IsType<SampleClassA>(defaultContainer.Resolve<ISample>()); Assert.IsNotType<SampleClassB>(defaultContainer.Resolve<ISample>()); // Now with the other container Assert.NotNull(container.Resolve<ISample>()); Assert.IsType<SampleClassA>(container.Resolve<ISample>()); Assert.IsNotType<SampleClassB>(container.Resolve<ISample>()); }
private static void Run_UsingUnityDI(string[] args) { using (var container = new Unity.UnityContainer().EnableDiagnostic()) { string environment = Settings.Environment(); string notificationRecipientList = Settings.NotificationRecipientList(); string sendEmailGateway = Settings.SendEmailGateway(); // -- httpclientfactory Microsoft.Extensions.DependencyInjection.ServiceCollection serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddHttpClient("Sender", (c) => { c.BaseAddress = new Uri(sendEmailGateway); }); serviceCollection.BuildServiceProvider(container); //var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); IHttpClientFactory clientFactory = container.Resolve <IHttpClientFactory>(); HttpClient httpClient = clientFactory.CreateClient("Sender"); container.RegisterSingleton <IConfig, AppConfig>( new InjectionProperty("Environment", environment) , new InjectionProperty("NotificationRecipientList", notificationRecipientList) , new InjectionProperty("SendEmailGateway", sendEmailGateway) ); container.RegisterSingleton <ILogger, Logger>(); //Sender(IConfig config, IHttpClientFactory httpClientFactory, ILogger logger) /* * container.RegisterSingleton<ISender, Sender>( * new InjectionConstructor( * new ResolvedParameter(typeof(IConfig)) * , new ResolvedParameter(typeof(IHttpClientFactory)) * , new ResolvedParameter(typeof(ILogger)) * ) * );*/ container.RegisterSingleton <ISender, Sender>(); container.RegisterType(typeof(RemoteAppenderSink)); var sink = container.Resolve <RemoteAppenderSink>(); //sink.EventsReached += (s, a) => AddLog(a.LoggingEvents); RemotingConfiguration.Configure("log4netListener.exe.config", false); RemotingServices.Marshal(sink, "RemoteAppenderLoggingSink"); } }
public static void Main(string[] args) { using (var container = new Unity.UnityContainer()) { container.AddNewExtension <Interception>(); container.RegisterType(typeof(IDataContainer <string>), typeof(DataContainer) , new Interceptor <InterfaceInterceptor>() , new InterceptionBehavior <LoggingAspect>()); container.RegisterType(typeof(IDataManager <string>), typeof(DataManager) , new Interceptor <InterfaceInterceptor>() , new InterceptionBehavior <LoggingAspect>()); container.RegisterType <IProcessor, Processor>( new Interceptor <InterfaceInterceptor>() , new InterceptionBehavior <LoggingAspect>()); var processor = container.Resolve <IProcessor>(); var result = processor.DoWork(); } /* * Processor processor = DependencyInjector.Retrieve<Processor>(); * var result = processor.DoWork(); */ }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); using (var container = new Unity.UnityContainer().EnableDiagnostic()) { string connectionInfo = @"Data Source=12.11.10.1\DATA;Initial Catalog=DATA;User ID=Data;Password=Data;Application Name=UI.NET"; string userName = "******"; container.RegisterType <IConfig, Config>(new InjectionProperty("ConnectionInfo", connectionInfo), new InjectionProperty("UserName", userName)); // -- DataAccess - container.RegisterType <IDataAccess, DataAccess.DataAccess>(); // // -- DataStore: container.RegisterType <IDataStore, DataStoreManagemer>(); // // container.RegisterType <IViewContainer, UserInfoViewContainer>(); // container.RegisterType <IView, formUserInfo>(new InjectionConstructor(typeof(UserInfoViewContainer))); // var ctorMainPresentation = new InjectionConstructor(typeof(formUserInfo)); container.RegisterType <IPresentation, MainPresentation>(ctorMainPresentation); var obj = container.Resolve <MainPresentation>(); obj.Populate().GetAwaiter().GetResult(); obj.Show(); } }
/// <summary> /// /// </summary> /// <returns></returns> public IUser GetUserByAppConfiguration() { Unity.IUnityContainer unityContainer = new Unity.UnityContainer(); unityContainer.LoadConfiguration("unityContainer"); Strategy.IUser user = unityContainer.Resolve <Strategy.IUser>(); user = user.GetUserById(); return(user); }
private static async Task Run(string[] args) { using (var container = new Unity.UnityContainer().EnableDiagnostic()) { container.RegisterSingleton <ILogger, Logger>(); Application.Run(container.Resolve <Form1>()); } }
/// <summary> /// /// </summary> /// <returns></returns> public Strategy.IUser GetUser() { //IOC容器 Unity.UnityContainer unityContainer = new Unity.UnityContainer(); unityContainer.RegisterType(typeof(Strategy.IUser), typeof(Domain.User)); Strategy.IUser user = unityContainer.Resolve <Strategy.IUser>(); return(user); }
public static void Init() { CosturaUtility.Initialize(); XQMain.Register(unityContainer); XQApi.Api_OutPutLog($"依赖注入{unityContainer.Registrations.Count()}"); XQEvent.Event_GroupMsgHandler += unityContainer.Resolve <IXQGroupMessage>().GroupMessage; }
static void Main(string[] args) { IUnityContainer container = new Unity.UnityContainer(); UnityContainer.RegisterElements(container); IBattery battery = container.Resolve <IBattery>(); Console.WriteLine(battery.SerialNumber()); ITuner tuner = container.Resolve <ITuner>(); Console.WriteLine(tuner.SerialNumber()); ICasing casing = container.Resolve <ICasing>(); Console.WriteLine(casing.TypeOfMaterial()); Console.ReadLine(); }
static void Main(string[] args) { var container = new Unity.UnityContainer(); container.RegisterType <ICar, BMW>(); container.RegisterType <ICar, Audi>("LuxuryCar"); // Registers Driver type container.RegisterType <Driver>("LuxuryCarDriver", new InjectionConstructor(container.Resolve <ICar>("LuxuryCar"))); Driver driver1 = container.Resolve <Driver>();// injects BMW driver1.RunCar(); Driver driver2 = container.Resolve <Driver>("LuxuryCarDriver");// injects Audi driver2.RunCar(); Console.WriteLine("Hello World!"); Console.ReadLine(); }
public async Task <bool> ProcessXmlProducts(int sellerId) { var seller = await db.tblSellers.Where(x => x.SellerId == sellerId).FirstOrDefaultAsync(); var container = new Unity.UnityContainer(); var sst = container.Resolve <ImportService.Service.Service>(); var xmlData = sst.ReadCSV(sellerId); var colorMappingList = sst.GetColorFamily("ColorMapping/COLOR_MAPPING.xml"); var result = false; var vendor = xmlData.ProductAttributeGroups.ProductAttributeGroup.ProductAttributeGroupCode; var plist = xmlData.Products.Product; List <ImportService.Wrapper.Product> successList = new List <ImportService.Wrapper.Product>(); List <ImportService.Wrapper.Product> errorList = new List <ImportService.Wrapper.Product>(); foreach (var p in plist) { try { var _p = await CreateProduct(p, vendor, seller.MyShopifyDomain, seller.ShopifyAccessToken, colorMappingList); successList.Add(p); } catch (Exception ex) { errorList.Add(p); result = true; Log.Error("Error in ProcessXmlProducts method", ex, vendor); } } if (successList.Count > 0) { xmlData.Products.Product = new List <ImportService.Wrapper.Product>(); xmlData.Products.Product.AddRange(successList); var sSetting = seller.tblSchedulerSettings.FirstOrDefault(); var filename = seller.MyShopifyDomain; sst.CreateFileSFTP(sSetting.FtpHost, sSetting.FtpUserName, sSetting.FtpPassword, sSetting.FtpFilePath, "success", filename, xmlData); } if (errorList.Count > 0) { xmlData.Products.Product = new List <ImportService.Wrapper.Product>(); xmlData.Products.Product.AddRange(errorList); var sSetting = seller.tblSchedulerSettings.FirstOrDefault(); var filename = seller.MyShopifyDomain; sst.CreateFileSFTP(sSetting.FtpHost, sSetting.FtpUserName, sSetting.FtpPassword, sSetting.FtpFilePath, "error", filename, xmlData); } return(result); }
static void Main(string[] args) { Unity.IUnityContainer container = new Unity.UnityContainer(); ContainerMagic.RegisterElements(container); var battery = container.Resolve <IBattery>(); Console.WriteLine($"Battery object: SerialNumber - {battery.SerialNumber()}"); var tuner = container.Resolve <ITuner>(); Console.WriteLine($"Tuner object: SerialNumber - {tuner.SerialNumber()}"); var dial = container.Resolve <Dial>(); Console.WriteLine($"Dial object: DialType - {dial.DialType()}"); var radio = container.Resolve <IRadio>(new Unity.Resolution.ParameterOverride("radioBattery", battery), new Unity.Resolution.ParameterOverride("radioTuner", tuner), new Unity.Resolution.ParameterOverride("radioName", "RocketRadio")); radio.Start(); var cheapSpeaker = container.Resolve <ISpeaker>("Cheap"); var priceySpeaker = container.Resolve <ISpeaker>("Expensive"); cheapSpeaker.Start(); priceySpeaker.Start(); Console.ReadLine(); }
static void Main(string[] args) { var container = new Unity.UnityContainer(); container.RegisterSingleton <IStringHelper, StringHelper>(); var newUsers = container.Resolve <Users>(); Console.WriteLine(newUsers.FormatUserName("cnf")); // Unity.Injection. Console.ReadKey(); }
private static IUnityContainer InitialiseUnityContainer() { var container = new Unity.UnityContainer(); RegisterAutoMapper(container); RegisterServiceClients(container); container.RegisterType <ICustomerVehicleClient, CustomerVehicleClient>( new InjectionConstructor(container.Resolve <IRestfulClient>(Constants.CustomerVehicleAPI), container.Resolve <IMapper>())); container.RegisterType <IDealerConfigurationClient, DealerConfigurationClient>( new InjectionConstructor(container.Resolve <IRestfulClient>(Constants.DealerConfigurationAPI))); container.RegisterType <IEmailProcessor, EmailProcessor>(); container.RegisterType <IExchangeServices, ExchangeServices>("ExchangeServices", new ContainerControlledLifetimeManager(), new InjectionConstructor( ExchangeVersion, ExchangeUserName, ExchangePassword, ExchangeUrl )); container.RegisterType <IMailService, MailService>( new InjectionConstructor(container.Resolve <IExchangeServices>("ExchangeServices"))); container.RegisterType <ICsvProcessor, CsvProcess>(new InjectionConstructor(GetCsvColumnNames())); container.RegisterType <IAzureServices, AzureServices>( new ContainerControlledLifetimeManager(), new InjectionConstructor( AzureFileConnectionString, AzureFileShareName, AzureFileProcessedFolderName, AppendDateFormatInFileName )); container.RegisterType <IAzureFileProcessor, AzureFileProcessor>( new InjectionConstructor(container.Resolve <IAzureServices>())); container.RegisterType <IFileContentService, FileContentService>( new InjectionConstructor(container.Resolve <IEmailProcessor>(), container.Resolve <IAzureFileProcessor>())); return(container); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var container = new Unity.UnityContainer(); var program = new Program(); program.Setup(container); var application = container.Resolve <Application.ApplicationViewModel>(); program.Configure(application); application.Start(); while (application.DoContinue().Result) { } }
public static void Init() { CosturaUtility.Initialize(); XQMain.Register(unityContainer); XQDLL.Api_OutPutLog($"事件依赖注入...{unityContainer.Registrations.Count()}"); if (unityContainer.IsRegistered <IXQGroupMessage>()) { XQEvent.Event_GroupMsgHandler += unityContainer.Resolve <IXQGroupMessage>().GroupMessage; } if (unityContainer.IsRegistered <IXQPrivateMessage>()) { XQEvent.Event_PrivateMsgHandler += unityContainer.Resolve <IXQPrivateMessage>().PrivateMessage; } if (unityContainer.IsRegistered <IXQAppEnable>()) { XQEvent.Event_AppEnableHandler += unityContainer.Resolve <IXQAppEnable>().AppEnable; } if (unityContainer.IsRegistered <IXQAppDisable>()) { XQEvent.Event_AppDisableHandler += unityContainer.Resolve <IXQAppDisable>().AppDisable; } if (unityContainer.IsRegistered <IXQAddGroup>()) { XQEvent.Event_AddGroupHandler += unityContainer.Resolve <IXQAddGroup>().AddGroup; } if (unityContainer.IsRegistered <IXQAddFriend>()) { XQEvent.Event_AddFriendHandler += unityContainer.Resolve <IXQAddFriend>().AddFriend; } if (unityContainer.IsRegistered <IXQCallMenu>()) { XQEvent.Event_CallMenu += unityContainer.Resolve <IXQCallMenu>().CallMenu; } ; }
public async Task SyncProducts(int sellerId) { try { var container = new Unity.UnityContainer(); var _importService = container.Resolve <ImportService.Service.Service>(); var seller = await db.tblSchedulerSettings.Where(x => x.SellerId == sellerId).FirstOrDefaultAsync(); var downloadResult = _importService.DownloadFileSFTP(seller.FtpHost, seller.FtpUserName, seller.FtpPassword, seller.FtpFilePath, sellerId); if (downloadResult.Item1 == true) { await ProcessXmlProducts(sellerId); } } catch (Exception ex) { Log.Error("Error in SyncProducts method for sellerId = " + sellerId + " in class ProductService.cs ", ex); } }
private static IUnityContainer InitialiseUnityContainer() { var container = new Unity.UnityContainer(); RegisterTelemetryClient(container); RegisterServiceClients(container); RegisterAutoMapper(container); RegisterSendGridClient(container); container.RegisterType <ICustomerVehicleDAL, CustomerVehicleDAL>( new InjectionConstructor(DatabaseConnectionString)); container.RegisterType <ICustomerServiceBooking, CustomerServiceBooking>( new InjectionConstructor(container.Resolve <IRestfulClient>(Constants.CDKAutolineAPI), container.Resolve <TelemetryClient>())); container.RegisterType <ISMSGatewayClient, PlivoGatewayClient>(new ContainerControlledLifetimeManager(), new InjectionConstructor(PilvoAuthId, PilvoAuthToken)); container.RegisterType <IEmailGatewayClient, EmailGatewayClient>(new ContainerControlledLifetimeManager(), new InjectionConstructor(container.Resolve <ISendGridClient>())); container.RegisterType <IDealerConfigurationService, DealerConfigurationService>( new InjectionConstructor(container.Resolve <IRestfulClient>(Constants.DealerConfigurationAPI))); container.RegisterType <ICustomerInvitationService, CustomerInvitationService>( new InjectionConstructor(ServiceBookingAppUrl, Convert.ToInt32(InvitationExpiredDays), InvitationFromPhoneNumber, container.Resolve <ISMSGatewayClient>(), container.Resolve <IEmailGatewayClient>(), container.Resolve <TelemetryClient>(), container.Resolve <IDealerConfigurationService>(), ServiceBookingUrlPlaceHolder, RegistrationNoPlaceHolder )); container.RegisterType <IEmailService, EmailService>( new InjectionConstructor(container.Resolve <IEmailGatewayClient>(), ServiceBookingEmail)); container.RegisterType <ICustomerRegistrationService, CustomerRegistrationService>( new InjectionConstructor(container.Resolve <IMapper>(), container.Resolve <IRestfulClient>(Constants.CDKAutolineAPI), container.Resolve <TelemetryClient>())); container.RegisterType <IValidateRequest, ValidateRequest>(); container.RegisterType <ICustomerVehicleService, Service.Implementation.CustomerVehicleService>( new InjectionConstructor(container.Resolve <ICustomerVehicleDAL>(), container.Resolve <IMapper>(), container.Resolve <ICustomerRegistrationService>(), container.Resolve <ICustomerInvitationService>(), container.Resolve <IEmailService>(), container.Resolve <ICustomerServiceBooking>(), Convert.ToInt32(ServiceBookingExpiredDays))); return(container); }
private static IUnityContainer InitialiseUnityContainer() { var container = new Unity.UnityContainer(); RegisterTelemetryClient(container); RegisterAutoMapper(container); container.RegisterType <IAppTokenDAL, AppTokenDAL>(new InjectionConstructor(new CDKAutolineContext(ConnectionString))); container.RegisterType <IEncryptionService, RijndaelEncryptionService>(new InjectionConstructor(SecretKey)); container.RegisterType <IValidateRequest, ValidateRequest>(); container.RegisterType <IRestApiClient, RestApiClient>(); container.RegisterType <IPasswordService, PasswordService>( new InjectionConstructor(Convert.ToInt32(PasswordLength), PasswordCharacters)); container.RegisterType <IEncryptedTokenCodeService, EncryptedTokenCodeService>( new InjectionConstructor(UnregisteredGuid, container.Resolve <IEncryptionService>())); container.RegisterType <ICdkCustomerDAL, CdkCustomerDAL>( new InjectionConstructor(new CDKAutolineContext(ConnectionString))); container.RegisterType <IDealerCDKConfigurationsDAL, DealerCDKConfigurationsDAL>( new InjectionConstructor(new CDKAutolineContext(ConnectionString))); container.RegisterType <ICdkCustomerService, CdkCustomerService>(); container.RegisterType <ICustomerService, CustomerService>( new InjectionConstructor(container.Resolve <IRestApiClient>(), CdkAutolineUrl, container.Resolve <IEncryptedTokenCodeService>())); container.RegisterType <ITokenService, TokenService>( new InjectionConstructor(container.Resolve <IRestApiClient>(), container.Resolve <IEncryptedTokenCodeService>(), CdkAutolineUrl, UnregisteredGuid, container.Resolve <IAppTokenDAL>(), container.Resolve <TelemetryClient>(), container.Resolve <ICdkCustomerService>(), container.Resolve <ICustomerService>(), container.Resolve <IDealerCDKConfigurationsDAL>())); container.RegisterType <ICDKAutolineServices, CDKAutolineServices>(); container.RegisterType <ICDKVehicleMaintenanceService, CDKVehicleMaintenanceService>( new InjectionConstructor(container.Resolve <IRestApiClient>(), CdkAutolineUrl, container.Resolve <ITokenService>(), container.Resolve <TelemetryClient>())); container.RegisterType <ICDKBookingService, CDKBookingService>( new InjectionConstructor(container.Resolve <IRestApiClient>(), container.Resolve <ITokenService>(), container.Resolve <ICdkCustomerDAL>(), container.Resolve <TelemetryClient>(), CdkAutolineUrl)); container.RegisterType <ICDKServiceAdvisors, CDKServiceAdvisors>( new InjectionConstructor(container.Resolve <IRestApiClient>(), container.Resolve <ITokenService>(), container.Resolve <ICdkCustomerDAL>(), container.Resolve <TelemetryClient>(), CdkAutolineUrl)); container.RegisterType <ICDKAppointmentSlotsService, CDKAppointmentSlotsService>( new InjectionConstructor(container.Resolve <IRestApiClient>(), container.Resolve <ICdkCustomerDAL>(), container.Resolve <ITokenService>(), container.Resolve <TelemetryClient>(), container.Resolve <IValidateRequest>(), container.Resolve <IMapper>(), CdkAutolineUrl)); return(container); }
protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) { var container = new UnityContainer(); Configuration.Register(serviceCollection, container); return container.Resolve<IServiceProvider>(); }
public IUserController Run() { return(_container.Resolve <IUserController>()); }