我有一个“主题库”来存储主题对象
主题
Public Interface ITheme
Property Name As String
Property Url As String
End Interface
主题库
Public Interface IThemeRepository
Property Themes As IList(Of ITheme)
Sub Add(theme As ITheme)
End Interface
在我的应用程序设置期间,我想向存储库添加一些主题,然后我希望任何东西都能够作为一种内存数据库访问存储库。这意味着存储库需要具有单例生活方式。即使使用以下代码,也总是会实例化 ThemeRepository 的新实例:
存储库安装程序
Public Class RepositoriesInstaller
Implements IWindsorInstaller
Public Sub Install(container As Castle.Windsor.IWindsorContainer,
store As Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore) _
Implements IWindsorInstaller.Install
container.Register(Classes.FromAssembly(Assemblies.Infrastructure) _
.BasedOn(Of IThemeRepository) _
.WithServiceDefaultInterfaces _
.LifestyleSingleton)
End Sub
End Class
主题库
Public Class ThemeRepository
Implements IThemeRepository
Public Sub New()
Themes = New List(Of ITheme)
End Sub
Public Sub Add(theme As ITheme) Implements IThemeRepository.Add
Themes.Add(theme)
End Sub
Public Property Themes As IList(Of ITheme) Implements IThemeRepository.Themes
End Class
ThemeRepository
服务总是能成功找到,但它不是单例的。一个新的实例ThemeRepository
总是被解析。即New()
总是被调用ThemeRepository
。这是怎么回事?
(答案中可接受 C# 或 VB.NET 代码)