Caliburn笔记-依赖注入容器(wpf框架)
2009-12-30 11:49
1011 查看
参考与此http://caliburn.codeplex.com/wikipage?title=Auto-Registering%20Components&referringTitle=Documentation
此为基础,看了没用,不看不行…了解下注册流程.
注册组件,差不多离不开这几种模式
手动注册
元数据标签注册
外部dll加载注册
1.内置服务则用手动注册.
2.挂元数据标签,如下[PerRequest(typeof(IHomePresenter))]
public class HomePresenter : Presenter, IHomePresenter
{
}
以前不是推荐此种做法的,标签会产生框架耦合,但框架用都用了,内置demo使用此方法最多,之前用的是手动注册,框架会去dll中寻找挂此标签的对象然后自动注册.
3.外部dll加载
重写CaliburnApplication的SelectAssemblies方法protected override System.Reflection.Assembly[] SelectAssemblies()
{
return new[] { Assembly.GetEntryAssembly(),typeof(Caliburn.WPF.ApplicationFramework.Bind).Assembly};
}
private void InspectAssembly(Assembly assembly, ICollection<ComponentInfo> componentList, ICollection<Type> configs)
{
var types = assembly.GetExportedTypes();
foreach (var type in types)
{
foreach (var attribute in type.GetCustomAttributes(true).OfType<RegisterAttribute>())
componentList.Add(attribute.GetComponentInfo(type));
}
foreach (var type in types)
{
if(_configType.IsAssignableFrom(type) && !type.IsAbstract)
configs.Add(type);
}
}
总的来说,我们只要加载dll,挂上标签就可以自动注册了
内置服务+自定义服务注册好以后,接下来的任务就是注册实例.即组件的生命周期状况.内置都为Singleton/// <summary>
/// The lifetime of a Caliburn component.
/// </summary>
public enum ComponentLifetime
{
/// <summary>
/// One instance per application.
/// </summary>
Singleton,
/// <summary>
/// A new instance is created on each request.
/// </summary>
PerRequest,
/// <summary>
/// A new instance is created per custom lifetime rules.
/// </summary>
Custom
}
可以通过重写ConfigureWith方法,使用第三方容器来注册服务,当然其内置也提供了一个较为简单的容器
新版本可能会更新,所以不去研究它了
此为基础,看了没用,不看不行…了解下注册流程.
注册组件,差不多离不开这几种模式
手动注册
元数据标签注册
外部dll加载注册
1.内置服务则用手动注册.
2.挂元数据标签,如下[PerRequest(typeof(IHomePresenter))]
public class HomePresenter : Presenter, IHomePresenter
{
}
以前不是推荐此种做法的,标签会产生框架耦合,但框架用都用了,内置demo使用此方法最多,之前用的是手动注册,框架会去dll中寻找挂此标签的对象然后自动注册.
3.外部dll加载
重写CaliburnApplication的SelectAssemblies方法protected override System.Reflection.Assembly[] SelectAssemblies()
{
return new[] { Assembly.GetEntryAssembly(),typeof(Caliburn.WPF.ApplicationFramework.Bind).Assembly};
}
private void InspectAssembly(Assembly assembly, ICollection<ComponentInfo> componentList, ICollection<Type> configs)
{
var types = assembly.GetExportedTypes();
foreach (var type in types)
{
foreach (var attribute in type.GetCustomAttributes(true).OfType<RegisterAttribute>())
componentList.Add(attribute.GetComponentInfo(type));
}
foreach (var type in types)
{
if(_configType.IsAssignableFrom(type) && !type.IsAbstract)
configs.Add(type);
}
}
总的来说,我们只要加载dll,挂上标签就可以自动注册了
内置服务+自定义服务注册好以后,接下来的任务就是注册实例.即组件的生命周期状况.内置都为Singleton/// <summary>
/// The lifetime of a Caliburn component.
/// </summary>
public enum ComponentLifetime
{
/// <summary>
/// One instance per application.
/// </summary>
Singleton,
/// <summary>
/// A new instance is created on each request.
/// </summary>
PerRequest,
/// <summary>
/// A new instance is created per custom lifetime rules.
/// </summary>
Custom
}
可以通过重写ConfigureWith方法,使用第三方容器来注册服务,当然其内置也提供了一个较为简单的容器
新版本可能会更新,所以不去研究它了
相关文章推荐
- Caliburn笔记-依赖注入容器(wpf框架)
- [Prism]Composite Application Guidance for WPF(5)——依赖注入容器
- Caliburn笔记-Action的创建(wpf框架)
- Caliburn笔记-元数据(Metadata)管理(wpf框架)
- Caliburn笔记-Presenter与View(视图策略)(wpf框架)
- Caliburn笔记-基本Command(wpf框架)
- Caliburn笔记-Presenter与View(视图策略)(wpf框架)
- spring技术内幕笔记:IoC容器的依赖注入
- Caliburn笔记-方法(IMethod)的创建(wpf框架)
- Caliburn笔记-Presenter(wpf框架)
- Caliburn笔记-过滤器管理(IFilterManager)(wpf框架)
- 1.2.2 依赖注入容器/框架 Spring
- WPF PRISM开发入门二(Unity依赖注入容器使用)
- (7)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 利用Polly+AOP+依赖注入封装的降级框架
- Yii框架依赖注入DI容器
- Caliburn笔记-Presenter(wpf框架)
- Caliburn笔记-Action简写(wpf框架)
- YII框架的依赖注入容器与服务定位器简述
- Slim研读笔记五之依赖注入容器(上)
- Caliburn笔记(1)(wpf框架)