对 TDD 友好的单例类 [英] TDD-friendly Singleton-like class

查看:51
本文介绍了对 TDD 友好的单例类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有至少 2 个其他类使用的存储库类.这个存储库类需要初始化——成本很高(查询数据库).现在,我在需要的地方创建单独的存储库实例.问题是,每次我创建存储库时都必须对其进行初始化.如何设计这样的存储库对 TDD 友好?我想到的第一件事是单身,但它不是解决方案.

I have repository class that is used by at least 2 other classes. This repository class needs to be initialized - which is high in cost (querying database). Now, I create separate instances of repository wherever I need it. The thing is, that everytime I create repository it has to be initialized. How to design such repository to be TDD-friendly? The first thing in my mind was Singleton but it's not the solution.

推荐答案

您使用任何类型的 IOC 容器吗?Unity 是我选择的容器,它包含一个 ContainerControledLifetimeManager 这使您的类成为单例,但不由您自己管理.

Do you use any type of IOC container? Unity is my container of choice, and it contains a ContainerControledLifetimeManager which makes your class a singleton, but not managed by yourself.

这篇关于对 TDD 友好的单例类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆