注入接口intoViewmodel为null

时间:2018-04-10 06:45:05

标签: dryioc

我对DryIoc的了解有限,我希望有人可以帮助我。 在我的移动应用程序中,我正在阅读包含所有设置的json文件

我想只将相关设置注入相关的viewmodel。

基本上如何将填充的对象/接口注入viewmodel的构造函数

下面是一个点头测试和样本,目前失败了。

我可以设置viewmodel的属性,但我希望接口不为null 当调用viewmodel构造函数时。

希望一切都清楚。很多人感谢!!

using NUnit.Framework;
using DryIoc;

namespace DryIocTests
{
    [TestFixture]
    public class TestClass
    {
        private IAppSettings appSettings;
        [OneTimeSetUp]
        public void Setup()
        {
            //pretend we have read a json file in our mobile app
             appSettings = new AppSettings
            {
                CloudSettings = new CloudSettings {TestPropertyTwo = "Two"}
            };
        }

        [Test]
        public void ThisWorksButIsNotWhatIWant()
        {
            using (var container = new Container())
            {
                container.Register<ICloudSettings, CloudSettings>();
                container.Register<ICloudSampleViewModel, CloudSampleViewModel>();

                var vm = container.Resolve<ICloudSampleViewModel>();
                //I know that setting the property like that will work
                //but I do not want to do that .I wanted to find a way that by the time the viewModel is resolved the interface got values
                vm.CloudSettings = appSettings.CloudSettings;

                Assert.IsNotNull(vm.CloudSettings);
                Assert.IsNotNull(vm.CloudSettings.TestPropertyTwo);
            }
        }
        [Test]
        public void HowDoIMakeThisToWork()
        {
            using (var container = new Container())
            {
                container.Register<ICloudSettings, CloudSettings>();
                container.Register<ICloudSampleViewModel, CloudSampleViewModel>();

                var cloudSettings = container.Resolve<ICloudSettings>();
                cloudSettings.TestPropertyTwo = appSettings.CloudSettings.TestPropertyTwo;

                container.UseInstance(typeof(ICloudSettings));

                //by now I want that myviewmodel has the injected interface ICloudSetting populated.
                var vm = container.Resolve<ICloudSampleViewModel>();

                Assert.IsNotNull(vm.CloudSettings.TestPropertyTwo);
            }
        }
    }

    public class CloudSampleViewModel :ICloudSampleViewModel
    {
        public CloudSampleViewModel(ICloudSettings cloudSettings)
        {
            CloudSettings = cloudSettings;
        }

        public ICloudSettings CloudSettings { get; set; }
    }
    public class AppSettings : IAppSettings
    {
        public CloudSettings CloudSettings { get; set; }
    }
    public class CloudSettings: ICloudSettings{public string TestPropertyTwo { get; set; }}
    public interface IAppSettings
    {
        CloudSettings CloudSettings { get; set; }
    }

    public interface ICloudSettings{string TestPropertyTwo { get; set; }}
    public interface ICloudSampleViewModel{ICloudSettings CloudSettings { get; set; }}
}

1 个答案:

答案 0 :(得分:0)

这是live working example

完整代码如下。搜索FIX!以查看实际修复程序。

using System;
using DryIoc;

public class Program
{
    public static void Main()
    {
        var p = new Program();

        p.Setup();

        p.ThisWorksButIsNotWhatIWant();

        p.HowDoIMakeThisToWork();
    }

    private IAppSettings appSettings;

    public void Setup()
    {
        //pretend we have read a json file in our mobile app
        appSettings = new AppSettings
        {
            CloudSettings = new CloudSettings {TestPropertyTwo = "Two"}
        };
    }

    public void ThisWorksButIsNotWhatIWant()
    {
        using (var container = new Container())
        {
            container.Register<ICloudSettings, CloudSettings>(
                made: PropertiesAndFields.Of.Name("TestPropertyTwo", _ => appSettings.CloudSettings.TestPropertyTwo)); // FIX!

            container.Register<ICloudSampleViewModel, CloudSampleViewModel>();

            var vm = container.Resolve<ICloudSampleViewModel>();

            Console.WriteLine("vm.CloudSettings: " + vm.CloudSettings);
            Console.WriteLine("vm.CloudSettings.TestPropertyTwo: " + vm.CloudSettings.TestPropertyTwo);
        }
    }

    public void HowDoIMakeThisToWork()
    {
        using (var container = new Container())
        {
            container.Register<ICloudSettings, CloudSettings>();
            container.Register<ICloudSampleViewModel, CloudSampleViewModel>();

            var cloudSettings = container.Resolve<ICloudSettings>();
            cloudSettings.TestPropertyTwo = appSettings.CloudSettings.TestPropertyTwo;

            container.UseInstance(cloudSettings); // FIX!

            //by now I want that myviewmodel has the injected interface ICloudSetting populated.
            var vm = container.Resolve<ICloudSampleViewModel>();

            Console.WriteLine("vm.CloudSettings.TestPropertyTwo: " + vm.CloudSettings.TestPropertyTwo);
        }
    }
}

public class CloudSampleViewModel :ICloudSampleViewModel
{
    public CloudSampleViewModel(ICloudSettings cloudSettings)
    {
        CloudSettings = cloudSettings;
    }

    public ICloudSettings CloudSettings { get; set; }
}

public class AppSettings : IAppSettings
{
    public CloudSettings CloudSettings { get; set; }
}

public class CloudSettings: ICloudSettings{ public string TestPropertyTwo { get; set; }}
public interface IAppSettings
{
    CloudSettings CloudSettings { get; set; }
}

public interface ICloudSettings{string TestPropertyTwo { get; set; }}
public interface ICloudSampleViewModel{ICloudSettings CloudSettings { get; set; }}