Tag: dependency injection

DI和存储库模式

目前,我的代码与此类似(缩短只是为了说明一点): DAL 存储库接口 public interface IRepository { IList GetAll(); TEntity Get(TKey id); TEntity Add(TEntity item); TEntity Update(TEntity item); bool Remove(TKey id); } 基本EF存储库 public class BaseEFRepository : IRepository where TEntity: class, IEntity where TKey: struct { protected readonly DbContext _dbContext; public BaseRepository() { _dbContext = new MyDB(); _dbContext.Configuration.ProxyCreationEnabled = false; _dbContext.Configuration.LazyLoadingEnabled = false; } public […]

团结和代表

我正在使用Unitydependency injection框架。 我有两个类,每个类在构造函数中使用相同的delegate参数。 解决后,每个类应该获得不同的方法。 我可以不使用属性进行设置吗? 如果不是,你会如何使用属性?

通过.NET Core控制台应用程序中的dependency injection访问配置

如何正确激活使用ServiceCollection.Configure函数添加的配置? public static void Main(args[] args) { serviceCollection = new ServiceCollection(); serviceCollection .Configure(Configuration.GetSection(“options”)); Services = serviceCollection.BuildServiceProvider(); ActivatorUtilities.CreateInstance(Services); ActivatorUtilities.CreateInstance(Services); } public SomeClassThatNeedsoptions(IOptions options) { _options = options.Value; } 在第二个ActivatorUtilities.CreateInstance我收到以下错误 无法解析类型’Microsoft.Extensions.Options.IOptions [MyOptions]`的服务 我在控制台应用程序中写这个,据我所知,我使用ActivatorUtilities类手动注入依赖项。 我似乎能够使用添加了AddSingleton的服务来完成它,而不是使用.Configure添加的服务

具有entity framework6的多对多通用更新方法

我在抽象的DatabaseOperations类中有一个用于Entity Framework的通用Update方法: public virtual void Update(T updatedObject, int key) { if (updatedObject == null) { return; } using (var databaseContext = new U()) { databaseContext.Database.Log = Console.Write; T foundEntity = databaseContext.Set().Find(key); databaseContext.Entry(foundEntity).CurrentValues.SetValues(updatedObject); databaseContext.SaveChanges(); } } 但是,这不能处理多对多关系。 通过覆盖TrussSetDatabaseOperations : DatabaseOperations的Update方法可以克服这种多对多更新问题TrussSetDatabaseOperations : DatabaseOperations ,如下所示: public override void Update(TrussSet updatedTrussSet, int key) { if (updatedTrussSet == null) { […]

运行时服务不再注入DNX控制台应用程序(RC1)

我曾经能够将运行时服务(如IApplicationEnvironment注入DNX控制台应用程序的Pogram类的构造函数中。 但是,使用RC1的最新CI版本,服务不再被注入: public Program(IApplicationEnvironment env) { if (env == null) { // env is null. throw new ArgumentNullException(nameof(env)); } }

希望Autofac不注册任何具有多个实现的接口

所以我目前正在为我们公司测试Autofac。 我们想要遵守以下规则: 如果一个接口只实现了一次,那么使用builder.RegisterAssemblyTypes自动添加它(见下文)。 否则,我们需要确保手动编写将决定哪个实现是“默认”实现的规则。 我有以下代码: var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly .Load(“Lunch.Service”)).As(t => t.GetInterfaces()[0]); builder.RegisterType() .As().SingleInstance(); builder.RegisterModule(new DestinationModule()); builder.RegisterType() .As().PropertiesAutowired(); 现在,它正在工作,但它决定了第一个实现是哪个,并将自动创建。 如果我们不手动创建“规则”,我们希望将其设为手动过程并抛出错误。 这可能吗?

无法使用autofac注册结构实例

我刚开始从Unity迁移到Autofac ,我在尝试注册实例时遇到了问题。 public static void Register(ContainerBuilder containerBuilder, CancellationToken shutDownCancellationToken) { containerBuilder.RegisterType(); containerBuilder.RegisterInstance(shutDownCancellationToken); } 我收到以下错误: “ CancellationToken ”类型必须是引用类型才能在generics类型或方法“ RegistrationExtensions.RegisterInstance(ContainerBuilder, T) ”中将其用作参数“ T ” 任何人都知道如何注册已经创建的结构实例?

RegisterWithContext和Lifestyle Mismatch

我想向我的控制器注入一个记录器,我需要将扩展​​信息传递给记录器的构造函数。 为此,我使用RegisterWithContext : container.RegisterWithContext(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); }); RegisterWithContext扩展方法显式将提供的委托注册为Transient 。 我需要在一个恰好是单例的服务中注入相同的Logger( Common.Logging.NLogLogger )。 在升级到SimpleInjector 3.0.6之前,事情似乎按预期工作,而container.Verify()对整个配置非常满意。 升级后,validation程序返回一些错误: [Lifestyle Mismatch] SearchEngineIndexerService(Singleton)依赖于ILogger(Transient)。 [Lifestyle Mismatch] MembershipService(Web Request)取决于ILogger(Transient)。 这是有道理的。 我能理解为什么会发生这种情况以及为什么要避免它。 我试图避免“我是否记录太多”综合症,但实际上,我确实需要在一些服务中进行一些记录。 我已经尝试使用RegisterConditional根据某些条件注册一个不同的记录器,但是,当然,所有记录器现在应该是有条件注册的,否则我会得到这个exception: […]

MVC请求的Ninject Contextual绑定

我有一个不寻常的情况将服务注入ASP.NET MVC控制器。 Controller提供单个动作来在页面上呈现侧栏菜单,注入Controller的服务是创建侧栏内容的工厂。 该动作使用[ChildActionOnly]属性进行修饰:侧栏只能在渲染另一个动作时渲染。 困难在于我想根据请求的页面(= Controller)注入侧栏工厂抽象的不同实例。 以前,我使用的是一种抽象工厂,它使用控制器名称字符串来确定要使用的具体工厂实现的不优雅实现; 我现在把它转移到一个合适的抽象工厂,因此需要在其他地方移动工厂类型的选择。 我的Ninject绑定目前非常简单地定义为: Kernel.Bind().To().InRequestScope(); Kernel.Bind().To().InRequestScope(); 当我添加更多控制器时,我将添加第一行的更多实例。 我希望看到这个工作的方式是: 收到/foo/action请求 Ninject将ISideBarFactory绑定到FooSideBarFactory并注入SideBarController /bar/action请求已收到 Ninject将ISideBarFactory绑定到BarSideBarFactory并注入SideBarController 收到/baz/action要求 No BazSideBarFactory存在,因此Ninject将ISideBarFactory绑定到默认实现DefaultSideBarFactory ,并注入SideBarController 我已经咨询了关于Contextual Binding的Ninject wiki页面,这似乎是我原则上想要的 ,但我没有找到任何记录在那里显然达到我的目标。

SimpleInjector针对每个Web请求和生命周期范围混合生活方式

我使用Simple Injector作为我的IoC容器,并使用以下技术为每个Web请求或每个线程注册某些对象的“混合”生活方式。 interface IUnitOfWork { } interface IWebUnitOfWork : IUnitOfWork { } interface IThreadUnitOfWork : IUnitOfWork { } class UnitOfWork : IWebUnitOfWork, IThreadUnitOfWork { } container.RegisterPerWebRequest(); container.RegisterLifetimeScope(); container.Register(() => container.GetInstance()); // Register as hybrid PerWebRequest / PerLifetimeScope. container.Register(() => { if (HttpContext.Current != null) return container.GetInstance() as UnitOfWork; else return container.GetInstance() as UnitOfWork; }); 我对这个解决方案并不完全满意,因为每个要求我必须定义额外的空接口以使其工作并确保它们由我的具体类引用。 […]