Ejemplo n.º 1
0
    /// <inheritdoc />
    public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName)
    {
        // Use IDynamicAssemblyBuilder.AddReferencedAssembly(string assemblyPath) or
        // IDynamicAssemblyBuilder.AddReferencedAssembly(Type typeInAssembly) to add assemblies that will be
        // referenced by auto-generated assembly if types in these assemblies are used in auto-generated code.
        dynamicAssemblyBuilder.AddReferencedAssembly(Path.Combine(Helpers.GetTestFilesFolderPath(), @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly1.dll"));
        dynamicAssemblyBuilder.AddReferencedAssembly(typeof(IInterface1));

        // By now Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo) already validated
        // that a single method with attribute is present in interface.
        var methodInfo = customAutoGeneratedServiceInfo.ImplementedInterface.GetMethods().First();

        var attribute = (SimpleMethodMetadataAttribute)methodInfo.GetCustomAttributes().FirstOrDefault(x => x is SimpleMethodMetadataAttribute);

        var dynamicClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName,
                                                                                 new[]
        {
            customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass()
        },
                                                                                 generatedClassNamespace);

        var methodData = dynamicClass.StartInterfaceImplementationMethod(methodInfo, false);

        methodData.AddCodeLine("{");

        methodData.AddCodeLine("var testReferencedAssembly = new DynamicallyLoadedAssembly1.Dog(40);");

        methodData.AddCodeLine($"return {attribute.ReturnedValue};");
        methodData.AddCodeLine("}");
    }
Ejemplo n.º 2
0
 public void ValidateOnIoCContainerLoaded(IDiContainer diContainer, ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
 {
     if (_simulateValidateOnIoCContainerLoadedFails)
     {
         throw new Exception($"{nameof(ValidateOnIoCContainerLoaded)} failed.");
     }
 }
Ejemplo n.º 3
0
 public void Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
 {
     if (_simulateValidateFails)
     {
         throw new Exception($"{nameof(Validate)} failed.");
     }
 }
 public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo,
                            IDynamicAssemblyBuilder dynamicAssemblyBuilder,
                            string generatedClassNamespace, string generatedClassName)
 {
     GenerateCSharpCode(dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName,
                                                                              new[]
     {
         customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass()
     }, generatedClassNamespace));
 }
    public void ValidateOnIoCContainerLoaded(IDiContainer diContainer, ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
    {
        var settings = diContainer.Resolve <ISettings>();

        if (settings.GetSettingValue <bool>("failCustomServiceValidation", false, out var failCustomServiceValidation))
        {
            if (failCustomServiceValidation)
            {
                throw new Exception($"Validation {nameof(ValidateOnIoCContainerLoaded)} failed because the value of setting 'failCustomServiceValidation' is true");
            }
        }
    }
Ejemplo n.º 6
0
    public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName)
    {
        if (_simulateGenerateCSharpFailure)
        {
            throw new Exception($"{nameof(GenerateCSharp)} failed.");
        }

        var dynamicallyGeneratedClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName,
                                                                                              new [] { customAutoGeneratedServiceInfo.ImplementedInterface.FullName }, generatedClassNamespace);

        dynamicallyGeneratedClass.AddCode("public override int GetColor() => 10");

        if (!_simulateAssemblyBuildFailure)
        {
            dynamicallyGeneratedClass.AddCode(";");
        }
    }
    public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo,
                               IDynamicAssemblyBuilder dynamicAssemblyBuilder,
                               string generatedClassNamespace, string generatedClassName)
    {
        dynamicAssemblyBuilder.AddReferencedAssembly(
            Path.Combine(Helpers.GetTestFilesFolderPath(),
                         @"DynamicallyLoadedDlls\TestProjects.DynamicallyLoadedAssembly1.dll"));

        dynamicAssemblyBuilder.AddReferencedAssembly(
            typeof(SharedServices.Interfaces.IInterface1));

        var classInfo = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(
            generatedClassName, generatedClassNamespace);

        var methodInfo = classInfo.StartMethod("GetValue", typeof(int),
                                               Array.Empty <IMethodParameterInfo>(),
                                               AccessLevel.Public, false, false);

        methodInfo.AddCodeLine("=> 10;");
    }
        //public IReferencedAssembliesElement ReferencedAssembliesElement { get; private set; }

        public override void ValidateAfterChildrenAdded()
        {
            base.ValidateAfterChildrenAdded();

            _customAutoGeneratedServiceInfo = new CustomAutoGeneratedServiceInfo(ImplementedInterfaceTypeInfo.Type);

            try
            {
                AutoServiceCodeGenerator.CustomAutoServiceCodeGenerator.Validate(_customAutoGeneratedServiceInfo);
            }
            catch (Exception e)
            {
                LogHelper.Context.Log.Error(e);

                throw new ConfigurationParseException(this, string.Format("Call to '{0}.{1}({2})' failed.",
                                                                          AutoServiceCodeGenerator.CustomAutoServiceCodeGenerator.GetType().FullName,
                                                                          nameof(ICustomAutoServiceCodeGenerator.Validate),
                                                                          typeof(ICustomAutoGeneratedServiceInfo).FullName));
            }
        }
Ejemplo n.º 9
0
    public void GenerateCSharp(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo, IDynamicAssemblyBuilder dynamicAssemblyBuilder, string generatedClassNamespace, string generatedClassName)
    {
        var dynamicClass = dynamicAssemblyBuilder.StartDynamicallyGeneratedClass(generatedClassName,
                                                                                 new[]
        {
            customAutoGeneratedServiceInfo.ImplementedInterface.GetTypeNameInCSharpClass()
        },
                                                                                 generatedClassNamespace);

        foreach (var referencedAssemblyPath in ReferencedAssemblyPathsToAddOnCodeGeneration)
        {
            dynamicAssemblyBuilder.AddReferencedAssembly(referencedAssemblyPath);
        }

        var methodInfo = customAutoGeneratedServiceInfo.ImplementedInterface.GetMethods()
                         .First(x => x.Name == nameof(IDogWeightsCalculator.GetDogWeightInKilograms));

        var methodData = dynamicClass.StartInterfaceImplementationMethod(methodInfo, false);

        methodData.AddCodeLine("=> (int)(0.453592 * (new DynamicallyLoadedAssembly1.Dog(40)).Weight);");
    }
Ejemplo n.º 10
0
    /// <inheritdoc />
    public void Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
    {
        var implementedInterfaceType = customAutoGeneratedServiceInfo.ImplementedInterface;

        if (!implementedInterfaceType.IsInterface ||
            implementedInterfaceType.GetInterfaces().Length > 0 ||
            implementedInterfaceType.GetMethods().Length != 1 ||
            implementedInterfaceType.GetProperties().Length != 0)
        {
            throw new Exception($"The demo auto-implemented interface should not have a parent interfaces and should have exactly one method.");
        }

        var methodInfo = implementedInterfaceType.GetMethods().First();

        if (methodInfo.GetParameters().Length != 0 || methodInfo.ReturnType != typeof(int))
        {
            throw new Exception($"The demo auto-implemented method should be veryyy simple to be short!!.");
        }

        if (methodInfo.GetCustomAttributes().FirstOrDefault(x => x is SimpleMethodMetadataAttribute) == null)
        {
            throw new Exception($"Method should have an attribute of type '{typeof(SimpleMethodMetadataAttribute)}'.");
        }
    }
 public void ValidateOnIoCContainerLoaded(IDiContainer diContainer,
                                          ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
 {
 }
 public void Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
 {
 }
Ejemplo n.º 13
0
 /// <inheritdoc />
 public void ValidateOnIoCContainerLoaded(IDiContainer diContainer, ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
 {
     // At this point the DI container diContainer is loaded. Do validation using some services in container
     // and throw an exception if necessary
     //diContainer.Resolve()
 }
    public void Validate(ICustomAutoGeneratedServiceInfo customAutoGeneratedServiceInfo)
    {
        _repositoryInterfaceType = customAutoGeneratedServiceInfo.ImplementedInterface;

        if (!_repositoryInterfaceType.IsInterface)
        {
            throw new Exception($"The repository class should be an interface. The class is '{_repositoryInterfaceType.FullName}'.");
        }

        if (_repositoryInterfaceType.GetInterfaces().Length > 0)
        {
            throw new Exception($"The repository interface'{_repositoryInterfaceType.FullName}' cannot have base interfaces.");
        }

        var attribute = _repositoryInterfaceType.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(DataRepositoryAttribute));

        if (attribute is not DataRepositoryAttribute dataRepositoryAttribute)
        {
            throw new Exception($"Attribute '{typeof(DataRepositoryAttribute)}' is missing in data repository '{_repositoryInterfaceType}'.");
        }

        if (_repositoryInterfaceType.GetProperties().ToList().Count > 0)
        {
            throw new Exception($"No properties are supported in repository interfaces that use attribute '{typeof(DataRepositoryAttribute)}'.");
        }

        _databaseEntityType = dataRepositoryAttribute.DatabaseEntityType;

        foreach (var methodInfo in _repositoryInterfaceType.GetMethods())
        {
            var attributes = methodInfo.GetCustomAttributes().ToList();

            if (attributes.Count == 0)
            {
                var validAttributeTypes = new Type[]
                {
                    typeof(RepositoryMethodMetadataForAddAttribute), typeof(RepositoryMethodMetadataForAddOrUpdateAttribute),
                    typeof(RepositoryMethodMetadataForDeleteAttribute), typeof(RepositoryMethodMetadataForSelectAttribute)
                };
                throw new Exception($"Only methods with one of the following attributes are supported in repository interface '{_repositoryInterfaceType}': {string.Join(",", validAttributeTypes.Select(x => x.FullName))}.");
            }

            foreach (var currentAttribute in attributes)
            {
                if (currentAttribute is RepositoryMethodMetadataForAddAttribute repositoryMethodMetadataForAddAttribute)
                {
                    ValidateRepositoryMethodSignature(methodInfo, new[] { _databaseEntityType }, new TypeInfo(_databaseEntityType));
                    _repositoryMethodMetadataForAdd.Add(new RepositoryMethodMetadata <RepositoryMethodMetadataForAddAttribute>(repositoryMethodMetadataForAddAttribute, methodInfo));
                }
                else if (currentAttribute is RepositoryMethodMetadataForAddOrUpdateAttribute repositoryMethodMetadataForAddOrUpdateAttribute)
                {
                    ValidateRepositoryMethodSignature(methodInfo, new[] { _databaseEntityType }, new TypeInfo(_databaseEntityType));
                    _repositoryMethodMetadataForAddOrUpdate.Add(new RepositoryMethodMetadata <RepositoryMethodMetadataForAddOrUpdateAttribute>(repositoryMethodMetadataForAddOrUpdateAttribute, methodInfo));
                }
                else if (currentAttribute is RepositoryMethodMetadataForDeleteAttribute repositoryMethodMetadataForDeleteAttribute)
                {
                    ValidateRepositoryMethodSignature(methodInfo, new[] { _databaseEntityType }, new TypeInfo("System", "Void"));
                    _repositoryMethodMetadataForDelete.Add(new RepositoryMethodMetadata <RepositoryMethodMetadataForDeleteAttribute>(repositoryMethodMetadataForDeleteAttribute, methodInfo));
                }
                else if (currentAttribute is RepositoryMethodMetadataForSelectAttribute repositoryMethodMetadataForSelectAttribute)
                {
                    if (repositoryMethodMetadataForSelectAttribute.IsSelectAll)
                    {
                        ValidateRepositoryMethodSignature(methodInfo, Type.EmptyTypes,
                                                          new TypeInfo("System.Collections.Generic", "IReadOnlyList`1",
                                                                       new List <Type>
                        {
                            _databaseEntityType
                        }));

                        _repositoryMethodMetadataForSelectAll.Add(new RepositoryMethodMetadata <RepositoryMethodMetadataForSelectAttribute>(repositoryMethodMetadataForSelectAttribute, methodInfo));
                    }
                    else
                    {
                        ValidateRepositoryMethodSignature(methodInfo, new Type[] { typeof(long) },
                                                          new TypeInfo(_databaseEntityType));
                        _repositoryMethodMetadataForSelectSingle.Add(new RepositoryMethodMetadata <RepositoryMethodMetadataForSelectAttribute>(repositoryMethodMetadataForSelectAttribute, methodInfo));
                    }
                }
                else
                {
                    throw new Exception($"Invalid attribute specified for repository interface '{_repositoryInterfaceType}'.");
                }
            }
        }

        attribute = _databaseEntityType.GetCustomAttributes().FirstOrDefault(x => x.GetType() == typeof(DatabaseEntityAttribute));

        _databaseEntityAttribute = attribute as DatabaseEntityAttribute;

        if (_databaseEntityAttribute == null)
        {
            throw new Exception($"Attribute '{typeof(DatabaseEntityAttribute)}' is missing in data repository entity class '{dataRepositoryAttribute.DatabaseEntityType}' in repository '{_repositoryInterfaceType}'.");
        }

        foreach (var propertyInfo in _databaseEntityType.GetProperties())
        {
            var attributes = propertyInfo.GetCustomAttributes();

            foreach (var currentAttribute in attributes)
            {
                if (currentAttribute is not ColumnAttribute columnAttribute)
                {
                    continue;
                }

                var columnMetadata = new ColumnMetadata(columnAttribute, propertyInfo);

                if (columnAttribute.IsKeyAttribute)
                {
                    if (_keyColumnMetadata == null)
                    {
                        _keyColumnMetadata = columnMetadata;
                    }
                    else
                    {
                        LogHelper.Context.Log.ErrorFormat("Only one key column attribute can be specified. Column {0} will not be used as  a key column. Entity type with multiple key columns: '{1}'.",
                                                          columnMetadata.ColumnName, _databaseEntityType);
                    }
                }

                _columnsMetadata.Add(columnMetadata);
            }
        }

        if (_keyColumnMetadata == null)
        {
            throw new Exception($"No key column was specified in database entity type '{_databaseEntityType}'.");
        }
    }