本文整理汇总了C#中ConventionBuilder类的典型用法代码示例。如果您正苦于以下问题:C# ConventionBuilder类的具体用法?C# ConventionBuilder怎么用?C# ConventionBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConventionBuilder类属于命名空间,在下文中一共展示了ConventionBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CompleteInitialization
public static void CompleteInitialization()
{
Assembly[] partsAssemblies;
lock (_initLock)
{
if (_isInitialized)
return;
_isInitialized = true;
partsAssemblies = _partAssemblies.Union(FindWebApplicationAssemblies()).ToArray();
}
var conventions = new ConventionBuilder()
.WithMvcConventions()
.WithEagerConstructionSupport();
conventions.ForTypesUnderNamespace("Parts").Export().ExportInterfaces();
var container = new ContainerConfiguration()
.WithDefaultConventions(conventions)
.WithAssemblies(partsAssemblies)
.WithApplicationSettings()
.CreateContainer();
MvcCompositionProvider.Initialize(container);
CompositionFilterProvider.Install(FilterProviders.Providers);
ImportCapableFilterAttributeFilterProvider.Install(FilterProviders.Providers);
container.ConstructEagerParts();
}
示例2: RegistrationByConvention
public static void RegistrationByConvention(this Container container,
BehaviorConfiguration configuration,
Action<IConventionBuilderSyntax<Lifestyle, Container>> convention)
{
if (configuration == null)
{
configuration = BehaviorConfiguration.Default;
}
var logger = new DiagnosticLogger(configuration);
var serviceMappingTracker = new ServiceMappingTracker(logger);
var implementationFilter = new ImplementationFilter();
var adapter = new SimpleInjectorContainerAdapter(container, serviceMappingTracker, implementationFilter);
var configServiceFilter = ((IBehaviorConfiguration)configuration).GetServiceFilter();
var asmSelector = new AssemblySelector();
var serviceExtractor = new ServiceExtractor();
var containerDecorator = new ContainerLogger<Lifestyle>(adapter, logger);
var serviceFilterAggregator = new ServiceFilterAggregator(new IServiceFilter[] { configServiceFilter, implementationFilter, serviceExtractor, serviceMappingTracker });
using (var builder = new ConventionBuilder<Lifestyle>(containerDecorator,
asmSelector,
serviceFilterAggregator,
serviceExtractor))
{
var importer = new ConventionBuilderSyntaxDecorator<Lifestyle, Container>(builder, container, implementationFilter, asmSelector);
convention(importer);
}
}
示例3: GetOkraContainerConfiguration
// *** Protected Methods ***
protected ContainerConfiguration GetOkraContainerConfiguration()
{
// Create a basic container configuration with,
// - The Okra assembly (via the INavigationManager interface)
// - The Okra.MEF assembly (via the OkraBootstrapper class)
ConventionBuilder okraConventionBuilder = new ConventionBuilder();
okraConventionBuilder.ForType<NavigationManager>().Export<INavigationManager>()
.Shared()
.SelectConstructor(ctors => ctors.First(), (info, builder) =>
{
if (info.ParameterType == typeof(INavigationTarget))
builder.AllowDefault();
});
okraConventionBuilder.ForType<SettingsPaneManager>().Export<ISettingsPaneManager>().Shared();
okraConventionBuilder.ForType<ActivationManager>().Export<IActivationManager>().Shared();
okraConventionBuilder.ForType<SearchManager>().Export<ISearchManager>().Shared();
okraConventionBuilder.ForType<ShareSourceManager>().Export<IShareSourceManager>().Shared();
okraConventionBuilder.ForType<ShareTargetManager>().Export<IShareTargetManager>().Shared();
okraConventionBuilder.ForType<LifetimeManager>().Export<ILifetimeManager>().Shared();
okraConventionBuilder.ForType<StorageManager>().Export<IStorageManager>().Shared();
okraConventionBuilder.ForType<LaunchActivationHandler>().Export<ILaunchActivationHandler>().Shared();
return new ContainerConfiguration()
.WithAssembly(typeof(INavigationManager).GetTypeInfo().Assembly, okraConventionBuilder)
.WithAssembly(typeof(OkraBootstrapper).GetTypeInfo().Assembly);
}
示例4: UsingNLog
public void UsingNLog()
{
var writer = GetTrace();
LoggingConfiguration config = new LoggingConfiguration();
TraceTarget target = new TraceTarget();
target.Layout = "NLOG [${level:uppercase=true}] ${message}";
config.AddTarget("trace", target);
config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, target));
LogManager.Configuration = config;
ConventionBuilder.Logger = t => LogManager.GetLogger(t);
var builder = new ConventionBuilder();
builder.ScanThisAssembly().For<HandlerConventions>();
var convention = builder.Build();
Approvals.VerifyAll(writer
.ToString()
.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.OrderBy(s => s), "");
}
示例5: GetContainerConfiguration
// *** Protected Methods ***
protected virtual ContainerConfiguration GetContainerConfiguration()
{
// Create a basic container configuration with,
// - The application's main assembly (i.e. that defines the current Application subclass)
// - The Cocoon assembly (via the INavigationManager interface)
// - The Cocoon.MEF assembly (via the CocoonBootstrapper class)
ConventionBuilder cocoonConventionBuilder = new ConventionBuilder();
cocoonConventionBuilder.ForType<NavigationManager>().Export<INavigationManager>()
.Shared()
.SelectConstructor(ctors => ctors.First(), (info, builder) =>
{
if (info.ParameterType == typeof(INavigationTarget))
builder.AllowDefault();
});
cocoonConventionBuilder.ForType<ActivationManager>().Export<IActivationManager>().Shared();
cocoonConventionBuilder.ForType<LifetimeManager>().Export<ILifetimeManager>().Shared();
cocoonConventionBuilder.ForType<StorageManager>().Export<IStorageManager>().Shared();
return new ContainerConfiguration()
.WithAssembly(Application.Current.GetType().GetTypeInfo().Assembly)
.WithAssembly(typeof(INavigationManager).GetTypeInfo().Assembly, cocoonConventionBuilder)
.WithAssembly(typeof(CocoonBootstrapper).GetTypeInfo().Assembly);
}
示例6: AddWinRTSpecificConventions
static partial void AddWinRTSpecificConventions( ConventionBuilder builder )
{
var assembly = new PublicKeyTokenSpecification( typeof( Host ) );
var settingsFlyout = new SettingsFlyoutSpecification();
builder.ForTypesMatching( settingsFlyout.IsSatisfiedBy ).Export().Export<SettingsFlyout>().ExportInterfaces( assembly.IsSatisfiedBy ).ImportProperties( p => p != null && p.Name == "Model" );
}
示例7: StandardExportInterfacesShouldWork
public void StandardExportInterfacesShouldWork()
{
// Export all interfaces except IDisposable, Export contracts on types without interfaces. except for disposable types
var builder = new ConventionBuilder();
builder.ForTypesMatching((t) => true).ExportInterfaces();
builder.ForTypesMatching((t) => t.GetTypeInfo().ImplementedInterfaces.Where((iface) => iface != typeof(System.IDisposable)).Count() == 0).Export();
var container = new ContainerConfiguration()
.WithPart<Standard>(builder)
.WithPart<Dippy>(builder)
.WithPart<Derived>(builder)
.WithPart<BareClass>(builder)
.CreateContainer();
var importer = new Importer();
container.SatisfyImports(importer);
Assert.NotNull(importer.First);
Assert.True(importer.First.Count() == 3);
Assert.NotNull(importer.Second);
Assert.True(importer.Second.Count() == 3);
Assert.NotNull(importer.Third);
Assert.True(importer.Third.Count() == 3);
Assert.NotNull(importer.Fourth);
Assert.True(importer.Fourth.Count() == 3);
Assert.NotNull(importer.Fifth);
Assert.True(importer.Fifth.Count() == 3);
Assert.Null(importer.Base);
Assert.Null(importer.Derived);
Assert.Null(importer.Dippy);
Assert.Null(importer.Standard);
Assert.Null(importer.Disposable);
Assert.NotNull(importer.BareClass);
}
示例8: ConfigureContainer
public static void ConfigureContainer()
{
var containerConventions = new ConventionBuilder();
containerConventions.ForType<DbProductRepository>()
.ExportInterfaces()
.SelectConstructorWithMostParameters()
.InstancePerHttpRequest();
containerConventions.ForType<DbLogger>()
.ExportInterfaces()
.SelectConstructorWithMostParameters()
.InstancePerHttpRequest();
containerConventions.ForType<ProductDbContext>()
.Export()
.InstancePerHttpRequest();
containerConventions.ForTypesDerivedFrom<Controller>()
.Export<IController>()
.Export()
.SelectConstructorWithMostParameters();
var containerConfig = new ContainerConfiguration();
containerConfig.WithAssembly(Assembly.GetExecutingAssembly(), containerConventions);
containerConfig.CreateContainer().UseWithMvc();
}
示例9: MapType_OverridingSelectionOfConventionSelectedConstructor
public void MapType_OverridingSelectionOfConventionSelectedConstructor()
{
var builder = new ConventionBuilder();
builder.
ForTypesDerivedFrom<IFoo>().
Export<IFoo>();
builder.ForType<FooImplWithConstructors>()
.SelectConstructor(cis => cis.ElementAtOrDefault(1));
var fooImplWithConstructors = typeof(FooImplWithConstructors).GetTypeInfo();
var constructor1 = fooImplWithConstructors.DeclaredConstructors.Where(c => c.GetParameters().Length == 0).Single();
var constructor2 = fooImplWithConstructors.DeclaredConstructors.Where(c => c.GetParameters().Length == 1).Single();
var constructor3 = fooImplWithConstructors.DeclaredConstructors.Where(c => c.GetParameters().Length == 2).Single();
// necessary as BuildConventionConstructorAttributes is only called for type level query for attributes
Assert.Equal(0, builder.GetCustomAttributes(typeof(FooImplWithConstructors), constructor1).Count());
Assert.Equal(0, builder.GetCustomAttributes(typeof(FooImplWithConstructors), constructor3).Count());
var ci = constructor2;
var attrs = builder.GetCustomAttributes(typeof(FooImplWithConstructors), ci);
Assert.Equal(1, attrs.Count());
Assert.Equal(typeof(ImportingConstructorAttribute), attrs.FirstOrDefault().GetType());
}
示例10: CreateContainer
protected static CompositionContext CreateContainer(ConventionBuilder rb, params Type[] types)
{
return new ContainerConfiguration()
.WithParts(types)
.WithDefaultConventions(rb)
.CreateContainer();
}
示例11: CanSpecifyExportsWithConventionBuilder
public void CanSpecifyExportsWithConventionBuilder()
{
var rb = new ConventionBuilder();
rb.ForType<BarePart>().Export();
var cc = CreateContainer(rb, typeof(BarePart));
var x = cc.GetExport<BarePart>();
Assert.NotNull(x);
}
示例12: AddUISpecificConventions
static partial void AddUISpecificConventions( ConventionBuilder builder )
{
var viewModel = new ViewModelSpecification();
builder.ForTypesDerivedFrom<IShellView>().Export().Export<IShellView>().Shared();
builder.ForTypesMatching( viewModel.IsSatisfiedBy ).Export();
builder.ForType<EventBroker>().Export<IEventBroker>().Shared();
}
示例13: AsContractName_AndContractType_SetsContractNameAndType
public void AsContractName_AndContractType_SetsContractNameAndType()
{
var builder = new ConventionBuilder();
builder.ForType<FooImpl>().ImportProperty((p) => p.IFooProperty, (c) => c.AsContractName("hey"));
ImportAttribute importAtt = GetImportAttribute(builder);
Assert.Equal("hey", importAtt.ContractName);
}
示例14: AsContractName_AndContractType_ComputeContractNameFromType
public void AsContractName_AndContractType_ComputeContractNameFromType()
{
var builder = new ConventionBuilder();
builder.ForType<FooImpl>().ImportProperty((p) => p.IFooProperty, c => c.AsContractName(t => "Contract:" + t.FullName));
ImportAttribute importAtt = GetImportAttribute(builder);
Assert.Equal("Contract:" + typeof(IFoo).FullName, importAtt.ContractName);
}
示例15: AsContractName_AndContractType_SetsContractNameAndType
public void AsContractName_AndContractType_SetsContractNameAndType()
{
var builder = new ConventionBuilder();
builder.ForTypesDerivedFrom<IFoo>().Export((e) => e.AsContractName("hey").AsContractType(typeof(IFoo)));
ExportAttribute exportAtt = GetExportAttribute(builder);
Assert.Equal("hey", exportAtt.ContractName);
Assert.Equal(typeof(IFoo), exportAtt.ContractType);
}