如何为未注册的服务注入null [英] How to inject null for unregistered services

查看:59
本文介绍了如何为未注册的服务注入null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装饰器和相关的服务,如下所示:

I have a decorator and an associated service that looks like this:

public interface IAdditionalDataService<TResult, TModel>
{
    TResult PopulateAdditionalData(TResult model, 
        params Expression<Func<TModel, object>>[] properties);
}

public class AdditionalDataDecorator<TQuery, TResult, TModel> 
    : IQueryHandler<TQuery, TResult>
    where TQuery : QueryBase<TResult, TModel>, IQuery<TResult>
    where TModel : ModelBase
{
    private readonly IQueryHandler<TQuery, TResult> _decorated;
    private readonly IAdditionalDataService<TResult, TModel> _additionalDataService;

    public AdditionalDataDecorator(IQueryHandler<TQuery, TResult> decorated,
        IAdditionalDataService<TResult, TModel>  additionalDataService)
    {
        _decorated = decorated;
        _additionalDataService = additionalDataService;
    }

    public TResult Handle(TQuery query)
    {
        var result = _decorated.Handle(query);

        if (query.RelatedDataProperties != null && query.RelatedDataProperties.Any())
            result = _additionalDataService.PopulateAdditionalData(result,
                query.RelatedDataProperties.ToArray());

        return result;
    }
}

装饰器包装查询处理程序,以便结果由 AdditionalDataService 处理,然后返回.

The decorator wraps a query handler so that the results are manipulated by the AdditionalDataService and then returned.

问题在于,并非我的所有查询结果都需要一个AdditionalDataService,因此并非每个可能的TResult都具有关联的AdditionalDataService实现.如果我尝试使用装饰器,而没有为相应的Type提供有效的AdditionalDataService实现,则Simple Injector会引发异常.

The issue is not all of my query results will need an AdditionalDataService, so not every possible TResult has an associated AdditionalDataService implementation. Simple Injector throws an exception if I try to use the decorator without a valid AdditionalDataService implementation for the corresponding Type.

我需要Simple Injector进行的操作-仅用于AdditionalDataService-是如果找不到对应类型的注入"null"的实现.从那里,我可以在调用服务之前测试null.

What I need Simple Injector to do - only for AdditionalDataService - is if it doesn't find an implementation for the corresponding type inject "null". From there I can test for null before calling into the service.

我的问题是

如何在遇到未注册的服务请求时告诉Simple Injector注入null而不抛出异常?

How can I tell Simple Injector to inject null when it encounters a request for a service that hasn't been registered, instead of throwing an exception?

推荐答案

您应该从不 null 注入到应用程序组件的构造函数中.这是一种不好的做法,因为它会使您的代码复杂化为空检查.一个组件应该总是总是能够假定它得到了一个值.因此,Simple Injector的构建使其(几乎)不可能将 null 值注入到构造函数中.

You should never inject null into your application components' constructors. This is bad practice, because it complicates your code with null checks. A component should simply always be able to assume that it gets a value. Because of this, Simple Injector is built to make it (almost) impossible to inject null values into constructors.

相反,您应该使用众所周知的空对象模式,即@manji是指.但是,除了使用 ResolveUnregisteredType 事件之外,还有一种更简单的方法来实现此目的.

Instead you should use the well-known Null Object pattern, which is what @manji is referring to. But instead of using the ResolveUnregisteredType event, there is a much easier way to do this.

我假定您使用 Register 注册您的 IAdditionalDataService< TResult,TModel> 实现,如下所示:

I assume that you register your IAdditionalDataService<TResult, TModel> implementations using Register much like this:

// Simple Injector v3.x
container.Register(typeof(IAdditionalDataService<,>),
    new[] { typeof(IAdditionalDataService<,>).Assembly });

// Simple Injector v2.x
container.RegisterManyForOpenGeneric(typeof(IAdditionalDataService<,>),
    typeof(IAdditionalDataService<,>).Assembly);

现在可以按以下方式注册后备注册:

A fallback registration can now be registered as follows:

// Simple Injector v3.x
container.RegisterConditional(
    typeof(IAdditionalDataService<,>),
    typeof(EmptyAdditionalDataService<,>),
    c => !c.Handled);

// Simple Injector v2.x
container.RegisterOpenGeneric(
    typeof(IAdditionalDataService<,>),
    typeof(EmptyAdditionalDataService<,>));

就是这样.

这篇关于如何为未注册的服务注入null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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