Using ServiceStack Funq IoC: how dependencies are injected?

I have a WinForm application and I want to use the ServiceStack dependency injection mechanism:

public class AppHost : AppHostBase
{
    public AppHost()
        : base("MyName", typeof(AppHost).Assembly)
    {
    }

    public override void Configure(Container container)
    {
        container.RegisterAutoWiredAs<AppApplicationContext, IAppApplicationContext>();
    }
}

Then, in one form or another, the class uses it:

public class SomeClass : AppBaseForm
{
    public IAppApplicationContext AppApplicationContext { get; set; }

    public SomeClass(IAppApplicationContext appApplicationContext)
    {
        AppApplicationContext = appApplicationContext;
    }

    public SomeClass()
    {
    }
}

But AppApplicationContextalways null. When in a constructor without parameters, I write:

AppApplicationContext = AppHostBase.Resolve<IAppApplicationContext>();

then everything is all right. But is this the right way to do this? I mean, AppApplicationContext should not be allowed by IoC automatically? And WinForm must have a constructor without parameters.

The rest of the code:

private static void Main()
{
    var appHost = new AppHost();
    appHost.Init();
}

public interface IAppApplicationContext
{
}

public class AppApplicationContext : IAppApplicationContext
{
}
+4
source share
1 answer

AutoWire, . WinForm :

public class SomeClass : AppBaseForm
{
    public IAppApplicationContext AppApplicationContext { get; set; }

    public SomeClass()
    {
        // Tell the container to inject dependancies
        HostContext.Container.AutoWire(this);
    }
}

ServiceStack, AutoWire , ServiceStack .

. . - , WinForms, , IoC ServiceStack, .

+8

Source: https://habr.com/ru/post/1524795/


All Articles