GithubHelp home page GithubHelp logo

abioc's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar jskimming avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

abioc's Issues

Registering a mapping before a factory can cause an exception

If a mapping from an interface to a concrete class comes before a factory mapping, then an exception will be thrown is the concrete class cannot be instantiated, e.g. it has multiple constructors.

This was discovered when mapping the Amazon S3 client to the interface, like this:

setup
    .Register<IAmazonS3, AmazonS3Client>()
    .RegisterFactory<AmazonS3Client>(CreateAmazonS3Client);

AmazonS3Client CreateAmazonS3Client()
{
    // Factory function implementation.
}

Since AmazonS3Client has multiple public constructors, the single constructor registration throw an exception when visited, but since it will be overridden, it should not throw an error.

If they the registrations are swapped, then no exception is thrown.

setup
    .RegisterFactory<AmazonS3Client>(CreateAmazonS3Client)
    .Register<IAmazonS3, AmazonS3Client>();

Multiple mappings are not handled correctly

If an interface is mapped to a concrete class, like this:

setup.Register<IService, Service>();

And later the concrete class registration is specialised to a factory:

setup.RegisterFactory(() => new Service());

This results in duplicate mappings, where the service cannot be resolved using GetService<IService>() and if GetServices<IService>() is called, two instances are returned.

This is caused because both the default (single construct) registration, and the factory registration are interpreted as a mapping, and therefore results in two mappings to the same.

Factory functions that return a generic produce invalid code

I've found this with MediatR when producing a notification handler. A field with this name is created

Factor_MediatR_ICancellableAsyncNotificationHandler`1[[Example_Notification, Example, Version=0_0_0_1, Culture=neutral, PublicKeyToken=null]]

It should be something like this:

Factor_MediatR_ICancellableAsyncNotificationHandler__Example_Notification__

Auto discover assemblies

If a registration requires several assemblies then they all currently need to be manually added to ensure the generated code correctly compiles. This can be quite cumbersome.

It would be better if the assemblies were auto discovered based upon the types used during compilation. There may be circumstances where custom assemblies may require adding, therefore them mechanism of adding assemblies should remain, though hopefully that becomes the exception.

Refactor and consolidate IParameterExpression

Problem

There are two near identical implementations for retrieving injected dependencies, originally constructor dependencies, and lateley property dependecies.

The logic in both implementations encapsulates:

  • Finding the composition for a dependency.
  • Handling IEnumerable<> dependencies.
  • Throwing a CompositionException for missing dependencies.

I'm not sure a third implementation could arise, (though it's possible), but it's likely a broader set of collections add amplified types will be supported, e.g. arrays, List<>, IReadOnlyCollection<>, Func<>, Lazy<T> etc. Therefore consolidating this into a single implementation would be beneficial and DRY.

Proposal

Not sure yet, most likely an internal static helper method.

Added RegisterInternal

This will allow internal dependencies to be resolved, without forming part of the final map.

Refactor the Registration Visitor initialization

Problem

The current mechanism of Registration Visitor initialization uses an Initialize function on the IRegistrationVisitor interface. When further initialization was needed the IRegistrationVisitorEx interface was introduced.

What happens when even more initialization is required IRegistrationVisitorExEx? It's got a bit of a code smell.

Proposal

Instead of using an Initialize method on the IRegistrationVisitor interface, Registration Visitors should express their initialization requirements via constructor parameters (this is an IoC project after all). A very limited (TBD) set of potential objects can be specified.

The VisitorFactory should then be updated to reflect upon the discovered visitors, to dynamically generate a Factory function, but rather than generate Func<object>, functions generate Func<PotentialArg1, PotentialArg2, object> functions.

The dynamically generated expression tree will then use the appropriate argument on the constructor. See CodeCompilation.CreateContainerFactory for another example.

Steps

  • Refactor the VisitorFactory to reflect visitor construct arguments
  • Generate Func<PotentialArg1, PotentialArg2, object> factory functions
  • Update the various Registration Visitors to use constructor initializaton.
  • Remove Initialize from IRegistrationVisitor and delete IRegistrationVisitorEx

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.