コード例 #1
0
        public string GetSourceCode()
        {
            var writer = new IndentedTextWriter2(2, false);
            var sng    = new ShortTypeNameGenerator();

            foreach (var methodProduct in MethodProducts)
            {
                methodProduct.Write(writer, sng);
            }

            return($@"
{sng.GetCombinedUsings()}

namespace {BindToType.ContainingNamespace.ToFullDisplayString()}
{{
    public partial class {BindToType.Name} : {BindFromType.ToGlobalDisplayString()}
    {{
        {Unknowns.Join(u => $"private readonly {u.Type!.ToGlobalDisplayString()} {u.Name};")}

        public {BindToType.Name}(
            {Unknowns.Join(u => $"{u.Type!.ToGlobalDisplayString()} {u.Name}", ",")}
            )
        {{
            {Unknowns.Join(u => $"this.{u.Name} = {u.Name};")}
        }}

        {writer.GetResultString()}
    }}
}}
");
        }
コード例 #2
0
        public string GetSourceCode()
        {
            var writer = new IndentedTextWriter2(2, false);
            var sng    = new ShortTypeNameGenerator();

            foreach (var methodProduct in MembersProduct)
            {
                methodProduct.Write(writer, sng);
            }

            var compilationUnit = DpdtInject.Generator.Core.Properties.Resource.CarcassProxy;

            var fixedCompilationUnit = compilationUnit
                                       .ReplaceLineStartsWith(
                "namespace",
                $"namespace {BindToType.ContainingNamespace.ToFullDisplayString()}"
                )
                                       .ReplaceLineStartsWith(
                "using BindFromType",
                $""
                )
                                       .ReplaceLineStartsWith(
                "using SessionSaver",
                $""
                )
                                       .CheckAndReplace(
                "//PROXYPRODUCER: additonal usings",
                sng.GetCombinedUsings()
                )
                                       .CheckAndReplace(
                nameof(CarcassProxy),
                BindToType.Name
                )
                                       .CheckAndReplace(
                "BindFromType",
                BindFromType.ToGlobalDisplayString()
                )
                                       .CheckAndReplace(
                "SessionSaver",
                _sessionSaverType.ToGlobalDisplayString()
                )
                                       .CheckAndReplace(
                "//PROXYPRODUCER: put methods here",
                writer.GetResultString()
                )
            ;

            return(fixedCompilationUnit);
        }
コード例 #3
0
        public string Produce(
            IReadOnlyList <UsingDirectiveSyntax> additionalUsings
            )
        {
            if (additionalUsings is null)
            {
                throw new ArgumentNullException(nameof(additionalUsings));
            }

            var instanceProducts    = new List <InstanceProduct>();
            var instanceProductDict = new Dictionary <ITypeSymbol, List <InstanceProduct> >(SymbolEqualityComparer.Default);

            foreach (var bindingExtender in ClusterBindings.BindingExtenders)
            {
                IInstanceProducer instanceProducer;
                switch (bindingExtender.BindingContainer.Scope)
                {
                case BindScopeEnum.Transient:
                    instanceProducer = new TransientInstanceProducer(
                        _typeInfoProvider,
                        ClusterBindings,
                        bindingExtender
                        );
                    break;

                case BindScopeEnum.Singleton:
                    instanceProducer = new SingletonInstanceProducer(
                        _typeInfoProvider,
                        ClusterBindings,
                        bindingExtender
                        );
                    break;

                case BindScopeEnum.Constant:
                    instanceProducer = new ConstantInstanceProducer(
                        _typeInfoProvider,
                        ClusterBindings,
                        bindingExtender
                        );
                    break;

                case BindScopeEnum.Custom:
                    instanceProducer = new CustomInstanceProducer(
                        _typeInfoProvider,
                        ClusterBindings,
                        bindingExtender
                        );
                    break;

                default:
                    throw new ArgumentOutOfRangeException(bindingExtender.BindingContainer.Scope.ToString());
                }

                var instanceProduct = instanceProducer.Produce();
                instanceProducts.Add(instanceProduct);

                foreach (var bindFrom in instanceProduct.BindingExtender.BindingContainer.BindFromTypes)
                {
                    if (!instanceProductDict.ContainsKey(bindFrom))
                    {
                        instanceProductDict[bindFrom] = new List <InstanceProduct>();
                    }

                    instanceProductDict[bindFrom].Add(instanceProduct);
                }
            }

            var resolutionInterfaceProducts = new List <InstanceResolutionInterfaceProduct>();

            foreach (var pair in instanceProductDict)
            {
                var bindFrom = pair.Key;
                var filteredInstanceProducts = pair.Value;

                var resolutionInterfaceProducer = new BindFromResolutionProducer(
                    _typeInfoProvider,
                    ClusterBindings,
                    bindFrom,
                    filteredInstanceProducts
                    );

                var resolutionInterfaceProduct = resolutionInterfaceProducer.Produce();
                resolutionInterfaceProducts.Add(resolutionInterfaceProduct);
            }


            var customInstanceContainerSize = ClusterBindings.BindingContainers.Count(bc => bc.Scope == BindScopeEnum.Custom);

            customInstanceContainerSize *= ArgumentWrapperHelper.WrapperCountIncludeNone;
            customInstanceContainerSize *= 2; //factor of 2 for additional sparsity;
            customInstanceContainerSize += 1; //addition of 1 is because of 0 is not allowed size for container

            var compilationUnit = DpdtInject.Generator.Core.Properties.Resource.CarcassCluster;

            var usings = new HashSet <string>(StringComparer.InvariantCulture);

            foreach (var additionalUsing in additionalUsings)
            {
                usings.Add(additionalUsing.ToString());
            }

            var sng = new ShortTypeNameGenerator();


            var itwMethods                    = new IndentedTextWriter2(2, _doBeautify);
            var itwInterfaces                 = new IndentedTextWriter2(3, _doBeautify);
            var itwNonGenericInterfaces       = new IndentedTextWriter2(4, _doBeautify);
            var itwNonGenericGetAllInterfaces = new IndentedTextWriter2(4, _doBeautify);

            if (resolutionInterfaceProducts.Count > 0)
            {
                itwInterfaces.WriteLine(",");
            }

            foreach (var(resolutionInterfaceProduct, isLast) in resolutionInterfaceProducts.IterateWithLastSignal())
            {
                resolutionInterfaceProduct.Write(
                    isLast,
                    itwMethods,
                    itwInterfaces,
                    itwNonGenericInterfaces,
                    itwNonGenericGetAllInterfaces,
                    sng
                    );
            }


            var itwDispose                 = new IndentedTextWriter2(3, _doBeautify);
            var itwCombinedBody            = new IndentedTextWriter2(4, _doBeautify);
            var itwCombinedUnknownTypeBody = new IndentedTextWriter2(4, _doBeautify);

            foreach (var instanceProduct in instanceProducts)
            {
                instanceProduct.WriteDisposeMethodInvoke(itwDispose, sng);
                instanceProduct.WriteCombinedBody(itwCombinedBody, sng);
                instanceProduct.WriteCombinedUnknownTypeBody(itwCombinedUnknownTypeBody, sng);
            }

            sng.WriteUsings(usings);

            var fixedCompilationUnit = compilationUnit
                                       .CheckAndReplace(
                "//GENERATOR: aggressive inline and optimize",
                "[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining | global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveOptimization)]"
                )
                                       .ReplaceLineStartsWith(
                "namespace",
                $"namespace {ClusterBindings.ClusterType.ContainingNamespace.ToFullDisplayString()}"
                )
                                       .CheckAndReplace(
                nameof(CarcassCluster),
                ClusterBindings.ClusterType.Name
                )
                                       .ReplaceLineContains(
                "//GENERATOR: place for conditional binding count",
                customInstanceContainerSize.ToString()
                )
                                       .CheckAndReplace(
                "//GENERATOR: place for an additional usings",
                usings.Join(a => a)
                )
                                       .AsStringBuilder()
                                       .Replace(
                "//GENERATOR: add nongeneric GET binding",
                itwNonGenericInterfaces.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: add nongeneric GET ALL binding",
                itwNonGenericGetAllInterfaces.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: place for a dispose clauses",
                itwDispose.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: place for a resolution interfaces",
                itwInterfaces.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: place for an instance interface providers",
                itwMethods.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: place for an instance object producers",
                itwCombinedBody.GetResultString()
                )
                                       .Replace(
                "//GENERATOR: place for an unknown type resolutions",
                itwCombinedUnknownTypeBody.GetResultString()
                )
                                       .ToString();

            return(fixedCompilationUnit);
        }