【发布时间】:2011-08-31 20:46:42
【问题描述】:
David Haydn 的这篇有用的文章(编辑:已删除诈骗链接,可能是 this article)展示了如何使用 InjectionConstructor 类来帮助您使用 Unity 的装饰器模式设置链。但是,如果装饰器链中的项目在其构造函数中有其他参数,InjectionConstructor 必须显式声明它们中的每一个(否则 Unity 会抱怨它找不到正确的构造函数)。这意味着您不能简单地将新的构造函数参数添加到装饰器链中的项目而不更新您的 Unity 配置代码。
这里有一些示例代码来解释我的意思。 ProductRepository 类首先由CachingProductRepository 包装,然后由LoggingProductRepostiory 包装。 CachingProductRepository 和 LoggingProductRepository 除了在其构造函数中获取一个 IProductRepository 外,还需要来自容器的其他接口。
public class Product
{
public int Id;
public string Name;
}
public interface IDatabaseConnection { }
public interface ICacheProvider
{
object GetFromCache(string key);
void AddToCache(string key, object value);
}
public interface ILogger
{
void Log(string message, params object[] args);
}
public interface IProductRepository
{
Product GetById(int id);
}
class ProductRepository : IProductRepository
{
public ProductRepository(IDatabaseConnection db)
{
}
public Product GetById(int id)
{
return new Product() { Id = id, Name = "Foo " + id.ToString() };
}
}
class CachingProductRepository : IProductRepository
{
IProductRepository repository;
ICacheProvider cacheProvider;
public CachingProductRepository(IProductRepository repository, ICacheProvider cp)
{
this.repository = repository;
this.cacheProvider = cp;
}
public Product GetById(int id)
{
string key = "Product " + id.ToString();
Product p = (Product)cacheProvider.GetFromCache(key);
if (p == null)
{
p = repository.GetById(id);
cacheProvider.AddToCache(key, p);
}
return p;
}
}
class LoggingProductRepository : IProductRepository
{
private IProductRepository repository;
private ILogger logger;
public LoggingProductRepository(IProductRepository repository, ILogger logger)
{
this.repository = repository;
this.logger = logger;
}
public Product GetById(int id)
{
logger.Log("Requesting product {0}", id);
return repository.GetById(id);
}
}
这是一个(通过)单元测试。请参阅 cmets 了解我想消除的多余配置:
[Test]
public void ResolveWithDecorators()
{
UnityContainer c = new UnityContainer();
c.RegisterInstance<IDatabaseConnection>(new Mock<IDatabaseConnection>().Object);
c.RegisterInstance<ILogger>(new Mock<ILogger>().Object);
c.RegisterInstance<ICacheProvider>(new Mock<ICacheProvider>().Object);
c.RegisterType<IProductRepository, ProductRepository>("ProductRepository");
// don't want to have to update this line every time the CachingProductRepository constructor gets another parameter
var dependOnProductRepository = new InjectionConstructor(new ResolvedParameter<IProductRepository>("ProductRepository"), new ResolvedParameter<ICacheProvider>());
c.RegisterType<IProductRepository, CachingProductRepository>("CachingProductRepository", dependOnProductRepository);
// don't want to have to update this line every time the LoggingProductRepository constructor changes
var dependOnCachingProductRepository = new InjectionConstructor(new ResolvedParameter<IProductRepository>("CachingProductRepository"), new ResolvedParameter<ILogger>());
c.RegisterType<IProductRepository, LoggingProductRepository>(dependOnCachingProductRepository);
Assert.IsInstanceOf<LoggingProductRepository>(c.Resolve<IProductRepository>());
}
【问题讨论】:
-
好吧,我以前从未使用过 unity,也许这已经过时了,但你不能用 injectionfactory 代替 injectionconstructor 吗?
-
@DarkSquirrel42 一个有趣的建议。不过,Action 可能必须调用具体的构造函数,这意味着只要构造函数发生更改,仍需要调用 Unity 配置代码。不过,它至少会导致编译错误,提醒您需要更改。
-
@DarkSquirrel42,再想一想,如果你只使用一个 InjectionFactory 其 Func 构建整个链,使用容器来实现其他依赖项,虽然你仍然没有与构造函数参数更改隔离,但在至少比我的原始代码更容易混淆,并且只需要在容器中注册一次。
-
文章链接已损坏
-
那个“有用的文章”现在是一个骗局网站,可能需要修复它。
标签: c# unity-container decorator