JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
563 stars 118 forks source link

Allow scanning internal classes with default conventions #288

Closed rchamorro closed 2 years ago

rchamorro commented 2 years ago

I would like to expose the interfaces from my assembly but hide the real implementations marking them as internal, but I have found no way of scanning internal classes. If I add them manually it works but it loses the magic that Lamar provides.

I tried to use a custom IRegistrationConvention but the types that were provided in ScanTypes TypeSet where all public and the TypeRepository that provides it is a static class that I am not able to override.

Here is the code I expected to work:

using System;
using Lamar;

namespace TestLamarScanInternalClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new Container(s =>
            {
                s.Scan(x =>
                {
                    x.AssemblyContainingType<IWidget>();
                    x.WithDefaultConventions(); // May be we should be able to tell here that we also want internal classes here with an enum? 
                });
            });

            // This throws exception
            var _= container.GetInstance<IWidget>();

        }

    }

    public interface IWidget
    {

    }

    internal class Widget : IWidget
    {

    }
}

Is there a way I can achieve this behaviour? If not, should It be implemented?

jeremydmiller commented 2 years ago

BaselineTypeDiscovery (the actual type scanning lib) uses Assembly.GetExportedTypes() to scan through assemblies, and that only exposes public types. I'm not enthusiastic about making enough changes in both BaselineTypeDiscovery and Lamar to opt into being able to scan internal types.

Plus, is it really that important in the end? If the users of those classes use the interfaces anyway.

Sorry.