C# TinyIoC向类构造函数注入模型
我在xamarin项目中使用TinyIoc,如果有必要,我可以更改IoC容器。我怎样才能解决这个问题C# TinyIoC向类构造函数注入模型,c#,.net,xamarin,inversion-of-control,ioc-container,C#,.net,Xamarin,Inversion Of Control,Ioc Container,我在xamarin项目中使用TinyIoc,如果有必要,我可以更改IoC容器。我怎样才能解决这个问题 internal class Program { private static void Main(string[] args) { TinyIoC.TinyIoCContainer.Current.Register<IService, Service>(); TinyIoC.TinyIoCContainer.Current.Regis
internal class Program
{
private static void Main(string[] args)
{
TinyIoC.TinyIoCContainer.Current.Register<IService, Service>();
TinyIoC.TinyIoCContainer.Current.Register<ViewModel>();
Model model; //From database... How I can inject this to my viewmodel?
var viewModel = TinyIoC.TinyIoCContainer.Current.Resolve<ViewModel>();
}
}
public class Model
{
public object Data { get; set; }
}
internal interface IService
{
string SomeMethod(Model model);
}
public class Service : IService
{
public string SomeMethod(Model model)
{
//...
return string.Empty;
}
}
internal class ViewModel
{
private readonly Model model;
private readonly IService service;
public string Name { get; private set; }
public ViewModel(IService service, Model model)
{
this.model = model;
this.service = service;
this.Name = this.service.SomeMethod(this.model);
}
}
内部类程序
{
私有静态void Main(字符串[]args)
{
TinyIoC.TinyIoCContainer.Current.Register();
TinyIoC.TinyIoCContainer.Current.Register();
Model Model;//来自数据库……如何将其注入我的viewmodel?
var viewModel=TinyIoC.TinyIoCContainer.Current.Resolve();
}
}
公共类模型
{
公共对象数据{get;set;}
}
内部接口设备
{
字符串方法(模型);
}
公共课服务:IService
{
公共字符串方法(模型)
{
//...
返回字符串。空;
}
}
内部类视图模型
{
私有只读模型;
私人只读设备服务;
公共字符串名称{get;private set;}
公共视图模型(iSeries服务、模型)
{
this.model=模型;
服务=服务;
this.Name=this.service.SomeMethod(this.model);
}
}
我唯一想到的是:
internal class Program
{
private static void Main(string[] args)
{
TinyIoC.TinyIoCContainer.Current.Register<IService, Service>();
TinyIoC.TinyIoCContainer.Current.Register<ViewModel>();
Model model; //From database... How I can inject this to my viewmodel?
var viewModel = TinyIoC.TinyIoCContainer.Current.Resolve<ViewModel>();
viewModel.Initialize(model);
}
}
internal class ViewModel
{
private Model model;
private readonly IService service;
public string Name { get; private set; }
public ViewModel(IService service)
{
this.service = service;
}
public void Initialize(Model model)
{
this.model = model;
this.Name = this.service.SomeMethod(this.model);
}
}
内部类程序
{
私有静态void Main(字符串[]args)
{
TinyIoC.TinyIoCContainer.Current.Register();
TinyIoC.TinyIoCContainer.Current.Register();
Model Model;//来自数据库……如何将其注入我的viewmodel?
var viewModel=TinyIoC.TinyIoCContainer.Current.Resolve();
初始化(模型);
}
}
内部类视图模型
{
私有模型;
私人只读设备服务;
公共字符串名称{get;private set;}
公共视图模型(iSeries服务)
{
服务=服务;
}
公共void初始化(模型)
{
this.model=模型;
this.Name=this.service.SomeMethod(this.model);
}
}
但我真的不喜欢这样:-(我有一个糟糕的设计?应该使用dependecy注入而不是construktor注入?或者另一个container可以做到这一点?查看带有Func的重载方法,看看哪一个最适合,因为我不知道TinyIoC如何处理注册实际实现。类似的方法可能会起作用:
.Register<ViewModel>(() => new ViewModel(
TinyIoC.TinyIoCContainer.Current.Resolve<IService>(),
model));
.Register(()=>新视图模型(
TinyIoC.TinyIoCContainer.Current.Resolve(),
模型);
或者只是传递服务的新实例:
.Register<ViewModel>(() => new ViewModel(new Service(), model));
.Register(()=>newviewmodel(newservice(),model));
如果TinyIoC不符合要求,那么您可以查看XLabs.IoC,寻找与Xamarin兼容的替代方案: