Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading info in README #195

Open
YegorStepanov opened this issue Aug 15, 2022 · 2 comments
Open

Misleading info in README #195

YegorStepanov opened this issue Aug 15, 2022 · 2 comments

Comments

@YegorStepanov
Copy link

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.

  1. 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
Copy link
Author

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'

@YegorStepanov YegorStepanov changed the title Typos in README Misleading info in README Aug 15, 2022
@YairHalberstadt
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants