Mati Silver
Mati Silver

Reputation: 195

Use external parameters into Service class - ServiceStack

i need to send external parameter into a service class, i could do it, but i don´t know if its the best way, here is what i did:

public class Context : ApplicationContext
{
  public Context()
  {
     var appHost = new AppHost(i, (ILayout)f);
     appHost.Init();
     appHost.Start(listeningOn);            
  }
}

public class AppHost : AppSelfHostBase
{
    private readonly Init _init;
    private readonly ILayout _activeForm;
    public AppHost(Init i, ILayout f)
        : base("ClientService", typeof(ClientService).Assembly)
    {
        _init = i;
        _activeForm = f;
    }

    public override void Configure(Container container)
    {
        container.Register("init",_init);
        container.Register("layout", _activeForm);
    }
}

public class ClientService : Service
{
   private Init _initConf;
   public HttpResult Post(Person request)
   {
      _initConf = ServiceStackHost.Instance.Container.ResolveNamed<Init>("init");
   }
}

It works great, but i´m not sure that this is the best choice.. What do youy think? Thanks

Upvotes: 0

Views: 47

Answers (1)

mythz
mythz

Reputation: 143399

It's ugly to have a reference to the IOC in your Services when it's not needed. Why aren't you just registering the types?

public override void Configure(Container container)
{
    container.Register(_init);
    container.Register(_activeForm);
}

And in your Service you can just declare them as public properties and they'll be injected when your Service is created, e.g:

public class ClientService : Service
{
    public Init Init { get; set; }
    public ILayout ActiveForm { get; set; }

    public HttpResult Post(Person request)
    {
        //Use this.Init or this.ActiveForm
    }
}

Upvotes: 2

Related Questions