static void Main(string[] args)
        {
            // basic constructor injection
            var container = new UnityContainer();
            container.RegisterType<IWeapon, Sword>();
            var samurai = container.Resolve<Samurai>();

            samurai.Attack("the evildoers");

            var container2 = new UnityContainer();
            container2.RegisterType<IWeapon, Shuriken>();
            InjectionProperty injectionProperty = new InjectionProperty("SecondaryWeapon", new Sword());
            container2.RegisterType<Samurai>(injectionProperty);
            // overriding the parameter of the constructor.
            var samurai2 = container2.Resolve<Samurai>(new ParameterOverride("weapon", new Sword()));

            var container3 = new UnityContainer();
            container3.RegisterType<IWeapon, Sword>();

            InjectionMethod injectionMethod = new InjectionMethod("PickupItem", new Ration());
            container3.RegisterType<Samurai>(injectionMethod);
            var samurai3 = container3.Resolve<Samurai>();

            Console.ReadLine();
        }
Example #2
0
 private void registerViews()
 {
     //Register all views within the module here
      //Create a new viewmodel to inject into the datacontext of the view by registerting type with the unityContainer
      HomeViewModel homeVM = new HomeViewModel();
      InjectionProperty ip = new InjectionProperty("DataContext", homeVM);
      container.RegisterType<Object, Home>("Home", ip);
 }
Example #3
0
        private void registerViews()
        {
            //Register all views within the module here
              //Create a new viewmodel to inject into the datacontext of the view by registerting type with the unityContainer
            LocalMediaHomeVM localMediaHomeVM = new LocalMediaHomeVM();
            InjectionProperty ip = new InjectionProperty("DataContext", localMediaHomeVM);
            container.RegisterType<Object, LocalMediaHome>("LocalMediaHome", ip);

            container.RegisterType<Object, LocalVLCPlayer>("LocalVLCPlayer");
        }
Example #4
0
        private void registerViews()
        {
            //Register all views within the module here
            //Create a new viewmodel to inject into the datacontext of the view by registerting type with the unityContainer

            ServiceSelectionHomeViewModel serviceSelectionHomeVM = new ServiceSelectionHomeViewModel();
            InjectionProperty ip = new InjectionProperty("DataContext", serviceSelectionHomeVM);
            container.RegisterType<Object, ServiceSelectionHome>("serviceSelectionHome", ip);

            NetflixHomeVM netflixHomeVM = new NetflixHomeVM();
            ip = new InjectionProperty("DataContext", netflixHomeVM);
            container.RegisterType<Object, NetflixHome>("NetflixHome", ip);

            YouTubeHomeViewModel youtubeHomeVM = new YouTubeHomeViewModel();
            ip = new InjectionProperty("DataContext", youtubeHomeVM);
            container.RegisterType<Object, YouTubeHome>("YouTubeHome", ip);
        }
        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterInstance(CloudConfiguration.GetStorageAccount("DataConnectionString"));

            container.RegisterInstance<IRetryPolicyFactory>(new DefaultRetryPolicyFactory());

            var cloudStorageAccountType = typeof(Microsoft.WindowsAzure.CloudStorageAccount);
            var retryPolicyFactoryProperty = new InjectionProperty("RetryPolicyFactory", typeof(IRetryPolicyFactory));

            container
                .RegisterType<IAzureBlobContainer<Tenant>, EntitiesBlobContainer<Tenant>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.Tenants),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureBlobContainer<byte[]>, FilesBlobContainer>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.Logos, "image/jpeg"),
                    retryPolicyFactoryProperty);

            container.RegisterType<ITenantStore, TenantStore>();
        }
Example #6
0
        static void Main(string[] args)
        {
            //Unity container
            var unityContainer = new UnityContainer();

            //Registers game when the dependency is detected
            unityContainer.RegisterType<IGame, TrivialPursuit>();

            // Inject a property when dependecy is resolved
            // Can be left out
            InjectionProperty injectionProperty = new InjectionProperty("Name", "Trivial Pursuit Genus Edition");
            unityContainer.RegisterType<IGame, TrivialPursuit>(injectionProperty);

            //Resolving

            //Direct resolving
            //Resolves directly through a game of TrivialPursuit
            var game = unityContainer.Resolve<IGame>();
            game.addPlayer();
            game.addPlayer();
            Console.WriteLine(string.Format("{0} people playing to {1}", game.CurrentPlayers, game.Name));
            Console.ReadLine(); //Press Enter twice in console to forward to next game session

            //Indirect resolving through a Table class
            var table = unityContainer.Resolve<Table>();
            table.AddPlayer();
            table.AddPlayer();
            table.Play();
            Console.WriteLine(table.GameStatus());
            Console.ReadLine();

            //Overriding the constructor parameter of Table
            var table2 = unityContainer.Resolve<Table>(new ParameterOverride("game", new TicTacToe()));
            table2.AddPlayer();
            table2.AddPlayer();
            table2.Play();
            Console.WriteLine(table2.GameStatus());
            Console.ReadLine();
        }
Example #7
0
        private void InitUnity()
        {
            UnityConfig.Instance.Container.RegisterInstance(SkewrlConfig.Instance.StorageAccount);

            var cloudStorageAccountType = typeof(CloudStorageAccount);
            var retryPolicyFactoryProperty = new InjectionProperty("RetryPolicyFactory", typeof(IRetryPolicyFactory));
            var visibilityTime = TimeSpan.FromSeconds(300);

            //Probably better to register all types that are used across the projects
            //Any types that are used specifically in that role can be be registered there.
            #region Register types for Unity
            UnityConfig.Instance.Container
                .RegisterType<IRetryPolicyFactory, DefaultRetryPolicyFactory>()
                .RegisterType<IAzureObjectWithRetryPolicyFactory, AzureObjectWithRetryPolicyFactory>()
                .RegisterType<IUrlMapDataSource, UrlMapDataSource>()
                .RegisterType<IUrlTrackerDataSource, UrlTrackerDataSource>()
                .RegisterType<IRepository<UrlMap>, AzureTableRepository<UrlMap>>()
                .RegisterType<IRepository<UrlVisited>, AzureTableRepository<UrlVisited>>()
                .RegisterType<IRepository<UrlSummary>, AzureTableRepository<UrlSummary>>()
                .RegisterType<IAzureQueue<UrlRedirectMessage>, AzureQueue<UrlRedirectMessage>>(
                    new InjectionConstructor(cloudStorageAccountType, Constants.AzureQueues.UrlTrackerQueue, visibilityTime),
                    retryPolicyFactoryProperty);
            #endregion
        }
Example #8
0
        /// <summary>
        /// Loads the main job configuration.
        /// </summary>
        /// <param name="unityContainer">The container to register to.</param>
        protected virtual void LoadConfiguration(IUnityContainer unityContainer)
        {
            if (PersistenceSupport)
            {
                var tablePrefix = ConfigurationManager.AppSettings[AbstractDbBatchMetadataDao.TablePrefixSetting];
                var injectionMembers = new InjectionMember[tablePrefix == null ? 1 : 2];
                injectionMembers[0] = new InjectionProperty("ConnectionStringSettings",
                    new ResolvedParameter<ConnectionStringSettings>("Default"));
                if (tablePrefix != null)
                {
                    injectionMembers[1] = new InjectionProperty("TablePrefix", tablePrefix);
                }

                unityContainer.RegisterSingletonWithFactory<IJobRepository, DbJobRepositoryFactory>(injectionMembers);
                unityContainer.RegisterSingletonWithFactory<IJobExplorer, DbJobExplorerFactory>(injectionMembers);
            }
            else
            {
                unityContainer.RegisterSingletonWithFactory<IJobRepository, MapJobRepositoryFactory>();
                unityContainer.RegisterSingletonWithFactory<IJobExplorer, MapJobExplorerFactory>();

            }
            unityContainer.RegisterSingleton<IJobOperator, SimpleJobOperator>(new InjectionProperty("JobLauncher"),
                                                                        new InjectionProperty("JobRepository"),
                                                                        new InjectionProperty("JobExplorer"),
                                                                        new InjectionProperty("JobRegistry"));
            unityContainer.RegisterSingleton<IJobLauncher, SimpleJobLauncher>(new InjectionProperty("JobRepository"));
            unityContainer.RegisterSingleton<IListableJobLocator, MapJobRegistry>();

            unityContainer.RegisterSingleton<IJobParametersIncrementer, RunIdIncrementer>();

            unityContainer.RegisterSingleton<ITaskExecutor, SimpleAsyncTaskExecutor>();
        }
        public static void RegisterTypes(IUnityContainer container, bool roleInitialization)
        {
            var account = CloudConfiguration.GetStorageAccount("DataConnectionString");

            container.RegisterInstance(account);

            // http://msdn.microsoft.com/en-us/library/hh680900(v=pandp.50).aspx
            container.RegisterInstance<IRetryPolicyFactory>(roleInitialization
                ? new DefaultRetryPolicyFactory() as IRetryPolicyFactory
                : new ConfiguredRetryPolicyFactory() as IRetryPolicyFactory);

            container.RegisterType<IDictionary<string, TenantSurveyProcessingInfo>, Dictionary<string, TenantSurveyProcessingInfo>>(new InjectionConstructor());

            var cloudStorageAccountType = typeof(Microsoft.WindowsAzure.CloudStorageAccount);
            var retryPolicyFactoryProperty = new InjectionProperty("RetryPolicyFactory", typeof(IRetryPolicyFactory));

            // registering IAzureTable types
            container
                .RegisterType<IAzureTable<SurveyRow>, AzureTable<SurveyRow>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.Tables.Surveys),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureTable<QuestionRow>, AzureTable<QuestionRow>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.Tables.Questions),
                    retryPolicyFactoryProperty);

            // registering IAzureQueue types
            var visibilityTime = TimeSpan.FromSeconds(300);
            container
                .RegisterType<IAzureQueue<SurveyAnswerStoredMessage>, AzureQueue<SurveyAnswerStoredMessage>>(
                    SubscriptionKind.Standard.ToString(),
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.Queues.SurveyAnswerStoredStandard, visibilityTime),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureQueue<SurveyAnswerStoredMessage>, AzureQueue<SurveyAnswerStoredMessage>>(
                    SubscriptionKind.Premium.ToString(),
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.Queues.SurveyAnswerStoredPremium, visibilityTime),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureQueue<SurveyTransferMessage>, AzureQueue<SurveyTransferMessage>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.Queues.SurveyTransferRequest, visibilityTime),
                    retryPolicyFactoryProperty);

            // registering IAzureBlobContainer types
            container
                .RegisterType<IAzureBlobContainer<byte[]>, FilesBlobContainer>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.Logos, "image/jpeg"),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureBlobContainer<SurveyAnswersSummary>, EntitiesBlobContainer<SurveyAnswersSummary>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.SurveyAnswersSummaries),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureBlobContainer<List<string>>, EntitiesBlobContainer<List<string>>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.SurveyAnswersLists),
                    retryPolicyFactoryProperty)
                .RegisterType<IAzureBlobContainer<Tenant>, EntitiesBlobContainer<Tenant>>(
                    new InjectionConstructor(cloudStorageAccountType, AzureConstants.BlobContainers.Tenants),
                    retryPolicyFactoryProperty);

            var cacheEnabledProperty = new InjectionProperty("CacheEnabled", !roleInitialization && Convert.ToBoolean(CloudConfiguration.GetConfigurationSetting("EnableCaching")));

            // registering Store types
            container
                .RegisterType<ISurveyStore, SurveyStore>(cacheEnabledProperty)
                .RegisterType<ITenantStore, TenantStore>(cacheEnabledProperty)
                .RegisterType<ISurveyAnswerStore, SurveyAnswerStore>(new InjectionFactory((c, t, s) => new SurveyAnswerStore(
                    container.Resolve<ITenantStore>(),
                    container.Resolve<ISurveyAnswerContainerFactory>(),
                    container.Resolve<IAzureQueue<SurveyAnswerStoredMessage>>(SubscriptionKind.Standard.ToString()),
                    container.Resolve<IAzureQueue<SurveyAnswerStoredMessage>>(SubscriptionKind.Premium.ToString()),
                    container.Resolve<IAzureBlobContainer<List<string>>>())))
                .RegisterType<ISurveyAnswersSummaryStore, SurveyAnswersSummaryStore>()
                .RegisterType<ISurveySqlStore, SurveySqlStore>()
                .RegisterType<ISurveyTransferStore, SurveyTransferStore>();

            // Container for resolving the survey answer containers
            var surveyAnswerBlobContainerResolver = new UnityContainer();

            surveyAnswerBlobContainerResolver.RegisterInstance(account);

            // http://msdn.microsoft.com/en-us/library/hh680900(v=pandp.50).aspx
            surveyAnswerBlobContainerResolver.RegisterInstance<IRetryPolicyFactory>(roleInitialization
                ? new DefaultRetryPolicyFactory() as IRetryPolicyFactory
                : new ConfiguredRetryPolicyFactory() as IRetryPolicyFactory);

            surveyAnswerBlobContainerResolver.RegisterType<IAzureBlobContainer<SurveyAnswer>, EntitiesBlobContainer<SurveyAnswer>>(
                new InjectionConstructor(cloudStorageAccountType, typeof(string)),
                retryPolicyFactoryProperty);

            container.RegisterType<ISurveyAnswerContainerFactory, SurveyAnswerContainerFactory>(
                new InjectionConstructor(surveyAnswerBlobContainerResolver));
        }