private DbContext TryCreateInstance(Type contextType) { Action <string> log = message => Logger.Log(typeof(ModelsCachedContainer), nameof(TryCreateInstance), message); if (contextType == null) { throw new ArgumentNullException(nameof(contextType)); } var method = contextType.FindStaticMethodWihoutParameters("GetDbContext"); if (method != null) { var methodName = $"{method.DeclaringType?.Name}.{method.Name}".TrimStart('.'); log($"Found method {methodName}"); if (contextType.IsAssignableFrom(method.ReturnType)) { log($"Call method {methodName}"); return((DbContext)method.Invoke(null, null)); } log( $"Skip calling method {methodName} because of return type {method.ReturnType} instead of {contextType}."); } else { log($"Method GetDbContext not found in type {contextType}"); } var services = ShamanOptions.CreateShamanOptions(contextType).Services.OfType <IValueProviderService>(); var constructorParameters = services.SelectMany(a => a.CreateObjects()).ToArray(); return(InstanceCreator.CreateInstance(contextType, Logger, constructorParameters) as DbContext); }
public void T04_ShouldHaveEmptyService() { var services = ShamanOptions.CreateShamanOptions(typeof(TestDbContext)).Services; var cnt = services.Count(a => a is EmptyService); Assert.Equal(1, cnt); }
public ModelInfo(Type dbContextType, ShamanOptions options = null) { _dbContextType = dbContextType; options = options ?? ShamanOptions.CreateShamanOptions(dbContextType); UsedShamanOptions = options; _logger = options.Logger ?? EmptyShamanLogger.Instance; Prepare(); }
public ModelInfo(Type dbContextType, IList <IShamanService> services = null) { _dbContextType = dbContextType; UsedServices = services ?? ShamanOptions.CreateShamanOptions(dbContextType).Services; Prepare(); }