Как зарегистрировать CustomContext (соединение DbConnection, DbCompiledModel compiledModel) с SimpleInjector? - PullRequest
0 голосов
/ 04 ноября 2019

Мой CustomContext Выглядит как показано ниже.

public class CustomContext : DbContext
{
    public CustomContext(DbConnection connection, DbCompiledModel compiledModel) : base(connection, compiledModel, true)
    {
        Database.SetInitializer<CustomContext>(null);
    }
    //Other methods go here
}

Моя регистрация служб выглядит следующим образом

_container = new Container();
_container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
RegisterTypes(_container);

Метод RegisterTypes

private static void RegisterTypes(Container container)
{
    container.RegisterSingleton<IMapper>(() =>
    {
        var mapper = new Mapper(new MapperConfiguration(cfg =>
        {
            cfg.AddExpressionMapping();
        }));
        #if DEBUG
        mapper.DefaultContext.ConfigurationProvider.AssertConfigurationIsValid();
        #endif
        return mapper;
    });

    container.Register(() => new AutoMapperConfiguration(),
        Lifestyle.Scoped);
    container.Register<IUoWFactory, DbUoWFactory>(Lifestyle.Scoped);
    container.Register<IRepository, DbRepository>(Lifestyle.Scoped);
    container.Register<ICartonDetailService, CartonDetailService>(Lifestyle.Scoped);
    var connectionString = ConfigurationManager.AppSettings["DbConnection"];
    container.Register<DbConnection>(() => new OracleConnection(connectionString),
            Lifestyle.Singleton);
    container.Register<DbCompiledModel>();
    container.Register(() => new CustomContext(container.GetInstance<DbConnection>(), container.GetInstance<DbCompiledModel>()),
        Lifestyle.Scoped);

    container.Options.AllowOverridingRegistrations = true;
}

Проблема в том, что DbCompiledModel делаетне имеет конструктора и, следовательно, я не могу зарегистрировать DbCompiledModel и CustomContext, как описано выше. Пожалуйста, помогите мне решить эту проблему.

Обновление: изменен конструктор CustomContext, как показано ниже

ShamrockContext(DbConnection connection, Type[] types, ISfcInMemoryCache cache) : base(connection, cache.GetValue<DbCompiledModel>(types), false)

cache.GetValue возвращает кэшированную DbCompiledModel, читая список моделей из типов. Проблема сейчас в том, что он выдает ошибку типа []. Есть ли способ передать параметры времени выполнения этим конструкторам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...