Skip to content

Fluent API not generating code from other assembly #306

@danilo73r

Description

@danilo73r

First, thanks for this awsome tool !!!

Issue description

When trying to generate code referencing OTHER_ASSEMBLY in fluent api, nothing happens.

Steps to reproduce the issue

  1. Install Mapster.Tool in a project
  2. Copy "Generate automatically on build" tags on csproj file
  3. Create a POCO class inside OTHER_ASSEMBLY.
  4. Create a "fluent api configuration class" inside the project:
    config.AdaptTo("[name]Dto")
            .ForAllTypesInNamespace(OTHER_ASSEMBLY, "POCO..Namespace");
  1. Build

What's the expected result?

  • Generated Dtos in Models Folder inside current Assembly.

What's the actual result?

  • nothing happens

Additional details

I review the code. Maybe I'm wrong, but in this method:

private static void GenerateModels(ModelOptions opt)
{
using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly));
var assembly = dynamicContext.Assembly;
var codeGenConfig = new CodeGenerationConfig();
codeGenConfig.Scan(assembly);
foreach (var type in assembly.GetTypes())
{
var builders = type.GetAdaptAttributeBuilders(codeGenConfig)
.Where(it => !string.IsNullOrEmpty(it.Attribute.Name) && it.Attribute.Name != "[name]")
.ToList();
if (builders.Count == 0)
continue;
Console.WriteLine($"Processing: {type.FullName}");
foreach (var builder in builders)
{
CreateModel(opt, type, builder);
}
}
}

the current ASSEMBLY is used, but inside the codeGenConfig.Scan method:
public static void Scan(this CodeGenerationConfig config, Assembly assembly)
{
var registers = assembly.GetTypes()
.Where(x => typeof(ICodeGenerationRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) &&
x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract)
.Select(type => (ICodeGenerationRegister) Activator.CreateInstance(type)!);
foreach (var register in registers)
{
register.Register(config);
}
}

when Register is called, my ForAllTypesInNamespace with OTHER_ASSEMBLY is called.

So, I think the problem is in the foreach of the GenerateModels method. There, the GetTypes() of the current ASSEMBLY is called (ignoring completely the OTHER_ASSEMBLY types).

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions