TinyIoC返回相同的实例

mit*_*lsg 0 dependency-injection tinyioc

我是依赖注入模式的新手,我在从容器中获取类的新实例时遇到问题.在tinyioc中解析它只是继续返回相同的实例而不是新实例.现在为代码

public abstract class HObjectBase : Object
{
    private string _name = String.Empty;
    public string Name 
    {
        get
        {
            return this._name;
        }
        set
        {
            if (this._name == string.Empty && value.Length > 0 && value != String.Empty)
                this._name = value;
            else if (value.Length < 1 && value == String.Empty)
                throw new FieldAccessException("Objects names cannot be blank");
            else
                throw new FieldAccessException("Once the internal name of an object has been set it cannot be changed");
        }
    }

    private Guid _id = new Guid();
    public Guid Id 
    {
        get
        {
            return this._id;
        }
        set
        {
            if (this._id == new Guid())
                this._id = value;
            else
                throw new FieldAccessException("Once the internal id of an object has been set it cannot be changed");

        }
    }

    private HObjectBase _parent = null;
    public HObjectBase Parent 
    {
        get
        {
            return this._parent;
        }
        set
        {
            if (this._parent == null)
                this._parent = value;
            else
                throw new FieldAccessException("Once the parent of an object has been set it cannot be changed");
        }
    }
}

public abstract class HZoneBase : HObjectBase
{
    public new HObjectBase Parent
    {
        get
        {
            return base.Parent;
        }
        set
        {
            if (value == null || value.GetType() == typeof(HZoneBase))
            {
                base.Parent = value;
            }
            else
            {
                throw new FieldAccessException("Zones may only have other zones as parents");
            }
        }
    }


    private IHMetaDataStore _store;
    public HZoneBase(IHMetaDataStore store)
    {
        this._store = store;
    }



    public void Save()
    {
        this._store.SaveZone(this);
    }
}
Run Code Online (Sandbox Code Playgroud)

派生类目前是假人,但现在是

public class HZone : HZoneBase
{
    public HZone(IHMetaDataStore store)
        : base(store)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

现在因为这是一个外部库,我有一个面向访问所有内容的类

public class Hadrian 
{
    private TinyIoCContainer _container;

    public Hadrian(IHMetaDataStore store)
    {
        this._container = new TinyIoCContainer();
        this._container.Register(store);
        this._container.AutoRegister();
    }

    public HZoneBase NewZone()
    {
        return _container.Resolve<HZoneBase>();
    }

    public HZoneBase GetZone(Guid id)
    {
        var metadataStore = this._container.Resolve<IHMetaDataStore>();
        return metadataStore.GetZone(id);
    }

    public List<HZoneBase> ListRootZones()
    {
        var metadataStore = this._container.Resolve<IHMetaDataStore>();
        return metadataStore.ListRootZones();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是测试失败,因为Hadrian类上的GetNewZone()方法不断返回相同的实例.

测试代码

[Fact]
public void ListZones()
{
    Hadrian instance = new Hadrian(new MemoryMetaDataStore());
    Guid[] guids = { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
    int cnt = 0;
    foreach (Guid guid in guids)
    {
        HZone zone = (HZone)instance.NewZone();
        zone.Id = guids[cnt];
        zone.Name = "Testing" + cnt.ToString();
        zone.Parent = null;
        zone.Save();
        cnt++;
    }

    cnt = 0;
    foreach (HZone zone in instance.ListRootZones())
    {
        Assert.Equal(zone.Id, guids[cnt]);
        Assert.Equal(zone.Name, "Testing" + cnt.ToString());
        Assert.Equal(zone.Parent, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道它可能是一些简单的我缺少模式,但我不确定,任何帮助将不胜感激.

Tea*_*Dev 6

首先,请始终将代码简化为证明问题的绝对必要条件,但要提供足够的代码才能实际运行; 我不得不猜测自己MemoryMetaDataStore做了什么,并自己实现它来运行代码.

另外,请说出失败的地点方式,直接指出其他问题.我花了几分钟搞清楚我得到的例外你的问题,你甚至没有得到断言.

也就是说,container.Resolve<HZoneBase>()将始终返回相同的实例,因为这是TinyIoC中的自动注册工作方式 - 一旦解析了抽象,就会为后续调用返回相同的实例.

要更改此设置,请将以下行添加到Hadrian构造函数中:

this._container.Register<HZoneBase, HZone>().AsMultiInstance();
Run Code Online (Sandbox Code Playgroud)

这将告诉容器为每个解析请求创建一个新实例HZoneBase.

此外,Bassetassen关于Assert部分的答案是正确的.

一般来说,如果你想学习DI,你应该阅读Mark Seemann的优秀书籍".NET中的依赖注入" - 不是很容易阅读,因为整个主题本身就很复杂,但它不仅仅值得它,还会让你进入它比自己学习快几年.