/// <summary>
        /// Registers the conventions.
        /// </summary>
        /// <param name="builder">The registration builder.</param>
        /// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
        public void RegisterConventions(IConventionsBuilder builder, IEnumerable<TypeInfo> candidateTypes)
        {
            var conventions = builder;
            var typeInfos = candidateTypes.ToList();

            // get all type infos from the composition assemblies
            var appServiceContractsInfos =
                typeInfos.ToDictionary(ti => ti, ti => ti.GetCustomAttribute<AppServiceContractAttribute>())
                    .Where(ta => ta.Value != null)
                    .ToList();

            foreach (var appServiceContractInfo in appServiceContractsInfos)
            {
                var serviceContract = appServiceContractInfo.Key;
                var serviceContractType = serviceContract.AsType();
                var serviceContractMetadata = appServiceContractInfo.Value;

                var partBuilder = this.TryGetPartBuilder(serviceContractMetadata, serviceContract, conventions, typeInfos);

                if (partBuilder == null)
                {
                    continue;
                }

                var exportedContractType = serviceContractMetadata.ContractType ?? serviceContractType;
                var exportedContract = exportedContractType.GetTypeInfo();
                this.CheckExportedContractType(exportedContractType, serviceContract, serviceContractType);

                var metadataAttributes = appServiceContractInfo.Value.MetadataAttributes;
                if (exportedContract.IsGenericTypeDefinition)
                {
                    if (serviceContractMetadata.AsOpenGeneric)
                    {
                        partBuilder.ExportInterfaces(
                            t => this.IsClosedGenericOf(exportedContract, t.GetTypeInfo()),
                            (t, b) => this.ConfigureExport(serviceContract, b, exportedContractType, metadataAttributes));
                    }
                    else
                    {
                        partBuilder.ExportInterfaces(
                            t => this.IsClosedGenericOf(exportedContract, t.GetTypeInfo()),
                            (t, b) => this.ConfigureExport(serviceContract, b, t, metadataAttributes));
                    }
                }
                else
                {
                    partBuilder.Export(
                        b => this.ConfigureExport(serviceContract, b, exportedContractType, metadataAttributes));
                }

                partBuilder.SelectConstructor(this.SelectAppServiceConstructor);

                partBuilder.ImportProperties(pi => this.IsAppServiceImport(pi, appServiceContractsInfos));

                if (serviceContractMetadata.IsShared)
                {
                    partBuilder.Shared();
                }
            }
        }
 public void RegisterConventions(IConventionsBuilder builder, IEnumerable<TypeInfo> candidateTypes)
 {
     builder
         .ForTypesDerivedFrom(typeof(ICalculator))
         .Export(
             b => b.AsContractType(typeof(ICalculator))
                   .AddMetadata("type", t => t.Name.StartsWith("Scientific") ? "Scientific" : "Classical"))
         .Shared();
 }
Пример #3
0
 public BusPublish(
     IOptions <RabbitMqOptions> options,
     IConventionsBuilder conventionsBuilder,
     IConnection connection)
 {
     _options            = options.Value;
     _conventionsBuilder = conventionsBuilder;
     _connection         = connection;
 }
Пример #4
0
        /// <summary>
        /// Registers the conventions.
        /// </summary>
        /// <param name="builder">The registration builder.</param>
        /// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
        /// <exception cref="System.InvalidOperationException">The provided conventions must be MEF conventions.</exception>
        public void RegisterConventions(IConventionsBuilder builder, IEnumerable<TypeInfo> candidateTypes)
        {
            var mefBuilder = builder as IMefConventionBuilderProvider;
            if (mefBuilder == null)
            {
                throw new InvalidOperationException(string.Format(Strings.InvalidConventions, typeof(IMefConventionBuilderProvider)));
            }

            this.RegisterConventions(mefBuilder.GetConventionBuilder());
        }
Пример #5
0
 public BusSubscribe(
     IOptions <RabbitMqOptions> options,
     IConventionsBuilder conventionsBuilder,
     IConnection connection,
     IServiceScopeFactory serviceScopeFactory)
 {
     _options            = options.Value;
     _conventionsBuilder = conventionsBuilder;
     _connection         = connection;
 }
Пример #6
0
 /// <summary>
 /// Registers the conventions.
 /// </summary>
 /// <param name="builder">The registration builder.</param>
 /// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
 public void RegisterConventions(IConventionsBuilder builder, IEnumerable<TypeInfo> candidateTypes)
 {
     Contract.Requires(builder != null);
     Contract.Requires(candidateTypes != null);
 }
Пример #7
0
 public ConventionsProvider(IConventionsBuilder builder)
 {
     _builder = builder;
 }
        /// <summary>
        /// Tries to get the part builder.
        /// </summary>
        /// <param name="serviceContractMetadata">The service contract metadata.</param>
        /// <param name="serviceContract">The service contract.</param>
        /// <param name="conventions">The conventions.</param>
        /// <param name="typeInfos">The type infos.</param>
        /// <returns>
        /// The part builder or <c>null</c>.
        /// </returns>
        private IPartConventionsBuilder TryGetPartBuilder(
            AppServiceContractAttribute serviceContractMetadata,
            TypeInfo serviceContract,
            IConventionsBuilder conventions,
            IEnumerable<TypeInfo> typeInfos)
        {
            var serviceContractType = serviceContract.AsType();

            if (serviceContract.IsGenericTypeDefinition)
            {
                // if there is non-generic service contract with the same full name
                // then add just the conventions for the derived types.
                return conventions.ForTypesMatching(t => this.MatchOpenGenericContractType(t, serviceContractType));
            }

            if (serviceContractMetadata.AllowMultiple)
            {
                // if the service contract metadata allows multiple service registrations
                // then add just the conventions for the derived types.
                return conventions.ForTypesDerivedFrom(serviceContractType);
            }

            var parts =
                typeInfos.Where(
                    ti =>
                    serviceContract.IsAssignableFrom(ti) && ti.IsClass && !ti.IsAbstract
                    && ti.GetCustomAttribute<ExcludeFromCompositionAttribute>() == null).ToList();
            if (parts.Count == 1)
            {
                return conventions.ForType(parts[0].AsType());
            }

            if (parts.Count > 1)
            {
                var overrideChain =
                    parts.ToDictionary(
                        ti => ti,
                        ti =>
                        ti.GetCustomAttribute<OverridePriorityAttribute>() ?? new OverridePriorityAttribute(Priority.Normal))
                        .OrderBy(item => item.Value.Value)
                        .ToList();

                var selectedPart = overrideChain[0].Key;
                if (overrideChain[0].Value.Value == overrideChain[1].Value.Value)
                {
                    throw new InvalidOperationException(
                        string.Format(
                            Strings.AmbiguousOverrideForAppServiceContract,
                            serviceContract,
                            selectedPart,
                            string.Join(", ", overrideChain.Select(item => item.Key.ToString() + ":" + item.Value.Value))));
                }

                return conventions.ForType(selectedPart.AsType());
            }

            return null;
        }
Пример #9
0
 public ConventionsProvider(IConventionsRegistry registry, IConventionsBuilder builder)
 {
     _registry = registry;
     _builder  = builder;
 }
Пример #10
0
 protected internal virtual void OnConventionsCreating(IConventionsBuilder conventionsBuilder)
 {
 }