dependency-injection

ASP.NET Core option dependency in constructor

强颜欢笑 提交于 2019-12-19 10:08:42
问题 I'm using ASP.NET Core and I'm attempting to create a resolvable class which has an optional parameter: public class Foo { public Foo() : this(null) {} public Foo(IValidator<FooEntity> validator) { } } I've created two constructors for this object so that if a dependency isn't found, I would assume it would just fall back to the default constructor. However when I run my application I receive this error Additional information: Unable to resolve service for type 'FluentValidation.IValidator`1

SimpleInjector: Injection does not work with MVC 4 ASP.NET Web API

情到浓时终转凉″ 提交于 2019-12-19 09:57:09
问题 I have this setup: public static void Initialize(ISessionFactory factory) { var container = new Container(); InitializeContainer(container, factory); container.RegisterMvcControllers( Assembly.GetExecutingAssembly()); container.RegisterMvcAttributeFilterProvider(); container.Verify(); DependencyResolver.SetResolver( new SimpleInjectorDependencyResolver(container)); } private static void InitializeContainer( Container container, ISessionFactory factory) { container.RegisterPerWebRequest

DbContext Dependency Injection outside of MVC project

别等时光非礼了梦想. 提交于 2019-12-19 09:54:13
问题 I have a C# solution with two projects, ProductStore.Web and ProductStore.Data, both targeting .NET Core 2.0. I have my HomeController and CustomerRepository as follows (I've set it up in the HomeController for speed, customer creation will be in the customer controller, but not yet scaffold-ed it out): namespace ProductStore.Web.Controllers { public class HomeController : Controller { private readonly DatabaseContext _context; public HomeController(DatabaseContext context) { _context =

Class-level injectable dependencies in Angular 1.x using TypeScript?

不羁岁月 提交于 2019-12-19 09:25:15
问题 Sorry for the somewhat convoluted title. I have been using Angular 1.x for a couple years now and have recently been investigating the potential of using TypeScript in a codebase I'm refactoring (also in Angular 1.x). Currently I'm having trouble thinking my way through making injectable dependencies of a class (and class families), rather than dependencies of constructor functions. Angular has a strong bias towards building applications out of singleton services, but I've found these

AutoFac - Registering a decorator for some of an open Generic

拥有回忆 提交于 2019-12-19 09:10:22
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

AutoFac - Registering a decorator for some of an open Generic

北城余情 提交于 2019-12-19 09:10:15
问题 I'm attempting to set up an Autofac module that seems to have a complicated requirement. here goes: I have a generic interface: public interface IMyInterface<TFoo, TBar> I have a bunch of classes that implement this interface e.g. class MyImpl1 : IMyInterface<string, bool> { } class MyImpl2 : IMyInterface<bool, string> { } class MyImpl3 : IMyInterface<bool, string> { } Finally, I have a decorator: class MyDecorator<TFoo, TBar> : IMyInterface<TFoo, TBar> I only want to "Decorate" the

Multi tenancy with Guice Custom Scopes and Jersey

主宰稳场 提交于 2019-12-19 08:56:57
问题 I am in the process of developing a multi tenancy application with Jersey using Guice for DI (I also use Dropwizard but I don't think it matters here). One thing that bothers me is the fact that some kind of tenancy_id is all over the place in my application. Most of my URLs look like this: /:tenancy_id/some_resource/do_stuff . So the method in my Jersey resource is called with the tenancy_id and hands it over to a service which calls other services and so on. These services are configured

Unit testing value objects in isolation from its dependencies

淺唱寂寞╮ 提交于 2019-12-19 08:50:45
问题 TL;DR How do you test a value object in isolation from its dependencies without stubbing or injecting them? In Misko Hevery's blog post To “new” or not to “new”… he advocates the following (quoted from the blog post): An Injectable class can ask for other Injectables in its constructor.(Sometimes I refer to Injectables as Service Objects, but that term is overloaded.). Injectable can never ask for a non-Injectable (Newable) in its constructor. Newables can ask for other Newables in their

Wiring up Simple Injector in WebForms in .NET 4.7.2

戏子无情 提交于 2019-12-19 07:09:21
问题 With the changes in .NET 4.7.2, constructor injection is now possible in Web Forms. I have gotten Simple Injector working with Web Forms, but would like some input as to if there any "gotchas" I might be missing. First I have the registration of the Pages themselves which is taken from here. public static void RegisterWebPages(this Container container) { var pageTypes = from assembly in BuildManager.GetReferencedAssemblies().Cast<Assembly>() where !assembly.IsDynamic where !assembly

AngularJS Filter with TypeScript and injection

。_饼干妹妹 提交于 2019-12-19 07:08:27
问题 Can somebody provide me with an example of how I can create an Angular Filter in TypeScript that uses dependency injection. At the bottom is what I currently have, which is working fine, but what I want to do is in is the function I want to have access to $filter, so that I can change the line return date.ToString() into $filter'date'. That way I use the built in date filter to show a nice friendly short date. class FriendlyDateFilter { public static Factory() { return function (input):