我使用Autofac进行DI和MVVM。我有一个注册的全球服务:
@PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
@PreRemove Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
@PostPersist Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed.
@PostRemove Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation.
@PreUpdate Executed before the database UPDATE operation.
@PostUpdate Executed after the database UPDATE operation.
@PostLoad Executed after an entity has been loaded into the current persistence context or an entity has been refreshed.
我还使用Autofac.Extras.NLog实现了明显的日志记录目的:
builder.RegisterType<MyGlobalService>().As<IGlobalService>().SingleInstance();
这样我可以在任何一个ViewModel中获取该服务和记录器的实例:
builder.RegisterModule<NLogModule>();
效果很好。 现在我还希望在MyGlobalService中提供日志记录支持。但这一次,我想使用Property Injection,而不是Constructor Injection。
问题是:我应该以不同的方式注册这样的规则吗?
使用示例:
namespace MyNameSpace.ViewModels
{
class MyViewModel : BindableBase
{
readonly IGlobalService _globalService;
readonly ILogger _logger;
public MyViewModel (IGlobalService globalService, ILogger logger)
{
_globalService = globalService;
_logger = logger;
}}}(...)
编辑:在下面回答。
答案 0 :(得分:2)
Property Injection无法开箱即用,您需要设置要使用属性注入的类,所以
builder.RegisterType<MyGlobalService>().As<IGlobalService>().PropertiesAutowired().SingleInstance();
应该做的伎俩。或者您可以使用OnActivating
终身事件手动执行此操作。