Created
February 1, 2014 09:26
-
-
Save scottmcarthur/8750037 to your computer and use it in GitHub Desktop.
ServiceStack IoC injection example (v4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using ServiceStack; | |
namespace Tests.Recipe | |
{ | |
class MainClass | |
{ | |
public static void Main() | |
{ | |
// Very basic console host | |
var appHost = new AppHost(); | |
appHost.Init(); | |
// Create instance of SomeClass | |
var someClass = new SomeClass(); | |
// Immediately use the IoC injected property | |
someClass.AppApplicationContext.Name = "Hello World"; | |
// Output | |
Console.WriteLine(someClass.AppApplicationContext.Name); | |
Console.ReadKey(); | |
} | |
} | |
public class AppHost : AppHostBase | |
{ | |
public AppHost() : base("Test Service", typeof(TestService).Assembly) {} | |
public override void Configure(Funq.Container container) | |
{ | |
container.RegisterAutoWiredAs<AppApplicationContext, IAppApplicationContext>(); | |
} | |
} | |
public class TestService : Service | |
{ | |
} | |
public class SomeClass | |
{ | |
public IAppApplicationContext AppApplicationContext { get; set; } | |
public SomeClass() | |
{ | |
HostContext.Container.AutoWire(this); | |
} | |
} | |
public interface IAppApplicationContext | |
{ | |
string Name { get; set; } | |
} | |
public class AppApplicationContext : IAppApplicationContext | |
{ | |
public string Name { get; set; } | |
public int Age { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment