Windsor 依赖注入
2021-05-17 14:30
标签:tle install not ike static ack 接口 构造 handle Windsor 学习 1.安装nuget Install-Package Castle.Windsor 安装后会多这两个引用 2.hello world 2.手动注入 这里可以看到CI1 构造函数需要的接口都被注入进来.调用成功 4.在asp.net mvc 中使用 5 高级用法 1.注入泛型 2.设置默认Component 3.指定依赖. 当一个类依赖的是接口,但是接口的实例有多个,可用这个处理 4. 类多继承接口 Windsor 依赖注入 标签:tle install not ike static ack 接口 构造 handle 原文地址:https://www.cnblogs.com/student-note/p/11768689.html class Program
{
static void Main(string[] args)
{
// 应用开始...
var container = new WindsorContainer();
// 使用windstorinstallers从执行程序集添加和配置所有组件
container.Install(FromAssembly.This());
// 实例化依赖
var king = container.Resolve
///
public interface I1
{
void Show1();
}
public interface I2
{
void Show2();
}
public class C1 : I1
{
public void Show1()
{
Console.WriteLine("C1");
}
}
public class C2 : I2
{
public void Show2()
{
Console.WriteLine("C2");
}
}
public class CI1
{
public CI1(I1 i1, I2 i2)
{
i1.Show1();
i2.Show2();
}
}
public class MvcApplication : System.Web.HttpApplication
{
private static WindsorContainer contariner;
protected void Application_End()
{
contariner.Dispose();
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
contariner = new WindsorContainer();
contariner.Install(FromAssembly.This());
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(contariner.Kernel));
}
}
public class CustomeWindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly().BasedOn
private static void SetGeneric()
{
var container = new WindsorContainer();
container.Register(
Component.For(typeof(IRespority))
.ImplementedBy(typeof(BaseRespority))
);
var rp = container.Resolve
private static void Repeat() {
var container = new WindsorContainer();
container.Register(
Component.For
///
var container = new WindsorContainer();
container.Register(
Component.For