有人在C#写过自己的IOC容器吗?或者绝大多数人使用各种框架,如Spring.每个人的专业和意见是什么?
我有一些看起来像这样的代码:
public MyService(IDependency dependency)
{
_dependency = dependency;
}
public Message Method1()
{
_dependency.DoSomething();
}
public Message Method2()
{
_dependency.DoSomething();
}
public Message Method2()
{
_dependency.DoSomething();
}
Run Code Online (Sandbox Code Playgroud)
现在我刚刚意识到,因为依赖对象包含内部状态信息.我需要在每个方法调用中新建一个新实例
那么最好的方法是什么,仍然没有新的具体实例?
您是否会使用IoC容器并在每个方法中调用容器?或者是否有一种更流畅的方式,您只能拨打一个容器?
如果我没有使用IoC容器怎么办?是否有办法不在每个方法中新建一个具体实例?
我认为这个问题的答案非常明显,以至于没有人对这方面的写作感到困扰,但是它已经很晚了,我真的无法理解这个问题.
我一直在阅读IoC容器(在这种情况下是Windsor),我很想念你如何从代码的各个部分与容器对话.
我得到DI,我一直在做穷人的DI(空构造函数调用带有默认参数实现的重载注入构造函数)一段时间,我可以完全看到容器的好处.但是,我错过了一条至关重要的信息; 每次需要服务时,你应该如何引用容器?
我是否创建了一个我传递的全局内容?当然不是!
我知道我应该这样称呼:
WindsorContainer container = new WindsorContainer(new XmlInterpreter());
Run Code Online (Sandbox Code Playgroud)
(例如)当我想加载我的XML配置时,但是我该如何处理容器?每次创建一个新容器之后是否通过一些内部静态majicks或其他方式持久保存加载的配置,或者每次都必须重新加载配置(我猜不是,或生命周期无法工作).
不理解这会阻止我弄清楚生命周期是如何工作的,并继续使用一些IoC的功能
谢谢,
安德鲁
现在有很多依赖 注入框架可供选择.由于您使用的库,您以前经常被迫使用给定的依赖注入框架.但是,Common Service Locator库使库代码独立于注入框架.
学习所有这些所需的时间足以决定使用哪个是不合理的.我不相信我们已经达到了可以讨论最佳依赖注入框架的阶段.那么我应该问什么问题关于项目和我自己来帮助决定在特定情况下使用的最佳依赖注入框架?
了解为什么选择当前使用的依赖注入框架以及您是否仍然对此选择感到满意也很有用.
在比较依赖注入框架的样式时,是否还有一个有用的词汇表?
服务定位器库是否在现实生活中工作,或者您是否被迫在同一项目中使用许多不同的依赖注入框架?
使用每个依赖注入框架对代码进行折射是多么容易,例如ReSharper等工具是否适用于给定的框架?
只是试着继续关注IOC原则.
Q1:静态方法 - 是否应将带有静态辅助方法的util类与IOC连接?
例如,如果我有一个带有许多静态方法的HttpUtils类,我应该尝试通过IOC将它传递给其他业务逻辑类吗?
关注这个问题可能是:
Q2:Singletons - 通过Logger.getInstance()类型调用,通常可以访问它的内容.您是否通常将其保留原样,并且不使用IOC将记录器注入需要它的业务类?
问题3:静态类 - 我还没有真正使用过这个概念,但如果你转向基于IOC的方法,那么你是否有任何指导方针可以解决这个问题.
提前致谢.
我的目标是修改asp.net mvc的控制器注册表,以便我可以在单独的(子)程序集中创建控制器和视图,只需将View文件和DLL复制到主机MVC应用程序,新控制器就可以"插入" "到主机应用程序.
显然,我需要某种IoC模式,但我很茫然.
我的想法是引用一个带有system.web.mvc的子程序集,然后开始构建继承自Controller
以下内容的控制器类:
单独组装:
using System.Web;
using System.Web.Mvc;
namespace ChildApp
{
public class ChildController : Controller
{
ActionResult Index()
{
return View();
}
}
}
Run Code Online (Sandbox Code Playgroud)
可是一切都很好,花花公子.但后来我考虑修改主机应用程序的Controller注册表以在运行时加载我的新子控制器,我感到困惑.也许是因为我需要更深入地了解C#.
无论如何,我以为我需要创建一个CustomControllerFactory
类.所以我开始编写一个覆盖该GetControllerInstance()
方法的类.当我打字的时候,intellisence突然出现了:
主机MVC应用程序:
public class CustomControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return base.GetControllerInstance(requestContext, controllerType);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,在这一点上,我很茫然.我不知道那是做什么的.最初,我打算写一个像这样的服务定位器的"Controller Loader"类:
主机MVC应用程序:
public class ControllerLoader
{
public static IList<IController> Load(string folder)
{
IList<IController> controllers = new List<IController>();
// Get files in folder
string[] files …
Run Code Online (Sandbox Code Playgroud) 所以我在Spring 3.2中有很多泛型,理想情况下我的架构看起来像这样.
class GenericDao<T>{}
class GenericService<T, T_DAO extends GenericDao<T>>
{
// FAILS
@Autowired
T_DAO;
}
@Component
class Foo{}
@Repository
class FooDao extends GenericDao<Foo>{}
@Service
FooService extends GenericService<Foo, FooDao>{}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,对于泛型的多个实现,自动装配会引发有关多个匹配bean定义的错误.我假设这是因为@Autowired
类型擦除之前的进程.我找到或想出的每一个解决方案看起来都很难看,或者只是莫名其妙地拒绝工作.解决这个问题的最佳方法是什么?
在Console应用程序中,我正在使用Log4Net,在Main方法中我得到了logger对象.现在,我想通过让所有类继承自具有ILog属性并且应该由Property Injection而不是Constructor Injection设置的BaseClass来使这个日志对象在我的所有类中可用.
我正在使用AutoFac IoC容器,如何将我的日志对象注入我的每个类的Log属性?
实现这一目标的最佳/最简单方法是什么?
有没有办法自动解决类型?
以下是我的测试应用程序:
namespace ConsoleApplication1
{
class Program
{
static ILog Log;
static IContainer Container;
static void Main(string[] args)
{
InitializeLogger();
InitializeAutoFac();
// the below works but could it be done automatically (without specifying the name of each class)?
Product.Log = Container.Resolve<ILog>();
// tried below but didn't inject ILog object into the Product
Container.Resolve<Product>();
RunTest();
Console.ReadLine();
}
private static void RunTest()
{
var product = new Product();
product.Do();
}
private static void InitializeAutoFac()
{
var …
Run Code Online (Sandbox Code Playgroud) 我的MVC 5项目中有一个基本控制器,它实现了一些共享功能.此功能需要一些依赖项.我正在使用Unity 3将这些实现注入到我的控制器中,这种模式运行良好,直到我将控制器切换为从该基本控制器继承.现在我遇到了以下问题:
public class BaseController : Controller
{
private readonly IService _service;
public BaseController(IService service)
{
_service = service;
}
}
public class ChildController : BaseController
{
private readonly IDifferentService _differentService;
public ChildController(IDifferentService differentService)
{
_differentService = differentService;
}
}
Run Code Online (Sandbox Code Playgroud)
这是可以理解的抛出错误'BaseController' does not contain a constructor that takes 0 arguments
.Unity没有解析BaseController的构造,因此它无法将依赖项注入其中.我看到了解决这个问题的两种明显方法:
1.)显式调用BaseController ctor并让每个ChildController ctor注入BaseController的依赖项
public class ChildController : BaseController
{
private readonly IDifferentService _differentService;
public ChildController(IDifferentService differentService,
IService baseService)
: base(baseService)
{
_differentService = differentService;
}
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种方法有几个原因:一,因为ChildControllers没有使用额外的依赖项(因此它会导致子控制器中的构造函数膨胀),更重要的是,如果我更改了构造函数签名基本控制器,我必须更改每个子控制器的构造函数签名. …
c# asp.net-mvc dependency-injection inversion-of-control unity-container
我有一个Web应用程序,其中注册了许多组件.LifestylePerWebRequest()
,现在我决定实现Quartz.NET,一个.NET作业调度库,它在不同的线程中执行,而不是Request线程.
因此,HttpContext.Current
收益率null
.IDbConnection
到目前为止,我的服务,存储库和实例已被使用,.LifestylePerWebRequest()
因为它使得在请求结束时更容易处理它们.
现在我想在两种情况下使用这些组件,在Web请求期间我希望它们不受影响,在非请求上下文中我希望它们使用不同的生活方式,我想我可以自己处理处理,但我该怎么办关于它根据当前背景选择组件的生活方式?
目前我注册服务(例如),像这样:
container.Register(
AllTypes
.FromAssemblyContaining<EmailService>()
.Where(t => t.Name.EndsWith("Service"))
.WithService.Select(IoC.SelectByInterfaceConvention)
.LifestylePerWebRequest()
);
Run Code Online (Sandbox Code Playgroud)
我想我应该使用某种扩展方法,但我只是看不到它..
c# castle-windsor inversion-of-control quartz.net asp.net-mvc-3
c# ×7
asp.net ×2
asp.net-mvc ×2
.net ×1
autofac ×1
autowired ×1
generics ×1
instance ×1
java ×1
log4net ×1
ninject ×1
plugins ×1
quartz.net ×1
spring-mvc ×1
static ×1