public void ShouldUseAServiceProviderInAnExceptionHandler() { var logger = new Logger(); var provider = new ResolveServiceProvider(logger); using (var mapper = Mapper.CreateNew()) { mapper.WhenMapping .UseServiceProvider(provider) .PassExceptionsTo(exData => exData .GetService <Logger>() .Log(exData.Exception.ToString())); mapper.After .CreatingInstances .Call(ctx => throw new InvalidOperationException("NO")); new PublicField <int> { Value = 123 } .MapUsing(mapper) .ToANew <PublicField <long> >(); } logger.EntryCount.ShouldBe(1); }
public void ShouldExposeAConfiguredServiceProvider() { var logger = new Logger(); var serviceProvider = new ResolveServiceProvider(logger, "Charlie"); using (var mapper = Mapper.CreateNew()) { mapper.WhenMapping.UseServiceProvider(serviceProvider); mapper.Before .MappingBegins .Call(ctx => { var log = (Logger)ctx .GetServiceProvider <ResolveServiceProvider>() .GetInstance(typeof(Logger)); log.Log("Logged!"); }); var source = new PublicField <string> { Value = "Logging!" }; mapper.Map(source).Over(new PublicField <string>()); } logger.EntryCount.ShouldBe(1); }
public void ShouldUseAConfiguredServiceProviderObjectResolveService() { var logger = new Logger(); var serviceProvider = new ResolveServiceProvider(logger, "Charlie"); using (var mapper = Mapper.CreateNew()) { mapper.WhenMapping.UseServiceProvider(serviceProvider); mapper.Before .MappingBegins .Call(ctx => ctx.GetService <Logger>("Charlie").Log("Mapping!")) .And .After .MappingEnds .Call(ctx => ctx.GetService <Logger>().Log("More mapping!")); var source = new PublicField <string> { Value = "Logging!" }; mapper.Map(source).Over(new PublicField <string>()); } logger.EntryCount.ShouldBe(2); }