YairHalberstadt / stronginject

compile time dependency injection for .NET
MIT License
845 stars 24 forks source link

Misleading info in README #195

Open YegorStepanov opened 2 years ago

YegorStepanov commented 2 years ago

You can register a type implementing IFactory as a Factory Registration. This will automatically register it as both IFactory and T.

1) [FactoryRegistration] has been renamed to [RegisterFactory]. Is Factory Registration naming outdated here?

2) [RegisterFactory] doesn't register factory, only T. Is it bug in code or outdated info in docs?:

[RegisterFactory(typeof(Factory))]
public partial class Container : IContainer<IFactory<int>> { }

public class Factory : IFactory<int> { public int Create() => 0; }

new Container().Run(_ => { });

// leads to error: 
// [SI0102] Error while resolving dependencies for 'StrongInject.IFactory<int>': We have no source for instance of type 'StrongInject.IFactory<int>'
//
// if we add [Register(typeof(Factory), typeof(IFactory<string>))] it works fine, but there is a warning now:
// [SI1001] 'Factory' implements 'StrongInject.IFactory<string>'. Did you mean to use FactoryRegistration instead?

FactoryRegistration is outdated in warning too.

3) In readme:

[RegisterFactory(typeof(InterfaceArrayFactory), scope: Scope.SingleInstance, factoryTargetScope: Scope.InstancePerResolution, typeof(IFactory<IInterface[]>))]

scope parameter should be factoryScope instead and [RegisterFactory] doesn't have parameter Type registerAs.

YegorStepanov commented 2 years ago

4.

Optional Parameters If a parameter to a type or method is optional, StrongInject will not error if it cannot be resolved, and will instead just use the default value.

An example of where this can be useful is for providing a default instance of an interface if none is registered:

public class DefaultImplementation : IInterface {} public interface IInterface {}

[Register(typeof(DefaultImplementation))]
public class Module
{
  [Decorator] GetIInterface(IInterface? impl = null, DefaultImplementation defaultImpl) => impl ?? defaultImpl;
}

Not working for me:

[Register<Service>]
[Register(typeof(DefaultImplementation))]
public partial class Container : IContainer<Service>
{
    [DecoratorFactory] IInterface GetIInterface(DefaultImplementation defaultImpl, IInterface? impl = null) => impl ?? defaultImpl;
}

public class DefaultImplementation : IInterface { }

public record Service(IInterface _);

//  [SI0102] Error while resolving dependencies for 'Service': We have no source for instance of type 'IInterface'
YairHalberstadt commented 2 years ago

Thanks for all the issues. I will look into them when I get a chance.