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

ExportExpression #113

Closed
ipjohnson opened this issue Aug 24, 2017 · 11 comments
Closed

ExportExpression #113

ipjohnson opened this issue Aug 24, 2017 · 11 comments
Milestone

Comments

@ipjohnson
Copy link
Owner

A user suggested offer an option to export a linq expression in addition to ExportFactory<T>. The idea would be that calls to Arg.Locate<T>() would be replaced by the container with the proper dependency.

This could allow the developer to make more interesting exports that have runtime logic in them while still maintaining the injection hierarchy.

block.ExportExpression(() => new DependentService(Arg.Locate<IBasicService>()));
@ipjohnson
Copy link
Owner Author

ipjohnson commented Aug 24, 2017

@ElMaxxi I opened this issue up to track the work for this but now I'm remembering the limitations involved with lambda to expression tree conversions.

Currently there is not support for a number of C# language features including but not limited to statement body. This is a huge limiting factor and for me makes me it not worth implementing.

If MS decides to implement more features for expression trees then this would be cool but right now it doesn't do anything that can't be done using the current configuration methods for Export<T>

@ipjohnson ipjohnson added this to the 6.3.0 milestone Sep 1, 2017
@ipjohnson
Copy link
Owner Author

Thinking about it a bit more it really doesn't add much to implement so I'll include it.

@canhazcodez
Copy link

I believe that adding such an option, will allow other "gracers" a bit more creativity on the creation of their types. Can't wait to see how it'll turn out :-)

@ipjohnson
Copy link
Owner Author

@ElMaxxi What I've added so far is ExportExpression<T>(Expression<Func<T>> expression) allowing for something like

c.ExportExpression(() => 
      new SomeClass(Arg.Locate<IDep1>(), Arg.Locate<IDep2>(), "stringValue", false)).LifeStyle.Singleton();

@canhazcodez
Copy link

@ipjohnson this looks pretty cool. Is it possible to throw in an IExportLocatorScope to the arguments of the expression? just as is possible with the exportfactory?

@ipjohnson
Copy link
Owner Author

@ElMaxxi Something like this

c.ExportExpression<IExportLocatorScope,SomeClass>(
     scope => new SomeClass(Arg.Locate<IDep1>(), scope.Locate<IDep2>(new { someString = "value" });

@canhazcodez
Copy link

@ipjohnson well i didn't mind the Arg, i just thought that having access to the site will let me have access to scope properties to help with the logic. Something enabling, for example, a different logic, if the scope name matches a specific scope.

@ipjohnson
Copy link
Owner Author

What if I put in Arg.Scope() and that gave you access to the scope so you could do something like

c.ExportExpression<ISomeClass>(
       () => Arg.Scope().Name == "Blah" ? new SomeClass() : new OtherClass())

@canhazcodez
Copy link

That would be totally fine.
I really appreciate your prompt followup on this :-)

ipjohnson added a commit that referenced this issue Sep 4, 2017
@ipjohnson
Copy link
Owner Author

I've added Arg.Scope() and Arg.Context()

I also added something that I thought was kind of interesting Arg.Locate(new { }) where you can specify how some dependencies are resolved.

Arg.Locate(new { SomeValue = 5 }) will use the value 5 for int dependencies named someValue.

Here are the tests I've added

@ipjohnson
Copy link
Owner Author

I've pushed a beta version to test this, let me know if you have any other suggestions.

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

No branches or pull requests

2 participants