0

Is there a better way to consume a service in mvvmcross by constructor injection? For example, I have my object in a plugin implementation

public class MyObject : IMyObject
{
   public MyObject (int id) 
   {
      _id = id;
   }
}

and want to consume it somewhere the portable core (in the buisness layer) like this :

public void ConsumeTheService()
{
    int i = 50;
    var myObject = this.GetService<IMyObject>(i);
}

Actually, I only use a setter to get my id back in the implementation.

4

2 回答 2

0

我不确定我是否理解您的示例-不确定intValueGetService

不管:

于 2013-03-24T15:42:45.470 回答
0

在您更新之后......如果您询问如何获得:

    var myObject = this.GetService<IMyObject>(50);

并为此返回一个初始化为的对象new MyObject(50)

那么我建议您实现一个接口

public interface IMyObjectFactory
{
    IMyObject CreateMyObject(int i);
}

有一个实现:

public class MyObjectFactory : IMyObjectFactory
{
    public IMyObject CreateMyObject(int i)
    {
        return new MyObject(i);
    }
}

然后你可以IMyObjectFactory用 IoC注册它

于 2013-03-24T17:19:34.727 回答