2011-05-14 89 views
2

好大家好多的網絡服務,從而多單

我有一個項目,倍數Web服務,所以我創造了性能的各種單例類的想法。現在我想創建一個單獨的類,並且有我的web服務

例如

public static WebServiceMaster 
{ 
    internal ServiceX WebX; 
    internal ServiceY WebY; 
    ...... 
    public static WEbServiceMaster GetInstance() 
    ..... 
} 

什麼想想的實例? 是那麼糟糕?

好了,終於做到這一點。我知道這是不完美的

Public NotInheritable Class ServiceProxySingleton 

Private _services As IDictionary(Of ProxyServicesEnum, IServiceDispatcher) = New Dictionary(Of ProxyServicesEnum, IServiceDispatcher) 
Private _dbRepository As IDACommon 

Private Sub New() 
    _dbRepository = New DACommon() 
    LoadServices() 
End Sub 

Private Sub LoadServices() 
    _services.Add(ProxyServicesEnum.eActivity, New ActivityServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eAvailability, New AvailabilityServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eBrochure, New BrochureServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eInformation, New InformationServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eMeetingRoom, New MeetingRoomServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eMembership, New MembershipServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eName, New NameServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eReservation, New ReservationServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eResvAdvanced, New ResvAdvancedServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eSecurity, New SecurityServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.eStayHistory, New StayHistoryServiceImp(_dbRepository)) 
    _services.Add(ProxyServicesEnum.ePostXml, New PostXmlServiceImp(_dbRepository, ConfigurationServiceSingleton.GetInstance.GetPostXmlConfig)) 
    _services.Add(ProxyServicesEnum.eOxiHttp, New OxiServiceImp(_dbRepository)) 
End Sub 

Public ReadOnly Property Service(ByVal serviceEnum As ProxyServicesEnum) As Object 
    Get 
    If _services.ContainsKey(serviceEnum) Then 
     Return _services.Item(serviceEnum) 
    End If 
    Return Nothing 
    End Get 
End Property 

Public ReadOnly Property GetMeta(ByVal serviceEnum As ProxyServicesEnum) As IDictionary(Of String, MethodIdentityAttribute) 
    Get 
    If _services.ContainsKey(serviceEnum) Then 
     Return _services.Item(serviceEnum).MetaInfo 
    End If 
    Return Nothing 
    End Get 
End Property 

Public Shared Function GetInstance() As ServiceProxySingleton 
    Return NestedPrWireService._instance 
End Function 

Class NestedPrWireService 
    Friend Shared ReadOnly _instance As ServiceProxySingleton = New ServiceProxySingleton() 
    Shared Sub New() 
    End Sub 
End Class 

End Class 

意見和批評,歡迎

+0

看看multiton,也許這就是你要找的。 http://en.wikipedia.org/wiki/Multiton_pattern#C.23 @Carlos和btw singelton不是一種表演模式。也許你應該閱讀有關它也 – sra 2011-05-14 06:10:29

回答

0

很好的方法是使用依賴注入。例如Unity

+0

謝謝,我已經在那裏此刻供應的依賴性,多數民衆贊成助手幫助創建的類的門面一些如何DAO,新ServiceXDAO(WebServiceMaster.ServiceXInterface),新ServiceYDAO(WebServiceMaster.ServiceYInterface)。也許一個好方法是使用ServiceLocator,但我不知道是否有創建單例服務的選項 – 2011-05-14 14:02:00

+0

Unity可以爲你創建這些對象:你可以設置你想要的生命週期(每個實例,單例或定義你的)。這個功能你可以在盒子裏找到。爲什麼要重塑自行車?所以,我不知道你爲什麼要投這個答案。例如,_container.Resolve ()將返回新的MyService或已創建的MyService實例,具體取決於您如何配置Unity。 – VikciaR 2011-05-16 10:41:39

+0

爲什麼要重塑自行車?不完全是,在這種情況下,我使用的是框架2.0和一個公司extern,並且他們對DI和IoC都是封閉的。真的,我看起來簡單但實用,思考應用程序的性能。感謝您的意見 – 2011-05-20 14:54:10