0

ISitecoreService accepts database name as a string parameter in its constructor (web or master)

ISitecoreService service = new SitecoreService("master"); //or
ISitecoreService service = new SitecoreService("web");

Is it possible I dynamically send database name as parameter to IoC and resolve it? for example I send web/master string parameter and get a new instance of ISitecoreService

AfshinZavvar
  • 722
  • 1
  • 10
  • 21
  • 1
    Please elaborate, how is ISiteCoreService used, are both used throughout, how is determined which ISoteCoreService to inject in consumers, etc. – Ric .Net Apr 07 '17 at 13:59

2 Answers2

1

Like this?

container.Register<ISitecoreService>(() => new SitecoreService("master"));
Steven
  • 166,672
  • 24
  • 332
  • 435
  • can we make it dynamic or conditional ? I mean can we somehow pass web or master to it and get new instance of it base on parameter value ? – AfshinZavvar Apr 07 '17 at 14:50
  • Please update your question to make your requirements more clear. I will update my answer accordingly. – Steven Apr 07 '17 at 14:52
0

Expanding on Stevens answer as I have experience with Sitecore and I love Simpleinjector.

If you like you can get at the Sitecore configuration when your application is bootstrapping using the configuration factory, access the website site configuration and use the database property.

var sites = Sitecore.Configuration.Factory.GetSiteInfoList();
var website = sites.Single(s => s.Name == "website");

ISitecoreService service = new SitecoreService(website.Database);
container.Register<ISitecoreService>(() => service);

This way your SitecoreService will be newed up with the same database that is defined in the website configuration.

Gravypower
  • 31
  • 1
  • 7