Activator.CreateInstance still being used in DataPortal

Activator.CreateInstance still being used in DataPortal

Old forum URL: forums.lhotka.net/forums/t/5796.aspx


vdhant posted on Wednesday, November 12, 2008

Hi guys
I noticed that Activator.CreateInstance is still being used within the DataPortal. Previously this wouldn't have been that bigger deal as the proxies where being cached, but now the system will be creating new instances on every request. Hence, The Activator.CreateInstance in GetDataPortalProxy should probably be replaced with MethodCaller.CreateInstance(_proxyType).
Cheers
Anthony

Edit:
I think i have just had the best idea of a change that could happen here which would really help.


private static IDataProxyFactory _ProxyFactory;

private static IDataProxy DetermineDataPortalProxyAlt(bool forceLocal)
{
    if (DataPortalClient.IsInDesignMode)
    {
        return new DataPortalClient.DesignTimeProxy();
    }
    else
    {
        if (forceLocal)
            return new DataPortalClient.LocalProxy();
        else
        {
            if (!String.IsNullOrEmpty(ApplicationContext.DataPortalProxyFactory) && _ProxyFactory == null)
            {
                Type proxyFactoryType = Type.GetType(ApplicationContext.DataPortalProxyFactory, true, true);
                _ProxyFactory = (IDataProxyFactory)MethodCaller.CreateInstance(proxyFactoryType);
            }


            if (_ProxyFactory != null)
            {
                return portal = _ProxyFactory.Create();
            }
            else
            {

                Csla.DataPortalClient.IDataPortalProxy portal;
                string proxyTypeName = ApplicationContext.DataPortalProxy;
                if (proxyTypeName == "Local")
                    portal = new LocalProxy();
                else
                {
                    if (DataPortalClient._ProxyType == null)
                        DataPortalClient._ProxyType = Type.GetType(proxyTypeName, true, true);
                    portal = (IDataProxy)MethodCaller.CreateInstance(DataPortalClient._ProxyType);
                }
                return portal;
            }
        }
    }
}

This kills about 10 birds with 1 stone. Basically as you can see I have introduced MethodCaller.CreateInstance to help boost performance. But that got me thinking that this is being be used a lot now that the proxy is being called every time request is made. So what i was thinking was is there a way of statically doing this (for people that are only ever using the 1 proxy). So what I have introduced is a factory that is created once, and then is reused from then on. This would mean that factory could be created (if one so desirers) which creates the proxy instances statically. But when thinking about it, one might only be splitting hairs in terms of performance as MethodCaller is used quite a few time in the DataPortal.

But then I came across this http://forums.lhotka.net/forums/thread/28189.aspx just now and this would solve his problem exactly. This would allow developers to create there own version of IDataProxyFactory and register it in the web config.

Note, i didn't convert over the current code in the else block there, but it to could use IDataProxyFactory and be set by default if no other Proxy factory is found.

But more than that, I came across this http://forums.lhotka.net/forums/thread/28189.aspx just now and this would solve his problem exactly. This would allow developers to create there own version of IDataProxyFactory and register it in the web config.

Note, i didn't convert over the current code in the else block there, but it to could use IDataProxyFactoryand be set by default if no other Proxy factory is found.

RockfordLhotka replied on Thursday, November 13, 2008

Good idea - added to wish list

http://www.lhotka.net/cslabugs/edit_bug.aspx?id=228

Copyright (c) Marimer LLC