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
{
}
source
share