dependency-injection

Dependency Injection Constructor that takes arguments

有些话、适合烂在心里 提交于 2019-12-24 10:32:13
问题 I'm building an Aurelia app that uses "Models" for every type of data object. All my Models look something like this: export class Item { id = null; name = ''; description = ''; constructor (data) { Object.assign(this, data); } } And I later create objects like this: export class SomeViewModel { activate () { this.myItem = new Item({ name: 'My Item!', description: 'This is my item. It will be initialized with these properties.' }); } } I got the Object.assign() bit from an article I read and

Overriding IOC Registration for use with Integration Testing

前提是你 提交于 2019-12-24 10:27:56
问题 so I think I'm perhaps not fully understanding how you would use an IOC container for doing Integration tests. Let's assume I have a couple of classes: public class EmailComposer : IComposer { public EmailComposer(IEmailFormatter formatter) { ... } ... public string Write(string message) { ... return _formatter.Format(message); } } OK so for use during the real application (I'm using autofac here) I'd create a module and do something like: protected override void Load(ContainerBuilder

Common code for dependency injection for web api, admin panel and mvc controller

此生再无相见时 提交于 2019-12-24 10:08:52
问题 Currently, I have below 4 projects in my solution file : API (Web API) Web (MVC) Admin (MVC) Service Layer (C# Library) The service layer is being used by all the 3 web projects. The service is injected using Autofac container. The services are registered in each of the web projects during startup which is causing duplication of the code. Is there a simpler way where I can register all the dependencies in one place so that it can be reused by all the projects? Any help is highly appreciated.

Dependency Injection fails with MVC5 and Ninject

时光毁灭记忆、已成空白 提交于 2019-12-24 09:52:44
问题 I'm trying to inject a couple of classes in a controller, but I'm failing. This is what I've done: Added Ninject.Web.WebApi.WebHost and WebActivatorEx NuGet packages Created the following class under App_Start : NinjectWebCommon.cs using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; using Ninject.Web.Common.WebHost; using MyProject; using MyProject.Models; using MyProject.Classes; using System; using System.Web; [assembly: WebActivatorEx

Is CreateChildContainer() Thread-Safe?

扶醉桌前 提交于 2019-12-24 09:38:31
问题 Back in 2009, which may have been pre-Unity 2.0, I see a thread of discussion which suggests CreateChildContainer() is not thread safe: Also, if you create child container with CreateChildContainer() method, they will add themself to its parent container’s livetime container during creation and remove when disposing. So, disposing child containers from separate threads may lead to errors, even each child container accessed only by one thread at time. Is that indeed (still) true for Unity 2.0?

Proper way of registering 3rd party DI Framework (Lamar/Autofac) on Azure functions V2

元气小坏坏 提交于 2019-12-24 09:29:10
问题 Azure Functions V2 now supports .net dependency injection In order to achieve that you need to do the following code: [assembly: FunctionsStartup(typeof(MyNamespace.Startup))] namespace MyNamespace { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddHttpClient(); builder.Services.AddSingleton((s) => { return new CosmosClient(Environment.GetEnvironmentVariable("COSMOSDB_CONNECTIONSTRING")); }); builder.Services

Testing - Stubbing service method is undefined

有些话、适合烂在心里 提交于 2019-12-24 09:27:53
问题 I have written a very simple test on a very simple code but for some reason stub service method is undefined. When I use Jasmine Spy it works but for such a simple task. could someone please explain why is this happening (i have removed import statements just to reduce code): businessarea-overview.component @Component({ templateUrl: './businessarea-overview.component.html', styleUrls: ['./businessarea-overview.component.css'] }) export class BusinessAreaOverviewComponent implements OnInit {

Ninject bind mulitple implementations to an interface

旧城冷巷雨未停 提交于 2019-12-24 09:26:19
问题 I'm looking at this tutorial, but not quite understanding how to create a factory that can provide me separate implementations of an interface. http://stefanoricciardi.com/2011/01/21/ninject-mini-tutorial-part-1/ public class IJob { ... } public class JobImpl1 : IJob { ... } public class JobImpl2 : IJob { ... } using (IKernel kernel = new StandardKernel()) { kernel.Bind<IJob>().To<JobImpl2>(); var job = kernel.Get<IJob>(); } My goal is to make a factory class that wraps this Ninject Kernel so

Using DI to add Interceptor to NHibernate Sessions in legacy code

若如初见. 提交于 2019-12-24 08:51:10
问题 So, there's a bug in some legacy code I'm maintaining. It causes some mild data corruption, so it's rather serious. I've found the root cause, and have made a sample application that reliable reproduces the bug. I would like to fix it with as little impact on existing applications as possible, but I'm struggling. The bug lies in the data access layer. More specifically, in how an interceptor is injected into a new Nhibernate Session. The interceptor is used to set a specific entity property

MVC Mocking (Moq) - HttpContext.Current.Server.MapPath

筅森魡賤 提交于 2019-12-24 08:39:31
问题 I have a method I am attempting to Unit Test which makes use of HttpContext.Current.Server.MapPath as well as File.ReadAllLines as follows: public List<ProductItem> GetAllProductsFromCSV() { var productFilePath = HttpContext.Current.Server.MapPath(@"~/CSV/products.csv"); String[] csvData = File.ReadAllLines(productFilePath); List<ProductItem> result = new List<ProductItem>(); foreach (string csvrow in csvData) { var fields = csvrow.Split(','); ProductItem prod = new ProductItem() { ID =