UWP, Caliburn.Micro : How to pass parameters of Constructor for Method Singleton? -
here caliburn.micro code.
in first step "configure() / app.xaml.cs" , want pass parameters constructor.
_container.singleton<mysingletonclass>("keyname");
mysigletonclass has constructor parameter. ( 1 string )
public mysigletonclass (string msg){ ...
how register singletone parameter ?
updated question
with caliburn.micro manual, there 4 type of register method. there type can pass argument ? should use "registerinstance" ?
in manual, can not understand "implementation". ?
void registerinstance(type service, string key, object implementation) void registerperrequest(type service, string key, type implementation) void registersingleton(type service, string key, type implementation) void registerhandler(type service, string key func<simplecontainer, object> handler)
the caliburn.micro simplecontainer
implementation not support constructor injection additional parameters can supplied during resolution. there other dependency injection containers support scenario, can work around limitation using factory pattern.
the following example demonstrates how customerviewmodel
needs customer name constructor parameter, has dependency on customerdatabase
instance:
/// <summary> /// dependency want use in view models. /// </summary> interface icustomerdatabase { void deletebyname(string name); } class customerdatabase : icustomerdatabase { public void deletebyname(string text) { console.writeline($"customer '{text}' has been deleted."); } } /// <summary> /// customer view model has dependency on /// <see cref="icustomerdatabase"/>-implementation, /// needs customer name during construction reason. /// </summary> class customerviewmodel { public customerviewmodel(string customername, icustomerdatabase database) { this.name = customername; this.database = database; } public string name { get; } public icustomerdatabase database { get; } public void delete() { this.database.deletebyname(this.name); } } /// <summary> /// support parameters, use factory pattern: factory /// gets view model's dependencies own dependencies /// using constructor injection. <see cref="create"/>-method /// supplies additional parameters. can use /// multiple factory methods correspond different /// constructor overloads. /// </summary> class customerviewmodelfactory { public customerviewmodelfactory(icustomerdatabase service) { this.service = service; } public icustomerdatabase service { get; } public customerviewmodel create(string text) { return new customerviewmodel(text, this.service); } } class program { private static void main() { var c = new simplecontainer(); // first, register factory , service. c.registersingleton(typeof(customerviewmodelfactory), null, typeof(customerviewmodelfactory)); c.registersingleton(typeof(icustomerdatabase), null, typeof(customerdatabase)); // later on, depend on factory, not view model itself. // use factory constructor parameter // everywhere need create view model instances. var factory = c.getinstance<customerviewmodelfactory>(); // create view model instance using factory. var viewmodel = factory.create("testcustomer"); viewmodel.delete(); console.readkey(); } }
Comments
Post a Comment