C# SimpleInjector和[Export attibute]
在WPF应用程序中,我使用Caliburn Micro for MVVM模式。。。我想尝试另一个IoC,并希望重用大部分现有代码 在我的应用程序中,我通过属性将所有可导出类定义为C# SimpleInjector和[Export attibute],c#,wpf,mvvm,caliburn.micro,simple-injector,C#,Wpf,Mvvm,Caliburn.micro,Simple Injector,在WPF应用程序中,我使用Caliburn Micro for MVVM模式。。。我想尝试另一个IoC,并希望重用大部分现有代码 在我的应用程序中,我通过属性将所有可导出类定义为 [Export(typeof(ITaggable))] [Export(typeof(CorporateActionViewModel))] [Export(typeof(IScreen))] public class CorporateActionViewModel :... 如何在不手动操作的情况下注册它们 Co
[Export(typeof(ITaggable))]
[Export(typeof(CorporateActionViewModel))]
[Export(typeof(IScreen))]
public class CorporateActionViewModel :...
如何在不手动操作的情况下注册它们
ContainerInstance.Register<ITaggable, CorporateActionViewModel>();
ContainerInstance.Register<IScreen, CorporateActionViewModel>();
ContainerInstance.Register<CorporateActionViewModel, CorporateActionViewModel>();
ContainerInstance.Register();
ContainerInstance.Register();
ContainerInstance.Register();
另一个问题是关于延迟初始化。。。我读过如何注册懒惰。。。但是我必须调用Container.Verify()吗
谢谢此查询将查找标有
ExportAttribute
private IEnumerable<Type> GetExportedTypes()
{
return from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where Attribute.IsDefined(type, typeof(ExportAttribute))
select type;
}
private IEnumerable<Type> GetServicesFromType(Type type)
{
return from attribute in Attribute
.GetCustomAttributes(type, typeof(ExportAttribute))
select ((ExportAttribute)attribute).ContractType;
}
这些查询可以这样使用
var container = new Container();
foreach(var type in GetExportedTypes())
{
foreach (var service in GetServicesFromType(type))
{
container.Register(service, type);
}
}
container.Verify();
关于
Verify()
的问题?调用Verify
从来都不是强制性的,但始终建议这样做。有帮助。此查询将查找标有ExportAttribute
private IEnumerable<Type> GetExportedTypes()
{
return from assembly in AppDomain.CurrentDomain.GetAssemblies()
from type in assembly.GetTypes()
where Attribute.IsDefined(type, typeof(ExportAttribute))
select type;
}
private IEnumerable<Type> GetServicesFromType(Type type)
{
return from attribute in Attribute
.GetCustomAttributes(type, typeof(ExportAttribute))
select ((ExportAttribute)attribute).ContractType;
}
这些查询可以这样使用
var container = new Container();
foreach(var type in GetExportedTypes())
{
foreach (var service in GetServicesFromType(type))
{
container.Register(service, type);
}
}
container.Verify();
关于
Verify()
的问题?调用Verify
从来都不是强制性的,但始终建议这样做。有帮助。如果您显式注册了惰性和正常版本的注册,那么对象图仍然是完全可验证的。请查看此注册:
container.Register<ITaggable, CorporateActionViewModel>();
container.Register<Lazy<ITaggable>>(
() => new Lazy<ITaggable>(container.GetInstance<ITaggable>));
container.Verify();
这里的Lazy
注册使用GetInstance
作为工厂方法,但是CorporateActionViewModel
没有显式注册。在验证过程中,Simple Injector将创建Lazy
,这显然会成功,但它不会自动为您调用Lazy.Value
属性(这是故意的,因为您可能有推迟创建对象图的原因)
但请重新考虑在整个代码库中注入惰性的策略。这是一个坏主意和坏做法。请阅读和以了解更多信息。如果您明确注册了惰性和正常版本的注册,则对象图仍将是完全可验证的。请查看此注册:
container.Register<ITaggable, CorporateActionViewModel>();
container.Register<Lazy<ITaggable>>(
() => new Lazy<ITaggable>(container.GetInstance<ITaggable>));
container.Verify();
这里的Lazy
注册使用GetInstance
作为工厂方法,但是CorporateActionViewModel
没有显式注册。在验证过程中,Simple Injector将创建Lazy
,这显然会成功,但它不会自动为您调用Lazy.Value
属性(这是故意的,因为您可能有推迟创建对象图的原因)
但请重新考虑在整个代码库中注入惰性的策略。这是一个坏主意和坏做法。请阅读和以获取更多信息。使用
ExportAttribute
而不考虑完整的源代码,只是为了注册所有类型,听起来似乎违反了依赖项反转原则。这本身就有问题,但肯定有几个缺点
Simple Injector不需要使用属性来查找要注册的类。它实际上是
简单的喷油器组
如果遵循viewmodels(和相应视图)的实体原则,则可以轻松(很容易…取决于当前的设计…)删除该属性
如果我们采用一个典型的LoB应用程序,其中数据库中有一组实体,我们可以将viewmodel/视图设计拆分为viewmodels将实现的这些通用接口(当然,一次一个):
//查看实体的典型datagrid视图,例如添加、编辑和删除按钮
i工作空间;
//对于一个实体(包括可能的子实体)的典型编辑视图
知识性;
//用于从编辑视图中选择特定外部实体类型
//e、 g.编辑订单并需要指定客户
鱼卵性
使用这些,我们将得到非常具体的视图模型,这些模型是实心的,如果您愿意,仍然可以为用户合成一个非常大的复杂视图
您可以很容易地使用以下简单的注入器注册这些类型:
container.RegisterManyForOpenGeneric(
typeof(IChooseEntityViewModel<>), Assembly.GetExecutingAssembly());
container.RegisterManyForOpenGeneric(
typeof(IChooseEntityViewModel),Assembly.getExecutionGassembly();
作为此设计的一个额外功能,您可以使用一个或多个装饰器包装viewmodels,这些装饰器可用于一些真正的MVVM内容,如查找视图、将其绑定到viewmodel以及在窗口/页面中显示视图等。
如果您想了解更多关于装饰器的信息,结合simple injector,您可以找到一些不错的文章(不要忘记各种链接)。使用
ExportAttribute
考虑完整的源代码,只注册所有类型,这听起来违反了依赖项反转原则。这本身就有问题,但肯定有几个缺点
Simple Injector不需要使用属性来查找要注册的类。它实际上是
简单的喷油器组
如果遵循viewmodels(和相应视图)的实体原则,则可以轻松(很容易…取决于当前的设计…)删除该属性
如果我们采用一个典型的LoB应用程序,其中数据库中有一组实体,我们可以将viewmodel/视图设计拆分为viewmodels将实现的这些通用接口(当然,一次一个):
//查看实体的典型datagrid视图,例如添加、编辑和删除按钮
i工作空间;
//对于一个实体(包括可能的子实体)的典型编辑视图
知识性;
//用于从编辑视图中选择特定外部实体类型
//e、 g.编辑订单并需要指定客户
鱼卵性
使用这些,我们将得到非常具体的视图模型,这些模型是实体的,并且仍然可以为用户合成一个非常大的复杂视图
ContainerInstance.Register<ISharedModuleObject, SharedModuleObject>();
ContainerInstance.RegisterSingle<ISharedModuleObject, SharedModuleObject>();