simple-injector

Injecting dependencies into Entity Framework entities and projects

我只是一个虾纸丫 提交于 2019-12-11 01:45:47
问题 Is there a way to inject dependencies into objects returned from an EF Linq context.Entities.Select(x => new Y {...}) projection? (I am using Simple Injector, but concept remains) Sort of thing I am trying to achieve: (this is just typed in, not compiled, sorry for any syntax errors/incompleteness) // person MAY be an entity, but probably more likely a class to serve a purpose public class Person { public string Name public DateTime DOB {get;set; } // what I want to achieve: note, I don't

Simple Injector - Custom WebViewPage

天大地大妈咪最大 提交于 2019-12-11 00:40:06
问题 I've a small application (for now) that uses translations. My translation Service is called Translator (ITranslator). My problem is that I don't want to retrieve this class in every controller (which returns a view) so I may use it in my views. I've a similar problem with a ListService (where I store all my SelectListItem lists). CustomWebViewPage public abstract class CustomViewPage<TModel> : WebViewPage<TModel> { readonly IScriptFactory scriptFactory; readonly IMembership membership;

Simple Injector Web Api Controller Constructor Injection Failing

你离开我真会死。 提交于 2019-12-10 23:37:12
问题 My Web Api app is failing when trying to instantiate an injected controller. I'm using Simple Injector. Bootstrapping the injector is as follows: [assembly: WebActivator.PostApplicationStartMethod(typeof(SimpleInjectorWebApiInitializer), "Initialize")] namespace WebApi { using System.Web.Http; using SimpleInjector; using SimpleInjector.Integration.WebApi; public static class SimpleInjectorWebApiInitializer { public static void Initialize() { var container = new Container();

Register IOC container to self

房东的猫 提交于 2019-12-10 23:05:32
问题 Let's say I have an interface IDependencyResolver: public interface IDependencyResolver{ T Resolve<T>() where T: class; object Resolve(Type source); } And an implementation with the use of SimpleInjector: public class SIDependencyResolver:IDependencyResolver{ private readonly Container _container; public SIDependencyResolver(Container container){ _container = container; _container.Options.DefaultScopedLifestyle = new WcfOperationLifeStyle(); RegisterDependencies(_container, LifeStyle.Scoped);

Nunit testing with Mock. Instance of Interface

做~自己de王妃 提交于 2019-12-10 18:55:22
问题 I have the following (simplified) code. public class Controller { private readonly IService _service; public Controller(IService service) { _service = service; } public async Task<IHttpActionResult> Create(MyObject object) { var result = _service.method(object); if (!result.Succeeded) { return this.GetErrorResult(object); } } } and SimpleInjector is used to inject the dependency between _service and its implementation class, like this: public static void Register(Container container) {

RegisterWithContext and Lifestyle Mismatch

旧巷老猫 提交于 2019-12-10 18:43:58
问题 I wanted to inject a logger to my controllers and I needed to pass extended info to the logger's constructor. For the purpose I've use RegisterWithContext : container.RegisterWithContext<Common.Logging.ILogger>(context => { if (context.ServiceType == null && !container.IsVerifying()) { throw new InvalidOperationException( "Can't request ILogger directly from container, " + "it must be injected as a dependency."); } return new Common.Logging.NLogLogger(context.ImplementationType.FullName); });

Injecting a dependency into OWIN Middleware and per web-request with Simple Injector

蹲街弑〆低调 提交于 2019-12-10 17:49:07
问题 I am trying to work out how to best inject all of my dependencies into the custom OWIN Middleware I have written for a Web API Application. A simple example would be the DbContext I am using. I have some middleware that needs to potentially query based on the request. The issue I have is that I want my DbContext to otherwise have a WebApiRequestLifestyle scope. My DbContext is registered as such: container.Register<MobileDbContext>(Lifestyle.Scoped); Obviously, this does not work: container

Simple injector and internal constructors

冷暖自知 提交于 2019-12-10 16:51:20
问题 I am working on a small class library and using Simple Injector for my DI. The class library has an access point (some sort of a service I guess) which is public and it has some internal services and repositories. I saw that Simple Injector does not support constructor injection with internal constructors. For example, my product service looks like: internal class ProductService : IProductService { private IProductRepository _productRepository; internal ProductService(IProductRepository

Unable to Create DbContext per Request with Simple Injector

别来无恙 提交于 2019-12-10 14:24:48
问题 Simple Inject is throwing the following exception when attempting to register my DbContext. The supplied connection string is not valid, because it contains insufficient mapping or metadata information. Parameter name: connectionString I'm new to DI and could be missing something fairly obvious. The connection string looks fine. It is the same one that gets used to create the DbContext normally. I was attempting the solution here public static class SimpleInjectorInitializer { /// <summary

UserManager dependency injection with Owin and Simple Injector

隐身守侯 提交于 2019-12-10 10:58:42
问题 After adding Simple Injector to my project to create an instance of my UserAppManager that will exist through the whole lifetime of a session I started to recieve errors: Error with no parameterless constructor: An error occurred when trying to create a controller of type 'AuthController'. Make sure that the controller has a parameterless public constructor. Error with a parameterless constructor: For the container to be able to create AuthController it should have only one public constructor