我已经使用EntLib一段时间了,最近发现了Unity.乍一看,它似乎可以处理EntLib所做的大部分工作,但看起来重量更轻.
哪一天推荐,MVVM架构,以及一个的优点/缺点?
谢谢.
我有一个MVC应用程序Controller需要一个IDomainService.在DomainService类的构造函数需要一个IRepository
与Repository类的构造函数需要一个连接字符串.
我有这样的事情:
string connectionString = ConfigurationManager
.ConnectionStrings["ApplicationServices"].ConnectionString;
c.RegisterInstance("ConnectionString", connectionString);
c.RegisterType<IDepartmentRepository, DepartmentRepository>(
"DepartmentRepository",
new InjectionConstructor(connectionString));
c.RegisterType<IDepartmentDomainService, DepartmentService>(
new InjectionConstructor(
new ResolvedParameter<IDepartmentRepository>(
"DepartmentRepository")));
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.
我做错了吗?我对存储库注入没有任何问题,但我无法注入(使用存储库)我的域服务.
谢谢.
c# asp.net-mvc dependency-injection ioc-container unity-container
在我的WCF Web应用程序中,我已经为拦截配置了Unity容器.以下是我的统一配置.
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
<assembly name="Infrastructure" />
<assembly name="WCFServiceLib1"/>
<namespace name="Infrastructure"/>
<namespace name="WCFServiceLib1" />
<container>
<extension type="Interception" />
<register type="IService1" mapTo="Service1">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="LogPerformanceDataBehavior"/>
</register>
</container>
</unity>
Run Code Online (Sandbox Code Playgroud)
当我尝试使用wcftestclient工具调用服务上的方法时,抛出以下异常.
ArgumentException - 类型WCFServiceLib1.Service1不可拦截.
参数名称:interceptedType
我使用svctraceviewer工具获取上述异常详细信息.
以下是LogPerformanceDataBehavior类的实现
public class LogPerformanceDataBehavior : IInterceptionBehavior
{
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
var watch = new Stopwatch();
watch.Start();
IMethodReturn methodReturn = getNext()(input, getNext);
watch.Stop();
string sb = string.Format("Method {0}.{1} executed in: ({2} …Run Code Online (Sandbox Code Playgroud) 我想获得使用Unity 3的一些经验,所以我设置了一个控制台应用程序并从nuGet下载了Unity.
但是我没有找到关于如何启动Unity的任何示例?我是否需要在main方法中添加一些代码来制作Unity容器,或者将该代码放入另一个文件并调用它是否正常?
请注意,我确实找到了"Unity动手实验",但这些实验教程看起来太复杂了.我真的想找到一些用C#编码的初学者示例,如果有人有任何链接,这将是一个很好的帮助.
对于我的单元测试我目前正在使用Moq模拟我的拦截器和拦截类,然后在Unity中注册截获的实例并为接口设置默认拦截器.然后我解析实例并调用截获的方法并验证是否正在调用拦截方法.
_mockInterceptor = new Mock<ExternalInterceptor>().As<IInterceptRelay>();
_mockInterception = new Mock<TestInterception> { CallBase = true }.As<IInterceptRelay>();
Container.RegisterInstance(_mockInterception.Object as ITestInterception);
UnityContainer.Configure<Interception>().SetDefaultInterceptorFor<ITestInterception>(new InterfaceInterceptor());
var test = Container.Resolve<ITestInterception>();
var returnValue = test.TestTheExternalInterception(TestMessage);
_mockInterceptor.Verify(i => i.ExecuteAfter(It.IsAny<object>(), It.IsAny<IEnumerable>(), It.IsAny<LoggingInterceptionAttribute>(), It.IsAny<IMethodResult>()), Times.Once());
Run Code Online (Sandbox Code Playgroud)
这很好用,但是我宁愿在注册期间设置拦截,就像我在注册服务/单例时保持一致性一样.
// Typical registration
UnityContainer.RegisterType<TFrom, TTo>(new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());
// Singleton registration
UnityContainer.RegisterType<TFrom, TTo>(new ContainerControlledLifetimeManager(), new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());
Run Code Online (Sandbox Code Playgroud)
我看不到使用该IUnityContainer.RegisterInstance()方法配置拦截的任何方法,因为它不需要任何方法InjectionMembers.如果我UnityContainer.Configure<Interception>().SetDefaultInterceptorFor<T>()在解析之前调用,我实际上可以使用实例拦截.
是否有更好/更简单的注册或嘲笑拦截器的方法?
c# unit-testing unity-container unity-interception interception
我正在使用反射来创建用户将在动态生成的菜单中使用的方法列表(我是统一的).我在用:
MethodInfo[] methodInfos = myObject.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
Run Code Online (Sandbox Code Playgroud)
但并不是所有类的公共方法都应该出现在这个菜单中,所以我想知道,是否有一些标志可以用来标记我需要的方法?
然后使用这个"自定义标志"来通过反射来获取这些方法.谢谢 :).
我在Winforms应用程序中通过将依赖项注入到表单的构造函数中来使用统一性(我知道这不是最佳实践),但是它确实起作用,但是当我尝试加载MDI表单时出现了stackoverflowexception。
有什么方法可以追踪团结正在试图解决的问题,并以某种方式找出正在发生的事情?
我有一个工作示例,我知道使用此“设计”可以工作。
我知道这不是一个理想的设计,我计划在线下引入一个Presenter,但现在应该可以了,我不知道为什么不这样
编辑:我知道我有例如互相引用的服务
public class Service1(IService2, IService3, IService4):IService1
public class Service2(IService1, IService5):IService2
Run Code Online (Sandbox Code Playgroud)
这会导致异常吗?
EDIT2:是的,我刚刚用第一次编辑中列出的循环引用创建了一个快速应用程序,并且得到了StackOverflowException-显然是不允许的。
我正试图在导航到ViewModel时将服务注入.尽管我已经看过Brian Lagunas的博客,示例代码等,但有些东西似乎不对.
在我的App.cs中给出
public class App : PrismApplication
{
protected override void OnInitialized ()
{
NavigationService.NavigateAsync ("SignInPage", animated: false);
}
protected override void RegisterTypes ()
{
Container.RegisterTypeForNavigation<SignInPage>();
Container.RegisterType<IUserAuthentication, UserAuthenticationService>();
}
Run Code Online (Sandbox Code Playgroud)
}
注意:usingIUserAuthentication和UserAuthenticationService命名空间的语句已到位.
SignInPageViewModel.cs
private INavigationService _navigationService;
private IUserAuthentication _userAuthenticationService;
public SignInPageViewModel(INavigationService navigationService, IUserAuthentication userAuthenticationService)
{
_navigationService = navigationService;
_userAuthenticationService = userAuthenticationService;
}
Run Code Online (Sandbox Code Playgroud)
Container.RegisterType似乎不接受通用.我在Unity中看到的所有示例似乎都支持它,但是我收到以下错误.
The non-generic method 'IUnityContainer.RegisterType(Type, Type, string, LifetimeManager, params InjectionMember[])' cannot be used with type arguments
Run Code Online (Sandbox Code Playgroud)
这是一个构建错误.
包装信息
我有我的MVC5 Web控制器并尝试使用以下方法进行依赖注入:
public class PatientsController : Controller
{
public ISomeRepository _repo;
// GET: Patients
public ActionResult Index(ISomeRepository repo)
{
_repo = repo;
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
我的Unity配置如下所示:
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
container.RegisterType<ISomeRepository, SomeRepository>();
// GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = new UnityResolver(container);
}
Run Code Online (Sandbox Code Playgroud)
但是当我导航到控制器时,收到错误:
[MissingMethodException:无法创建接口的实例.]
unity-container ×10
c# ×7
asp.net-mvc ×2
.net ×1
aop ×1
asp.net ×1
bindingflags ×1
interception ×1
mvvm ×1
prism ×1
reflection ×1
unit-testing ×1
wcf ×1