本文整理汇总了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());
}
示例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));
}
示例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");
}
}
示例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);
}
//.........这里部分代码省略.........