sharing about .NET and technology RSS 2.0
 Sunday, August 21, 2005

RemotingHelper is a little helper class by Ingo Rammer that enables you to use interfaces to access remote objects instead of the implementation. In .NET 2.0 there are a lot of new features, and one of them are generics. Especially with the RemotingHelper we deal with types and with the GetObject method we can use generics to parameterize the method by type.

In .NET 1.1 we need to write something like

ICustomerService customerService = RemotingHelper.GetObject(typeof(ICustomerService)) as ICustomerService;

when using generics in .NET 2.0 we can simply write

ICustomerService customerService = RemotingHelper.GetObject<ICustomerService>();

No need to to cast and no typeof operator!

Below you find a version of the RemotingHelper using generics, or you can download the RemotingHelper.zip

using System;
using System.Collections.Generic;
using System.Runtime.Remoting;

public class RemotingHelper
{
   private static bool isInit;
   private static IDictionary<Type, WellKnownClientTypeEntry> wellKnownTypes;

   public static T GetObject<T>()
   {
      if (!isInit)
         InitTypeCache();

      WellKnownClientTypeEntry entry = wellKnownTypes[typeof(T)];

      if (entry == null)
      {
         throw new RemotingException("Type not found!");
      }

      return (T)Activator.GetObject(entry.ObjectType, entry.ObjectUrl);
   }

   public static void InitTypeCache()
   {
      isInit = true;
      wellKnownTypes = new Dictionary<Type, WellKnownClientTypeEntry>();

      foreach (WellKnownClientTypeEntry entry in RemotingConfiguration.GetRegisteredWellKnownClientTypes())
      {
         if (entry.ObjectType == null)
         {
            throw new RemotingException("A configured type could not be found. Please check spelling");
         }

         wellKnownTypes.Add(entry.ObjectType, entry);
      }
   }
}

Sunday, August 21, 2005 9:57:40 PM (Romance Standard Time, UTC+01:00)  #    Comments [2] -

Thursday, February 23, 2006 10:29:12 PM (Romance Standard Time, UTC+01:00)
Hi

Thanks a lot for this great example !!

I would like to write exactly the same RemotingHelper class using generics, but in C++ !!!

As for now I don't manage to do that....

Can you help me ?

Regards,
David.
David
Friday, March 24, 2006 7:03:19 PM (Romance Standard Time, UTC+01:00)
This was very usefull, thanks.

ka
Kamran Amin
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Navigation
Archive
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009
Christoph De Baene
Sign In
Statistics
Total Posts: 154
This Year: 0
This Month: 0
This Week: 0
Comments: 166
All Content © 2009, Christoph De Baene
DasBlog theme 'Business' created by Christoph De Baene (delarou)