• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# UnityContainer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中UnityContainer的典型用法代码示例。如果您正苦于以下问题:C# UnityContainer类的具体用法?C# UnityContainer怎么用?C# UnityContainer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



UnityContainer类属于命名空间,在下文中一共展示了UnityContainer类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: GetContainer

        public static IUnityContainer GetContainer(FrontEndUnityContainerAvailable containerSelected)
        {
            IUnityContainer result = new UnityContainer();
            result.RegisterType(typeof(IAuthenticationProxy), typeof(AuthenticationProxy), new InjectionMember[0]);
            result.RegisterType(typeof(IMembershipProxy), typeof(MembershipProxy), new InjectionMember[0]);
            result.RegisterType(typeof(IRoleManagerProxy), typeof(RoleManagerProxy), new InjectionMember[0]);
            result.RegisterType(typeof(IRolesProxy), typeof(RolesProxy), new InjectionMember[0]);
            result.RegisterType(typeof(IProfileProxy), typeof(ProfileProxy), new InjectionMember[0]);
            result.RegisterType(typeof(ILoggingProxy), typeof(LoggingProxy), new InjectionMember[0]);
            result.RegisterType(typeof(ISyndicationProxy), typeof(SyndicationProxy), new InjectionMember[0]);
            result.RegisterType(typeof(IUserRequestContextFrontEnd), typeof(UserRequestContextFrontEnd), new InjectionMember[0]);
            result.RegisterType(typeof(IUserSessionModel), typeof(UserSessionAtHttpCookies), new InjectionMember[0]);

            switch (containerSelected)
            {
                case FrontEndUnityContainerAvailable.ProxiesToCustomHost:
                    result.RegisterType(typeof(IClientChannelInitializer<>), typeof(ClientChannelCustomHostInitializer<>), new InjectionMember[0]);
                    break;
                case FrontEndUnityContainerAvailable.ProxiesToAzure:
                    result.RegisterType(typeof(IClientChannelInitializer<>), typeof(ClientChannelAzureInternalRoleInitializer<>), new InjectionMember[0]);
                    break;
                default:
                    throw new Exception("IUnityContainer does not exist in the list of available providers");
            }

            return result;
        }
开发者ID:jordivila,项目名称:Net_MVC_NLayer_Generator,代码行数:27,代码来源:UnityContainerProvider.cs


示例2: OptionalDependencyParametersAreInjectedWithNull

        public void OptionalDependencyParametersAreInjectedWithNull()
        {
            IUnityContainer container = new UnityContainer();

            var result = container.Resolve<ObjectWithOptionalConstructorParameter>();
            Assert.IsNull(result.SomeInterface);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:OptionalDependencyAttributeFixture.cs


示例3: When_getting_controller_attributes_then_builds_up_instance

        public void When_getting_controller_attributes_then_builds_up_instance()
        {
            using (var container = new UnityContainer())
            {
                // Arrange
                var someInstance = new SomeClass();
                container.RegisterInstance<ISomeInterface>(someInstance);
                container.RegisterType<TestFilterAttribute>(new InjectionProperty("Some"));

                var context = new ControllerContext { Controller = new ControllerWithTypeAttribute() };
                var controllerDescriptor = new ReflectedControllerDescriptor(context.Controller.GetType());
                var action = context.Controller.GetType().GetMethod("MyActionMethod");
                var actionDescriptor = new ReflectedActionDescriptor(action, "MyActionMethod", controllerDescriptor);
                var provider = new UnityFilterAttributeFilterProvider(container);

                // Act
                Filter filter = provider.GetFilters(context, actionDescriptor).Single();

                // Assert
                TestFilterAttribute attrib = filter.Instance as TestFilterAttribute;
                Assert.IsNotNull(attrib);
                Assert.AreEqual(FilterScope.Controller, filter.Scope);
                Assert.AreEqual(1234, filter.Order);
                Assert.AreSame(someInstance, attrib.Some);
            }
        }
开发者ID:kangkot,项目名称:unity,代码行数:26,代码来源:MvcUnityFilterAttributeFilterProviderFixture.cs


示例4: CanUseContainerToResolveFactoryParameters

        public void CanUseContainerToResolveFactoryParameters()
        {
            bool factoryWasCalled = false;
            string connectionString = "Northwind";

            IUnityContainer container = new UnityContainer();

            container.AddNewExtension<StaticFactoryExtension>()
                .Configure<IStaticFactoryConfiguration>()
                    .RegisterFactory<MockDatabase>(c =>
                    {
                        Assert.AreSame(container, c);
                        factoryWasCalled = true;
                        string cs = c.Resolve<string>("connectionString");
                        return MockDatabase.Create(cs);
                    })
                    .Container
                .RegisterInstance<string>("connectionString", connectionString);

            MockDatabase db = container.Resolve<MockDatabase>();

            Assert.IsTrue(factoryWasCalled);
            Assert.IsNotNull(db);
            Assert.AreEqual(connectionString, db.ConnectionString);
        }
开发者ID:kangkot,项目名称:unity,代码行数:25,代码来源:StaticFactoryFixture.cs


示例5: StaticMethodsShouldNotBeInjected

        public void StaticMethodsShouldNotBeInjected()
        {
            IUnityContainer container = new UnityContainer();

            GuineaPig pig = container.Resolve<GuineaPig>();
            Assert.IsFalse(GuineaPig.StaticMethodWasCalled);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:MethodInjectionFixture.cs


示例6: ContainerControlledLifetimeReturnsSameInstanceFromMultipleThreads

        public void ContainerControlledLifetimeReturnsSameInstanceFromMultipleThreads()
        {
            IUnityContainer container = new UnityContainer()
                .AddExtension(new SpyExtension(new DelayStrategy(), UnityBuildStage.Lifetime))
                .RegisterType<object>(new ContainerControlledLifetimeManager());

            object result1 = null;
            object result2 = null;

            Thread thread1 = new Thread(delegate()
            {
                result1 = container.Resolve<object>();
            });

            Thread thread2 = new Thread(delegate()
            {
                result2 = container.Resolve<object>();
            });

            thread1.Start();
            thread2.Start();

            thread2.Join();
            thread1.Join();

            Assert.IsNotNull(result1);
            Assert.AreSame(result1, result2);
        }
开发者ID:kangkot,项目名称:unity,代码行数:28,代码来源:ContainerControlledLifetimeThreadingFixture.cs


示例7: Arrange

 protected virtual void Arrange()
 {
     Container =
         new UnityContainer()
             .RegisterType<BaseClass, DerivedClass>("derived",
                 new Interceptor<TransparentProxyInterceptor>(),
                 new InterceptionBehavior<PolicyInjectionBehavior>())
             .RegisterType<BaseClass, DerivedWithNoOverrideClass>("derived-nooverride",
                 new Interceptor<TransparentProxyInterceptor>(),
                 new InterceptionBehavior<PolicyInjectionBehavior>())
             .RegisterType<BaseClass>(
                 new Interceptor<TransparentProxyInterceptor>(),
                 new InterceptionBehavior<PolicyInjectionBehavior>())
             .AddNewExtension<Interception>()
             .Configure<Interception>()
                 .AddPolicy("base")
                     .AddMatchingRule(new TypeMatchingRule(typeof(BaseClass)))
                     .AddMatchingRule(new MemberNameMatchingRule("InterceptedMethod"))
                     .AddCallHandler(new AppendSuffixCallHandler { Suffix = "-basehandler", Order = 1 })
                 .Interception
                 .AddPolicy("derived-no-override")
                     .AddMatchingRule(new TypeMatchingRule(typeof(DerivedWithNoOverrideClass)))
                     .AddMatchingRule(new MemberNameMatchingRule("InterceptedMethod"))
                     .AddCallHandler(new AppendSuffixCallHandler { Suffix = "-derivednooverridehandler", Order = 2 })
                 .Interception
                 .AddPolicy("derived")
                     .AddMatchingRule(new TypeMatchingRule(typeof(DerivedClass)))
                     .AddMatchingRule(new MemberNameMatchingRule("InterceptedMethod"))
                     .AddCallHandler(new AppendSuffixCallHandler { Suffix = "-derivedhandler", Order = 3 })
                 .Interception
             .Container;
 }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:32,代码来源:PolicyInjectionBehaviorFixture.cs


示例8: CanAddExtensionWithNonDefaultConstructor

 public void CanAddExtensionWithNonDefaultConstructor()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<ContainerExtensionWithNonDefaultConstructor>();
     var extension = container.Configure(typeof (ContainerExtensionWithNonDefaultConstructor));
     Assert.IsNotNull(extension);
 }
开发者ID:theoju,项目名称:CleanCode,代码行数:7,代码来源:UnityExtensionFixture.cs


示例9: CanConfigureRemotingInterceptionOnInterface

        public void CanConfigureRemotingInterceptionOnInterface()
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();

            container.Configure<Interception>().SetInterceptorFor<Interface>(new TransparentProxyInterceptor());
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:InterceptionFixture.cs


示例10: ContainerThrowsWhenConfiguringStaticMethodForInjection

 public void ContainerThrowsWhenConfiguringStaticMethodForInjection()
 {
     IUnityContainer container = new UnityContainer()
         .RegisterType<GuineaPig>(
                new InjectionMethod("ShouldntBeCalled"));
     Assert.Fail("Should not get here");
 }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:MethodInjectionFixture.cs


示例11: DoesNotRegisterTypeWithNoLifetimeOrInjectionMembers

        public void DoesNotRegisterTypeWithNoLifetimeOrInjectionMembers()
        {
            var container = new UnityContainer();
            container.RegisterTypes(new[] { typeof(MockLogger) }, getName: t => "name");

            Assert.IsFalse(container.Registrations.Any(r => r.MappedToType == typeof(MockLogger)));
        }
开发者ID:kangkot,项目名称:unity,代码行数:7,代码来源:AssemblyScanningFixture.cs


示例12: Configure

        public override void Configure(Container container)
        {
            var unityContainer = new UnityContainer()
              .AddNewExtension<BuildTracking>()
              .AddNewExtension<LogCreation>();

            var userRepository = new InMemoryAuthRepository();

            this.Plugins.Add(new AuthFeature(() => new AuthUserSession(),
              new IAuthProvider[] { new BasicAuthProvider()}));

            this.Plugins.Add(new RegistrationFeature());

            unityContainer.RegisterInstance<ICacheClient>(new MemoryCacheClient());
            unityContainer.RegisterInstance<IUserAuthRepository>(userRepository);

            unityContainer.RegisterInstance<IDbConnectionFactory>(new OrmLiteConnectionFactory("~/Database/SmartDom.db".MapServerPath(),
                SqliteOrmLiteDialectProvider.Instance));

            unityContainer.RegisterType<IOrmWrapper, OrmWrapper>();
            unityContainer.RegisterType<IConfigurationRepository, AppConfigRepository>();
            unityContainer.RegisterType<MediaAdapterAbstractFactory<SerialPort>, SerialPortAdapterFactory>(new ContainerControlledLifetimeManager());
            unityContainer.RegisterType<ModbusMasterAbstractFactory<SerialPort>, RtuSerialModbusMasterFactory>(new ContainerControlledLifetimeManager());
            unityContainer.RegisterType<IGenericRepository<Device>, DeviceDbRepository>(new ContainerControlledLifetimeManager(),
                new InjectionMethod("Initialize"));
            unityContainer.RegisterType<IDeviceAccessLayer, SerialAccessLayer>(new ContainerControlledLifetimeManager());
            unityContainer.RegisterType<IMessageDecoder, MessageDecoder>();
            unityContainer.RegisterType<IDeviceManager, DeviceManager>();

            var unityAdapter = new UnityContainerAdapter(unityContainer);
            container.Adapter = unityAdapter;

            AddUser(userRepository);
        }
开发者ID:MarcinKowal,项目名称:SmartDom,代码行数:34,代码来源:AppHost.cs


示例13: CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers

        public void CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers()
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();

            container
                .Configure<Interception>()
                    .AddPolicy("policy1")
                        .AddMatchingRule("rule1")
                        .AddCallHandler("handler1")
                        .AddCallHandler("handler2").Interception.Container
                .RegisterType<IMatchingRule, AlwaysMatchingRule>("rule1")
                .RegisterType<ICallHandler, GlobalCountCallHandler>(
                    "handler1",
                    new InjectionConstructor("handler1"))
                .RegisterType<ICallHandler, GlobalCountCallHandler>(
                    "handler2",
                    new InjectionConstructor("handler2"),
                    new InjectionProperty("Order", 10));

            GlobalCountCallHandler.Calls.Clear();

            container
                .Configure<Interception>()
                    .SetInterceptorFor<Wrappable>("wrappable", new VirtualMethodInterceptor());

            Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
            wrappable1.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]);
            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]);
        }
开发者ID:theoju,项目名称:CleanCode,代码行数:32,代码来源:ConvenienceConfigurationFixture.cs


示例14: UnityResolver_Resolves_Registered_Utilities_Test

 public void UnityResolver_Resolves_Registered_Utilities_Test()
 {
     var container = new UnityContainer();
     container.RegisterInstance<IUtilities>(new Utilities(new PersistenceService(_context),new LogService("ConnectionString", new EmailService())));
     var resolver = new UnityResolver(container);
     var instance = resolver.GetService(typeof(IUtilities));
     Assert.IsNotNull(instance);
 }
开发者ID:jpsjph,项目名称:JP_EroPro,代码行数:8,代码来源:DependencyInjectionTest.cs


示例15: UnityResolver_Resolves_Registered_LogService_Test

 public void UnityResolver_Resolves_Registered_LogService_Test()
 {
     var container = new UnityContainer();
     container.RegisterInstance<ILogService>(new LogService("ConnectionString", new EmailService()));
     var resolver = new UnityResolver(container);
     var instance = resolver.GetService(typeof(ILogService));
     Assert.IsNotNull(instance);
 }
开发者ID:jpsjph,项目名称:JP_EroPro,代码行数:8,代码来源:DependencyInjectionTest.cs


示例16: UnityResolver_Resolves_Registered_PersistenceService_Test

 public void UnityResolver_Resolves_Registered_PersistenceService_Test()
 {
     var container = new UnityContainer();
     container.RegisterInstance<IPersistenceService>(new PersistenceService(_context));
     var resolver = new UnityResolver(container);
     var instance = resolver.GetService(typeof(IPersistenceService));
     Assert.IsNotNull(instance);
 }
开发者ID:jpsjph,项目名称:JP_EroPro,代码行数:8,代码来源:DependencyInjectionTest.cs


示例17: UnityResolver_Resolves_Registered_DBContect_Test

 public void UnityResolver_Resolves_Registered_DBContect_Test()
 {
     var container = new UnityContainer();
     container.RegisterInstance<IDataContext>(_context);
     var resolver = new UnityResolver(container);
     var instance = resolver.GetService(typeof(IDataContext));
     Assert.IsNotNull(instance);
 }
开发者ID:jpsjph,项目名称:JP_EroPro,代码行数:8,代码来源:DependencyInjectionTest.cs


示例18: CanResolveALazy

        public void CanResolveALazy()
        {
            var container = new UnityContainer()
                .RegisterType<ILogger, MockLogger>();

            var lazy = container.Resolve<Lazy<ILogger>>();
            Assert.IsNotNull(lazy);
        }
开发者ID:kangkot,项目名称:unity,代码行数:8,代码来源:LazyResolveFixture.cs


示例19: OptionalPropertiesGetNullWhenNotConfigured

        public void OptionalPropertiesGetNullWhenNotConfigured()
        {
            IUnityContainer container = new UnityContainer();

            var result = container.Resolve<ObjectWithOptionalProperty>();

            Assert.IsNull(result.SomeInterface);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:8,代码来源:OptionalDependencyAttributeFixture.cs


示例20: ContainerReturnsEmptyArrayIfNoObjectsRegistered

        public void ContainerReturnsEmptyArrayIfNoObjectsRegistered()
        {
            IUnityContainer container = new UnityContainer();
            List<object> results = new List<object>(container.ResolveAll<object>());

            Assert.IsNotNull(results);
            CollectionAssert.AreEqual(new object[0], results);
        }
开发者ID:theoju,项目名称:CleanCode,代码行数:8,代码来源:ResolvingArraysFixture.cs



注:本文中的UnityContainer类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# UnityEditor类代码示例发布时间:2022-05-24
下一篇:
C# UnityAction类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap