当前位置: 首页>>代码示例>>C#>>正文


C# IContainer.With方法代码示例

本文整理汇总了C#中IContainer.With方法的典型用法代码示例。如果您正苦于以下问题:C# IContainer.With方法的具体用法?C# IContainer.With怎么用?C# IContainer.With使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IContainer的用法示例。


在下文中一共展示了IContainer.With方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetViewLocators

 internal IEnumerable<IViewLocator> GetViewLocators(IContainer container)
 {
     return _viewLocators.Select(o => o.Maybe(
           v => v.Cast<IViewLocator>(),
           v => v.Cast<Type>().Get(container.GetInstance).Cast<IViewLocator>(),
           v => v.Cast<ModelMatchConfiguration>().Get(m => (IViewLocator)container.With(m).GetInstance<DynamicViewLocator>())
         ).MustHaveValue());
 }
开发者ID:flq,项目名称:Scal,代码行数:8,代码来源:AppModel.cs

示例2: ConstructorInjectionWithParams

 public void ConstructorInjectionWithParams()
 {
     Container = new Container(x =>
     {
         x.For<IFoo>().Use<Foo>();
         x.For<IBar>().Use<Bar>();
     });
     var bar = Container.With("v1").EqualTo(1).With("v2").EqualTo(2).GetInstance<IBar>();
     Assert.That(bar.V1, Is.EqualTo(1));
     Assert.That(bar.V2, Is.EqualTo(2));
 }
开发者ID:recr0ns,项目名称:IoCTesting,代码行数:11,代码来源:IoCTesting.cs

示例3: Start

        static void Start(IContainer container, Action<ExtensionInitializer> initializeAction)
        {
            _log.Debug("Parsing command line");

            IEnumerable<Command> commands = CommandLine.Parse<Command>(init =>
                {
                    var containerConfigurator = new StructureMapContainerConfigurator(container);
                    var containerAccess = new StructureMapContainer(container);

                    container.Configure(x =>
                        {
                            x.For<core.Container>().Singleton().Use(containerAccess);
                            x.For<ContainerConfigurator>().Singleton().Use(containerConfigurator);
                        });

                    var initializer = container.With(init).GetInstance<StructureMapExtensionInitializer>();

                    initializeAction(initializer);
                }).ToArray();

            if (commands.Any())
            {
                ExecuteCommands(commands);
            }
            else
            {
                _log.Warn("No commands specified");
            }
        }
开发者ID:wbinford,项目名称:nu,代码行数:29,代码来源:Program.cs

示例4: Configure

        public static void Configure(IContainer container, JsonApiConfiguration jsonApiConfiguration)
        {
            container.Configure(cfg => cfg.AddRegistry<JsonApiRegistry>());

            var registry = new ResourceTypeRegistry();
            foreach (var resourceTypeConfiguration in jsonApiConfiguration.ResourceTypeConfigurations)
            {
                var resourceTypeRegistration = resourceTypeConfiguration.BuildResourceTypeRegistration();
                registry.AddRegistration(resourceTypeRegistration);

                var configuration = resourceTypeConfiguration;


                container.Configure(cfg =>
                {
                    cfg.For<IResourceTypeConfiguration>().Use(configuration).Named(resourceTypeRegistration.Type.FullName).Singleton();
                    cfg.For<IResourceTypeConfiguration>().Use(configuration).Named(resourceTypeRegistration.ResourceTypeName).Singleton();
                });

                if (resourceTypeConfiguration.DocumentMaterializerType != null)
                {
                    container.Configure(cfg => cfg.For(resourceTypeConfiguration.DocumentMaterializerType));
                }

                foreach (var relationship in resourceTypeRegistration.Relationships)
                {
                    IResourceTypeRelationshipConfiguration relationshipConfiguration;
                    if (resourceTypeConfiguration.RelationshipConfigurations
                        .TryGetValue(relationship.Property.Name, out relationshipConfiguration))
                    {
                        if (relationshipConfiguration.MaterializerType != null)
                        {
                            container.Configure(cfg => cfg.For(relationshipConfiguration.MaterializerType));
                            continue;
                        }
                    }

                    // They didn't set an explicit materializer. See if they specified a factory for this resource type.
                    if (configuration.RelatedResourceMaterializerTypeFactory == null) continue;

                    var materializerType = configuration.RelatedResourceMaterializerTypeFactory(relationship);
                    container.Configure(cfg => cfg.For(materializerType));
                }
            }
            container.Configure(cfg => cfg.For<IResourceTypeRegistry>().Use(c => registry).Singleton());


            container.Configure(cfg =>
            {
                cfg.For<IDocumentMaterializerLocator>()
                    .Use<DocumentMaterializerLocator>()
                    .Ctor<Func<string, IDocumentMaterializer>>().Is(resourceTypeName =>
                    {
                        var configuration = container.GetInstance<IResourceTypeConfiguration>(resourceTypeName);
                        var registration = registry.GetRegistrationForResourceTypeName(resourceTypeName);
                        var args = new ExplicitArguments();
                        args.Set<IResourceTypeRegistration>(registration);
                        if (configuration.DocumentMaterializerType != null)
                            return
                                (IDocumentMaterializer)
                                    container.With(args).GetInstance(configuration.DocumentMaterializerType);
                        return container.With(args).GetInstance<IDocumentMaterializer>();
                    })
                    .Ctor<Func<Type, IDocumentMaterializer>>().Is(type =>
                    {
                        var configuration = container.GetInstance<IResourceTypeConfiguration>(type.FullName);
                        var registration = registry.GetRegistrationForType(type);
                        var args = new ExplicitArguments();
                        args.Set<IResourceTypeRegistration>(registration);
                        if (configuration.DocumentMaterializerType != null)
                            return
                                (IDocumentMaterializer)
                                    container.With(args).GetInstance(configuration.DocumentMaterializerType);
                        return container.With(args).GetInstance<IDocumentMaterializer>();
                    })
                    .Ctor<Func<string, string, IRelatedResourceDocumentMaterializer>>().Is((resourceTypeName, relationshipName) =>
                    {
                        var configuration = container.GetInstance<IResourceTypeConfiguration>(resourceTypeName);
                        var registration = registry.GetRegistrationForResourceTypeName(resourceTypeName);
                        var relationship = registration.GetFieldByName(relationshipName) as ResourceTypeRelationship;
                        if (relationship == null)
                            throw JsonApiException.CreateForNotFound(
                                string.Format("No relationship `{0}` exists for the resource type `{1}`.", relationshipName, resourceTypeName));

                        var args = new ExplicitArguments();
                        args.Set<IResourceTypeRegistration>(registration);
                        args.Set<ResourceTypeRelationship>(relationship);

                        // First, see if they have set an explicit materializer for this relationship
                        IResourceTypeRelationshipConfiguration relationshipConfiguration;
                        if (configuration.RelationshipConfigurations.TryGetValue(relationship.Property.Name,
                            out relationshipConfiguration) && relationshipConfiguration.MaterializerType != null)
                            return (IRelatedResourceDocumentMaterializer)container.With(args).GetInstance(relationshipConfiguration.MaterializerType);

                        // They didn't set an explicit materializer. See if they specified a factory for this resource type.
                        if (configuration.RelatedResourceMaterializerTypeFactory != null)
                        {
                            var materializerType = configuration.RelatedResourceMaterializerTypeFactory(relationship);
                            return (IRelatedResourceDocumentMaterializer)container.With(args).GetInstance(materializerType);
                        }
//.........这里部分代码省略.........
开发者ID:SathishN,项目名称:JSONAPI.NET,代码行数:101,代码来源:JsonApiConfigurator.cs


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