国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET系列:單元測試之StructureMap

2019-11-14 14:26:46
字體:
來源:轉載
供稿:網友

asp.net使用StructureMap等依賴注入組件時最重要就是EntityFramework的DbContext對象要保證在每次HttPRequest只有一個DbContext實例,這里將使用第三方提供的HttpSimulator進行測試。

1.定義IDependency接口

創建屏蔽不同依賴注入組件使用差別的接口。

public interface IDependency{    void Build();    void EndRequest();    void AddTransient(Type from, Type to, object instance = null);    void AddScoped(Type from, Type to, object instance = null);    void AddSingleton(Type from, Type to, object instance = null);    object GetInstance(Type type);    IEnumerable GetAllInstances(Type type);}

2.提供StructureMap的適配類StructureMapAdapter

public class StructureMapAdapter : IDependency, IDisposable{    private bool _disposed = false;    private Container _container;    private Registry _registry;    public StructureMapAdapter()    {        this._registry = new Registry();    }    public void Build()    {        _container = new Container(_registry);    }    public void EndRequest()    {        HttpContextLifecycle.DisposeAndClearAll();    }    public void AddTransient(Type from, Type to, object instance = null)    {        if (instance == null)        {            _registry.For(from).Use(to).LifecycleIs<TransientLifecycle>();        }        else        {            _registry.For(from).Use(instance).LifecycleIs<TransientLifecycle>();        }    }    public void AddScoped(Type from, Type to, object instance = null)    {        if (instance == null)        {            _registry.For(from).Use(to).LifecycleIs<HttpContextLifecycle>();        }        else        {            _registry.For(from).Use(instance).LifecycleIs<HttpContextLifecycle>();        }    }    public void AddSingleton(Type from, Type to, object instance = null)    {        if (instance == null)        {            _registry.For(from).Use(to).LifecycleIs<SingletonLifecycle>();        }        else        {            _registry.For(from).Use(instance).LifecycleIs<SingletonLifecycle>();        }    }    public object GetInstance(Type type)    {        return _container.GetInstance(type);    }    public IEnumerable GetAllInstances(Type type)    {        return _container.GetAllInstances(type);    }    public void Dispose()    {        Dispose(true);        GC.SuppressFinalize(this);    }    protected virtual void Dispose(bool disposing)    {        if (!_disposed)        {            if (disposing)            {                this._container.Dispose();            }            _disposed = true;        }    }}

3.使用HttpSimulator進行單元測試

public class StructureMapAdapterTest{    [Fact]    public void TransientTest()    {        IDependency dependency = new StructureMapAdapter();        dependency.AddTransient(typeof(ITest), typeof(Test));        dependency.Build();        var version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        var version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        Assert.NotEqual(version1, version2);    }    [Fact]    public void SingletonTest()    {        IDependency dependency = new StructureMapAdapter();        dependency.AddSingleton(typeof(ITest), typeof(Test));        dependency.Build();        var version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        var version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        Assert.Equal(version1, version2);    }    [Fact]    public void ScopedTest()    {        var version1 = "";        var version2 = "";        using (HttpSimulator simulator = new HttpSimulator())        {            IDependency dependency = new StructureMapAdapter();            dependency.AddScoped(typeof(ITest), typeof(Test));            dependency.Build();            simulator.SimulateRequest(new Uri("http://localhost/"));            version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;            version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;            Assert.Equal(version1, version2);        }        using (HttpSimulator simulator = new HttpSimulator())        {            IDependency dependency = new StructureMapAdapter();            dependency.AddScoped(typeof(ITest), typeof(Test));            dependency.Build();            simulator.SimulateRequest(new Uri("http://localhost/"));            version1 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        }        using (HttpSimulator simulator = new HttpSimulator())        {            IDependency dependency = new StructureMapAdapter();            dependency.AddScoped(typeof(ITest), typeof(Test));            dependency.Build();            simulator.SimulateRequest(new Uri("http://localhost/"));            version2 = ((ITest)dependency.GetInstance(typeof(ITest))).Version;        }        Assert.NotEqual(version1, version2);    }}public interface ITest{    String Version { get; }}public class Test : ITest{    private string _version = Guid.NewGuid().ToString();    public string Version { get { return this._version; } }}

運行結果:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 土默特左旗| 花垣县| 福清市| 通山县| 安吉县| 遂昌县| 精河县| 台南市| 东源县| 冀州市| 南充市| 阳曲县| 句容市| 平凉市| 韶关市| 古丈县| 贺州市| 新邵县| 新干县| 龙江县| 北宁市| 虹口区| 黔东| 巴东县| 新兴县| 石狮市| 隆尧县| 景德镇市| 焦作市| 天等县| 乌拉特前旗| 江山市| 鹤峰县| 南召县| 泰宁县| 清涧县| 秭归县| 锡林郭勒盟| 中卫市| 铁岭县| 水富县|