目前我的代码具有以下属性:
Private _queueRetriever As Services.IQueueRetriever Public ReadOnly Property QueueRetriever As Services.IQueueRetriever Get If _queueRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueRetriever = RemotingFactory.CreateProxy(Of Services.IQueueRetriever)(Options.DevLocal) End If Return _queueRetriever End Get End Property Private _queueCountRetriever As Services.IQueueCounter Public ReadOnly Property QueueCounter As Services.IQueueCounter Get If _queueCountRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueCountRetriever = RemotingFactory.CreateProxy(Of Services.IQueueCounter)(Options.DevLocal) End If Return _queueCountRetriever End Get End Property如您所见,两个属性的代码基本相同。 只有类型不同。 我在泛型方法方面不是很有经验,但我觉得我应该能够使用泛型使这段代码更干 。 可以这样做,还是这不是通用方法的工作?
编辑
在Marc的回答基础上,我想出了以下内容:
Protected Services As New Dictionary(Of Type, Object) Protected Function CreateService(Of T)() As T If Not Services.ContainsKey(GetType(T)) Then RemotingSetup.RegisterHttpBinaryChannel() Services.Add(GetType(T), RemotingFactory.CreateProxy(Of T)(Options.DevLocal)) End If Return DirectCast(Services(GetType(T)), T) End Function当然,这可以像这样使用:
Me.CreateService(Of IService).Foo()Currently my code has the following properties:
Private _queueRetriever As Services.IQueueRetriever Public ReadOnly Property QueueRetriever As Services.IQueueRetriever Get If _queueRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueRetriever = RemotingFactory.CreateProxy(Of Services.IQueueRetriever)(Options.DevLocal) End If Return _queueRetriever End Get End Property Private _queueCountRetriever As Services.IQueueCounter Public ReadOnly Property QueueCounter As Services.IQueueCounter Get If _queueCountRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueCountRetriever = RemotingFactory.CreateProxy(Of Services.IQueueCounter)(Options.DevLocal) End If Return _queueCountRetriever End Get End PropertyAs you can see, the code is essentially the same for both properties. Only the types are different. I'm not very experienced in generic methods, but I feel like I should be able to make this code more DRY using generics. Can it be done or is this not a job for generic methods?
EDIT
Building on Marc's answer, I came up with the following:
Protected Services As New Dictionary(Of Type, Object) Protected Function CreateService(Of T)() As T If Not Services.ContainsKey(GetType(T)) Then RemotingSetup.RegisterHttpBinaryChannel() Services.Add(GetType(T), RemotingFactory.CreateProxy(Of T)(Options.DevLocal)) End If Return DirectCast(Services(GetType(T)), T) End FunctionWhich, of course, can be used like so:
Me.CreateService(Of IService).Foo()最满意答案
你可以做(并且转而使用C#道歉,但我的VB技能在这里缺乏细微之处 - 我确信它在VB中工作得很好)
private T GetProxy<T>(ref T field) where T : class { if(field == null) { RemotingSetup.RegisterHttpBinaryChannel(); field = RemotingFactory.CreateProxy<T>(Options.DevLocal); } return field; }有:
private Services.IQueueCounter queueCounter; public Services.IQueueCounter QueueCounter { get { return GetProxy(ref queueCounter); } } private Services.IQueueRetriever queueRetriever; public Services.IQueueRetriever QueueRetriever { get { return GetProxy(ref queueRetriever); } }You could do (and apologies for switching into C#, but my VB skills lack the subtlety here - I'm sure it works fine in VB too though)
private T GetProxy<T>(ref T field) where T : class { if(field == null) { RemotingSetup.RegisterHttpBinaryChannel(); field = RemotingFactory.CreateProxy<T>(Options.DevLocal); } return field; }with:
private Services.IQueueCounter queueCounter; public Services.IQueueCounter QueueCounter { get { return GetProxy(ref queueCounter); } } private Services.IQueueRetriever queueRetriever; public Services.IQueueRetriever QueueRetriever { get { return GetProxy(ref queueRetriever); } }更多推荐
发布评论