标签: unity-container

依赖注入在 MVVM 中以对话框形式启动视图

我需要一些关于如何处理 ViewModel 中的依赖注入的建议。我的 viewModelMenuViewModel有一个ICommand方法,当用户单击视图中的按钮时将运行该方法。该方法将打开一个新窗口。该方法如下所示。

public void bookingCommand_DoWork(object obj)
{
    BookingView bookingView = new BookingView();
    BookingViewModel model = new BookingViewModel();
    bookingView.DataContext = model;

    bookingView.ShowDialog();
}
Run Code Online (Sandbox Code Playgroud)

BookingView它创建和的实例BookingViewModel。我正在尝试使用依赖项注入而不是创建这样的实例。

菜单视图模型

public class MenuViewModel : IViewMainWindowViewModel
{
    //commands
    public ICommand bookingCommand { get; set; }

    public MenuViewModel()
    {
        bookingCommand = new RelayCommand(bookingCommand_DoWork, () => true);
    }

    public void bookingCommand_DoWork(object obj)
    {
        BookingView bookingView = new BookingView();
        BookingViewModel model = new BookingViewModel();
        bookingView.DataContext = model;

        bookingView.ShowDialog();
    }
}
Run Code Online (Sandbox Code Playgroud)

这 …

c# wpf dependency-injection unity-container

5
推荐指数
1
解决办法
2568
查看次数

为什么 Unity 在解析注册为某个接口的实现的类时应用注入行为

我像这样注册依赖项:

this.Container.RegisterType<IFoo, Foo>(
    new ContainerControlledLifetimeManager(),
    new InterceptionBehavior<PolicyInjectionBehavior>(), new Interceptor<InterfaceInterceptor>());
Run Code Online (Sandbox Code Playgroud)

这很好用。但是,当我解决Foo而不是IFoo出现错误时:

ArgumentException - 传递的类型必须是接口。

看来 Unity 仍在尝试应用InterfaceInterceptor此依赖项。但为什么?

.net c# dependency-injection unity-container interception

5
推荐指数
0
解决办法
68
查看次数

具有依赖注入的单例类 C#

我们有一个带有 QCServiceLog 类的外部项目,该类具有由 Unity 解析的 ILogging 依赖项。\n但 QCServiceLog 是一个 Singleton 类,如以下示例所示:

\n
private readonly ILogging _logging = null;\n\nprivate static QCServiceLog _instance = null;\npublic static QCServiceLog Instance\n{\n    get\n    {\n        return _instance;\n    }\n}\n\npublic QCServiceLog(ILogging logging)\n{\n    _logging = logging;\n    if (_instance == null)\n    {\n        _instance = this;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我们正在尝试使用它,在我们的解决方案中,我们进行了如下注册:

\n
uc.RegisterType<ILogging, QCFileManager>(new ContainerControlledLifetimeManager());\n
Run Code Online (Sandbox Code Playgroud)\n

但由于 QCServiceLog 是单例,我们相信代码永远不会通过构造函数,因此 _instance 永远不会实例化。\n我们使用它来执行以下操作:

\n
QCServiceLog.Instance.Log(ex);\n
Run Code Online (Sandbox Code Playgroud)\n

Singleton 是否正确实现?我们相信它\xc2\xb4s永远不会做的QCServiceLog。

\n

你怎么认为?我们可以在不更改外部项目的情况下做些什么吗?\n你可以想象的例外是:

\n

你调用的对象是空的。

\n

我将衷心感谢您的帮助!

\n

singleton dependency-injection unity-container

5
推荐指数
2
解决办法
2万
查看次数

Web Api 中的 Unity 容器 PerRequestLifetimeManager

我有一个 WEB API 应用程序,并且使用 UnityContainer 作为我的 DI。由于我将 UnitOfWork 与实体框架结合使用,因此我想在每个请求的 UnityContainer 中注册 DataContext。我很失望地发现没有 LifeTimeManager 可以为每个请求提供新的范围。经过进一步挖掘,我发现 Unity.AspNet.Mvc 确实包含一个 PerRequestLifetimeManager。我的问题是:

  1. Unity 不包含 PerRequestLifetimeManager 是否有原因?在我看来,根据请求注册某些内容是很常见的情况。
  2. Unity.AspNet.Mvc 包含 PerRequestLifetimeManager 是否有特殊原因?
  3. 如果我在 WebApi 项目中使用 Unity.AspNet.Mvc,有什么需要了解的吗?
  4. 如果使用线程或任务,容器如何知道它属于同一个请求?

c# dependency-injection unity-container asp.net-web-api asp.net-web-api2

5
推荐指数
1
解决办法
2766
查看次数

为什么需要NativeArray来获取Unity作业系统的返回值?

使用下面的代码块作为参考,一次更新可提供以下结果:

intReturn = 0

inputArrayReturn = 1

internalArrayReturn = 1

这里对我来说唯一有意义的值是internalArrayReturn.

为什么不intReturn等于1?即使在执行过程中应该将 1 添加到结构中,但到底发生了什么导致该结构的值没有改变?如果这只是 NativeArray 和 int 之间的根本区别,有人可以解释一下或给我指出一个参考资料来帮助我更好地理解这一点吗?

另外,为什么不inputArrayReturn等于0? NativeArray是一个结构体,所以当该行执行时它不应该被视为值的副本而不是引用吗testArray = testArrayResults?如果是这种情况,则 theninputArrayReturn将保持等于 0,而不是更新为 中包含的相同值testJob.testArray

using UnityEngine;
using Unity.Jobs;
using Unity.Collections;

public class ParallelTesting : MonoBehaviour
{
    void Update()
    {
        NativeArray<int> testArrayResults = new NativeArray<int>(1, Allocator.TempJob);

        TestJob testJob = new TestJob
        {
            testInt = 0,
            testArray = testArrayResults
        };

        JobHandle testJobHandle = testJob.Schedule();
        testJobHandle.Complete();

        int intReturn …
Run Code Online (Sandbox Code Playgroud)

c# parallel-processing jobs unity-container unity-game-engine

5
推荐指数
1
解决办法
3828
查看次数

为通用接口和类对指定默认的Unity类型映射

我们在代码库上使用基于构造函数的依赖注入,AutoMapperUnity.

我们用一个通用接口包装了AutoMapper ......

public interface IMapper<TSource, TDestination>
{
    TDestination Map(TSource source);
}
Run Code Online (Sandbox Code Playgroud)

还有一个实现这个接口的类......

public class AutomaticMapper<TSource, TDestination> : IMapper<TSource, TDestination>
{
    public TDestination Map(TSource source)
    {
        return AutoMapper.Mapper.Map<TSource, TDestination>(source);
    }
}
Run Code Online (Sandbox Code Playgroud)

这很有效,但这意味着对于我们在AutoMapper配置中定义的每个映射,我们需要执行额外的操作UnityContainer.RegisterType.

这些类型映射几乎总是这种形式

container.RegisterType<IMapper<ClassA, ClassB>, AutomaticMapper<ClassA, ClassB>>();
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以告诉团结使用从映射IMapperAutomaticMapper使用相同TSourceTDestination每个映射的默认类型映射?

dependency-injection unity-container automapper

4
推荐指数
1
解决办法
2473
查看次数

Microsoft.Practices.Unity.ResolutionFailedException

安装我的WPF应用程序后,我尝试运行该应用程序,它崩溃并在事件查看器中记录下面的错误.有人有想法吗?

'----------------------------------------------------------------------

Application: MyApp.Windows.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: Microsoft.Practices.Unity.ResolutionFailedException
Stack:
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(System.Type, System.Object, System.String, System.Collections.Generic.IEnumerable`1<Microsoft.Practices.Unity.ResolverOverride>)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(System.Type, System.String, Microsoft.Practices.Unity.ResolverOverride[])
   at Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Microsoft.Practices.Unity.IUnityContainer, Microsoft.Practices.Unity.ResolverOverride[])
   at MyApp.Windows.IoC.Resolve[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]()
   at MyApp.Windows.Navigation.NavigationController.Navigate(System.String, System.Windows.Controls.UserControl)
   at MyApp.Windows.LoginWindow..ctor()
   at MyApp.Windows.App.OnStartup(System.Windows.StartupEventArgs)
   at System.Windows.Application.<.ctor>b__1(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, …
Run Code Online (Sandbox Code Playgroud)

unity-container

4
推荐指数
1
解决办法
7837
查看次数

ASP.NET MVC依赖注入Unity与WCF服务 - 工作示例解决方案

我正在寻找使用Unity并调用WCF服务的ASP.NET MVC Web应用程序的工作示例.我已经看了很多关于如何向WCF服务添加依赖注入的解释,但坦率地说,我在这里有点过头了.我也不熟悉WCF服务.

我目前正在为我们的ASP.NET MVC应用程序使用Unity with Contructor injection,但到目前为止我们还没有使用任何WCF Web服务.计划是开始使用Web服务,我对如何将Unity与它们合并感到困惑.

我会喜欢一个很好的工作样本,我可以通过它来更好地了解如何去做.

asp.net-mvc wcf dependency-injection unity-container

4
推荐指数
1
解决办法
5159
查看次数

Unity中的Singleton Per Call上下文WCF Web请求

在Unity中经历了Singleton Per Call Context(Web Request)的问题.

基本上我想使用Unity Container为每个wcf请求创建一个单例对象.虽然我发现其他问题的答案对ASP.Net Web应用程序有帮助,但我不确定这些答案是否仍然适用于WCF服务.

问题是,在WCF服务中使用CallContext和HttpContext,我们可以创建PerCallContextOrRequestLifeTimeManager吗?这会为每个wcf调用提供单例对象吗?

wcf unity-container

4
推荐指数
1
解决办法
2360
查看次数

在MVC 4应用程序中使用Unity with Entity Framework的最佳实践是什么

我正在努力使用MVC 4应用程序中的Entityframework,利用Unity for Dependency注入和Automapper将对象自动化到DTO.我从一个问题跑到另一个问题,EF有时会返回旧数据,所以我认为我的设计不够好.

我有什么:

要在我的Application_Start中配置Unity:

var UnityContainer = UnityConfig.GetConfiguredContainer();
Mapper.Initialize(cfg => cfg.ConstructServicesUsing(type => UnityContainer.Resolve(type)));
UnityContainer.Resolve<AutoMapperConfig>().MapAutoMapper();
...
Run Code Online (Sandbox Code Playgroud)

在UnityConfig.RegisterTypes中:

container.RegisterType<IMyContext, MyContext>(new ContainerControlledLifetimeManager())
...
Run Code Online (Sandbox Code Playgroud)

我的存储库使用构造函数依赖注入:

public class MSSQLTenantRepository : IDalTenantRepository
{
   private readonly IMyContext _Db;
   public MSSQLTenantRepository(IMyContext db)
   {
      Db = db;
   }
...
Run Code Online (Sandbox Code Playgroud)

我的控制器也使用构造函数依赖注入:

public class TenantController : Controller
{
   private readonly ITenantRepository _TenantRepository;
   public TenantController(ITenantRepository tenantRepository,
   {
      _TenantRepository = tenantRepository;
   }
...
Run Code Online (Sandbox Code Playgroud)

自动配置配置:

public class AutoMapperConfig
{
    private readonly ITenantRepository _TenantRepository;
    public AutoMapperConfig(ITenantRepository tenantRepository)
    {
        _TenantRepository = tenantRepository;
    }
... …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework dependency-injection unity-container asp.net-mvc-4

4
推荐指数
1
解决办法
1万
查看次数