aspnet / DependencyInjection

[Archived] Contains common DI abstractions that ASP.NET Core and Entity Framework Core use. Project moved to https://github.com/aspnet/Extensions
Apache License 2.0
873 stars 318 forks source link

Allow use outside of ASP.NET #99

Closed erlimar closed 9 years ago

erlimar commented 10 years ago

Add support for using Microsoft.Framework.DependencyInjection in non ASP.NET applications, console applications eg.

public class Program
{
    public static void Main(string[] args)
    {
          var services = new ServiceCollection();
          services.AddTransient<ISampleType, SampleType>();
          var provider = new DefaultServiceProvider(services);
          var myType = provider.GetService<ISampleType>();
    }
}
davidfowl commented 10 years ago

Any reason you would use this over a real container that already works everywhere else like inject, auto fac, {name a container that exists already}

erlimar commented 10 years ago

I understand.

Actually my idea is: "do not need a complex container (like auto fac, windsor, unity, etc) if I just want something simple, but I do not give decoupling".

I faced the following problem a few days ago:

While I created a simple library for console app (which used Autofac, but restricted to the-svrc50) then discovered that Autofac package I was using was not available for -svrc50. I actually did not need to use Autofac, only done for convenience (since I am still studying and not vNext domino new techniques). Result: I had several errors at runtime.

How I solved the problem:

public class MyServiceContainer
{
    public IServiceProvider GetProvider()
    {
        return DefaultServices().BuildServiceProvider();
    }
    private IEnumerable<IServiceDescriptor> DefaultServices()
    {
        var describer = new ServiceDescriber();
        yield return describer.Transient<ISample1, Sample1>();
        yield return describer.Transient<ISample2, Sample2>();
        yield return describer.Transient<ISample3, Sample3>();
    }
}

And in my Main:

public static void Main(string[] args)
{
    var provider_ = new MyServiceContainer().GetProvider();
    var sample1_ = provider_.GetService<ISample1>();
}

I just do not know if this is the correct way, or if the scenario I'm presenting should not be covered by the component. But I imagine it's doable for the component by default provide this basic scenario where I can register my types (including batch) and solve them with few lines. And then, if you need something more complete, then I turn the other implementations.

I do not know if I was clear.

PS: Text automatically translated, excuse me any error.

i4004 commented 10 years ago

@erlimar You can use this

erlimar commented 10 years ago

@i4004 i am analysing. Thanks!

wwwlicious commented 9 years ago

@davidfowl for r4mvc we want to extend/register types in a ICompileModule, you don't really have access to the external container to modify it so you have to create you own afaik

[Edit] removed as per @davidfowl request. (it's a replacement for t4mvc built on roslyn fyi)

davidfowl commented 9 years ago

Sounds like the wrong approach to extend the container. ICompileModule is about compiler extensibility.

davidfowl commented 9 years ago

@wwwlicious I wouldn't add anything to this issue as it has nothing to do with what you're trying to do (and I'm not even sure what that is).