注册新的TService时如何在SimpleInjector中使用通用接口?

时间:2018-09-16 08:44:44

标签: c# simple-injector

我有一个简单的界面:

from django.db import models


class DescriptionMixin(models.Model):

    class Meta:
        abstract = True

    description = models.TetxField(
        # how to get value here?
        blank=inheriting_class.description_required
    )


class OptionalDescription(DescriptionMixin, SomeOtherClass):

    class Meta:
        verbose_name = 'Optional description'

    description_required = False


class RequiredDescription(DescriptionMixin, SomeOtherClass):

    class Meta:
        verbose_name = 'Required description'

    description_required = True

此接口的一些实现:

public interface IRepo<T>
{
    T GetSingle(int id);

    IQueryable<T> GetCollection();
}

现在我正尝试在简单注入器中将此public class ParametersFields_Repo : GenericRepository<Parameters_Fields, MachineDbConnection>, IRepo<Parameters_Fields> { ... } public class FieldTypesDict_Repo : GenericRepository<Parameters_FieldTypes_Dict, MachineDbConnection>, IRepo<Parameters_FieldTypes_Dict> { ... } 注册为IRepo<T>,如下所示:

TService

但符合以下要求:

private void ConfigureSimpleInjector()
    {
        var container = new Container();
        RegisterDIServices(container);

        DependencyResolver.SetResolver(
            new SimpleInjectorDependencyResolver(container));
    }

    private void RegisterDIServices(Container container)
    {
        container.Register<IFieldModelRepository, FieldModelRepository>(
            Lifestyle.Scoped);
        container.Register<IField_Repo, Field_Repo>(Lifestyle.Scoped);
        container.Register<IRepo<Parameters_Fields>, ParametersFields_Repo>(
            Lifestyle.Scoped);
    }

我收到错误消息:

  

在通用类型或方法“ Container.Register(Lifestyle)”中,类型“ DbAccess.Repositories.ParametersFields_Repo”不能用作类型参数“ TImplementation”。没有从'DbAccess.Repositories.ParametersFields_Repo'到'DbAccess.Interface.IRepo'的隐式引用转换

您能告诉我什么是这样做的好方法吗?这是我第一次尝试在项目中使用DI时。

0 个答案:

没有答案