Tag: unity container

.Net RIA服务:DomainService需要一个无参数的构造函数?

我在带有一些Silverlight组件的ASP.Net应用程序中使用.Net RIA Services的July CTP。 我从Silverlight打电话给RIA服务。 当我尝试在我的域服务(LinqToEntitiesDomainService对象)中使用Unity和构造函数dependency injection时出现了我的问题。 Silverlight应用程序现在抱怨没有无参数构造函数。 我不想拥有无参数构造函数,我希望Unity能够解析构造函数参数。 这可能吗? 难道我做错了什么? 或者我应该找到另一种方法来注入我的构造函数参数? public class DashboardService : LinqToEntitiesDomainService { private IUserService userService; public DashboardService(IUserService userService) : base() { if (userService == null) { throw ExceptionBuilder.ArgumentNull(“userService”); } this.userService = userService; } … 这是我得到的错误: Webpage error details User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET […]

Unity解析多个类

如何获得microsoft unity以“构造”给定接口类型的类列表。 很简单的例子: List list = new List(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity, must i do this for all the concrete classes? //how does it know to give a list. Container.RegisterType();?? //What I want public […]

Unity 3和错误“类型名称或别名”xxxxx“无法解析。 请检查您的配置文件并validation此类型名称。“

有没有办法解决Unity 3的这个问题? 我已经尽一切可能绕过这个消息错误,但我无法解决; 我已经做了我在googles搜索中看到的所有内容。 我几乎放弃并尝试另一种DI解决方案。 我的配置文件: 我的界面: using Biblioteca.Transport; using System.Linq; namespace Biblioteca.Contracts { public interface IManterCategoriaBO { IQueryable GetAll(); CategoriaDTO GetById(int id); void Insert(CategoriaDTO dto); } } 我的具体课程: using Biblioteca.Contracts; using Biblioteca.Transport; using Biblioteca.Data; using System; using System.Linq; namespace Biblioteca.Business { public class ManterCategoriaBO : IManterCategoriaBO { public CategoriaDTO GetById(int id) { CategoriaDTO dto […]

Unity不使用类的默认构造函数

我有这堂课: public class Repo { public Repo() : this(ConfigurationManager.AppSettings[“identity”], ConfigurationManager.AppSettings[“password”]) { } public Repo(string identity,string password) { //Initialize properties. } } 我在web.config中添加了一行,以便Unity容器自动构建此类型。 但在执行我的应用程序期间,我收到此错误消息: “System.InvalidOperationException : the parameter identity could not be resolved when attempting to call constructor Repo(String identity, String password) –>Microsoft.Practices.ObjectBuilder2.BuildFailedException : The current Build operation ….” 1)为什么Unity不使用默认构造函数? 2)假设我希望Unity使用第二个构造函数(参数构造函数),如何通过配置文件将该信息传递给Unity?

使用unity xml config注册具有嵌套generics的接口

如果我有一个实现通用接口的类,可以使用unity xml配置来配置它。 public interface IReader { } public class Fund { } public class FundReader : IReader { } 和统一xml: 这只是使用以下代码: var container = new UnityContainer().LoadConfiguration(); var fundReader = container.Resolve<IReader>(); 但是,在某些情况下,阅读器中使用的类型周围有一个包装器。 例如,添加以下两个类: public class Wrapper { } public class WrappedFundReader : IReader<Wrapper> { } 如果我将以下内容添加到unity配置: 然后尝试使用以下方法解决它: var wrappedReader = container.Resolve<IReader<Wrapper>>(); 我得到以下exception: InvalidOperationException – The current […]

在(数据)注释中使用Unity的dependency injection

我正在使用Unity并且有一个标有数据注释的模型: public class SomeModel { [SlackDisplayName(“ED0CAD76-263E-496F-ABB1-A4DFE6DEC5C2”)] public String SomeProperty { get; set; } } 此SlackDisplayName属性是DisplayName的子类,它解析属性的静态显示名称。 我只是想通过满足这个标准来动态地做到这一点: 可以使用此注释。 我可以使用该注释实现多语言应用程序。 语言模板由GUID标识 我不能将文化ID传递给注释 所以,我的SlackDisplayName注释看起来像这样: /// /// Annotation for non-fixed display names /// public class SlackDisplayNameAttribute : DisplayNameAttribute { /// /// TODO /// /// public SlackDisplayNameAttribute(String identifierGUID) : this(Guid.Parse(identifierGUID)) { } /// /// TODO /// /// public SlackDisplayNameAttribute(Guid identifier) […]

使用全局Web APIfilter属性的Unitydependency injection

参考此CodePlex统一文章,我能够使用WebAPI控制器获取filter属性,如下所示: [MyFilterAttribute] public class TestController : ApiController {} 但是,如果我想在使用GlobalConfiguration的所有操作中应用我的filter属性,它将被剥离注入的依赖项: public class MyFilterAttribute : ActionFilterAttribute { [Dependency] public MyDependency { get; set; } public override void OnActionExecuting(HttpActionContext actionContext) { if (this.MyDependency == null) //ALWAYS NULL ON GLOBAL CONFIGURATIONS throw new Exception(); } } public static class UnityWebApiActivator { public static void Start() { var resolver = […]

配置Unity以解析采用装饰依赖项的类型,该依赖项的参数随注入的类型而变化

这是一个相当简单的装饰器模式场景,其复杂性在于,装饰类型具有一个构造函数参数,该参数取决于它被注入的类型。 我有这样的界面: interface IThing { void Do(); } 这样的实现: class RealThing : IThing { public RealThing(string configuration) { … implementation … } public void Do() { … implementation … } } 像这样的装饰者: class DecoratingThing : IThing { IThing _innerThing; public DecoratingThing(IThing thing) { _innerThing = thing; } public void Do() { _innerThing.Do(); } } 最后,我有一些需要IThing类型,称为Depender1 […]

RegisterType()中的InjectionMembers是什么?

我一直在使用Microsoft的Unity IOC容器。 RegisterType()方法有一堆重载,看起来类似于 IUnityContainer RegisterType(Type t, params InjectionMember[] injectionMembers); 我想知道injectionMembers参数何时用于? 我找不到任何关于它们的文档(即使它们在每次重载中都没有),我看过的示例代码都没有使用它们。 我在这里错过了什么吗? 它们不常用或者我只是错过了这些例子吗?

Prism,将Views和ViewModels与Unity连接,试图理解它

使用Unity创建视图和视图模型 使用Unity作为dependency injection容器类似于使用MEF,并且支持基于属性和基于构造函数的注入。 主要区别在于通常不会在运行时隐式发现类型; 相反,他们必须在容器中注册。 通常,您在视图模型上定义接口,以便视图模型的特定具体类型可以与视图分离。 例如,视图可以通过构造函数参数定义其对视图模型的依赖性,如此处所示。 C# public QuestionnaireView() { InitializeComponent(); } public QuestionnaireView(QuestionnaireViewModel viewModel) : this() { this.DataContext = viewModel; } 默认的无参数构造函数是允许视图在设计时工具(如Visual Studio和Expression Blend)中工作所必需的。 或者,您可以在视图上定义只写视图模型属性,如此处所示。 Unity将实例化所需的视图模型,并在实例化视图后调用属性setter。 C# public QuestionnaireView() { InitializeComponent(); } [Dependency] public QuestionnaireViewModel ViewModel { set { this.DataContext = value; } } 视图模型类型已在Unity容器中注册,如此处所示。 C# IUnityContainer container; container.RegisterType(); 然后可以通过容器实例化视图,如此处所示。 C# IUnityContainer container; […]