在多个接口中实现Ninject Convention

时间:2019-04-01 16:41:46

标签: c# asp.net ninject single-responsibility-principle ninject-extensions

我有几个界面

public interface IViewModelBuilder<in TView> 
{
    void Build(TView model);
}

public interface IViewModelBuilder<in TInput, out TView>
{
    TView Build(TInput input);
}

public interface IViewModelSaver<in TService, in TView>
{
    //bool Save(TView model);
    void Save<T>(TView model) where T: TService;
}

和这些接口的实现

public class SummaryViewModelBuilder : 
    IViewModelBuilder<SummaryViewModel>
    , IViewModelSaver<ICustomerQueryService, SummaryViewModel>
    , IViewModelSaver<ISmallBussinessService, SummaryViewModel>
{
    public void Build(SummaryViewModel model)
    {
        model.Status = "Done " + model.ResellerId;
    }

    void IViewModelSaver<ICustomerQueryService, SummaryViewModel>.Save<T>(SummaryViewModel model)
    {
        Console.WriteLine("ICustomerQueryService");
    }

    void IViewModelSaver<ISmallBussinessService, SummaryViewModel>.Save<T>(SummaryViewModel model)
    {
        Console.WriteLine("ISmallBussinessService");
    }
}

它可以与

之类的Ninject约定一起正常工作
kernel.Bind(x =>
                x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IViewModelBuilder<>))
                    .BindSingleInterface());

kernel.Bind(x =>
            x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IViewModelBuilder<,>))
                .BindSingleInterface());

kernel.Bind(x =>
            x.FromThisAssembly().SelectAllClasses().InheritedFrom(typeof(IViewModelSaver<,>))
                .BindSingleInterface());

但仅当类实现单个接口时,才意味着其中一个绑定接口,在示例情况下,我会收到一个异常“序列包含多个元素”,如何防止该异常,我也了解SRP,但是相关的方法,我不想在单独的类中实现。

0 个答案:

没有答案