CodeSmith Community
Your Code. Your Way. Faster!

Runtime Connection Switching

Latest post 07-21-2008 12:15 PM by blake05. 20 replies.
  • 04-02-2006 5:34 PM

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Runtime Connection Switching

    The ability to switch connections at runtime has now been integrated into the .netTiers templates for .net 2.0.  Multiple connections can now be used whether they are declared in the *.config file or generated dynamically in code.

    The original syntax using the default configured connection:

    TList<Customers> list = DataRepository.CustomersProvider.GetAll();

    New syntax using a declared connection string:

    TList<Customers> list = DataRepository.Connections["NorthwindConnectionString2"].Provider.CustomersProvider.GetAll();

    New syntax using a dynamic connection string:

    DataRepository.AddConnection("DynamicConnectionString", "Data Source=(local);Initial Catalog=Northwind;Integrated Security=true;");

    TList<Customers> list = DataRepository.Connections["DynamicConnectionString"].Provider.CustomersProvider.GetAll();

    More examples to come...

    Regards,
    Bobby

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 130
  • 04-06-2006 10:46 AM In reply to

    Re: Runtime Connection Switching

    Bobby:

    I don't see the dynamic connection string functionality in the .NET Tiers version 0.9.2 templates. I would expect to find it in DataRepository.cst - can you direct me to the right place?

    Thanks.

    Mike
    • Post Points: 35
  • 04-07-2006 1:03 PM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Runtime Connection Switching

    Hey Mike,

    Unforunately, I only made the changes to the CTP templates for .net 2.0.  I have just recently started making changes to the templates and really only have experience with the CTP.  I will see if the same changes can be integrated easily into the 0.9.2 templates.

    Thanks,
    Bobby

    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 35
  • 04-19-2006 4:37 AM In reply to

    • daryl_tt
    • Top 500 Contributor
    • Joined on 04-07-2006
    • Posts 10
    • Points 260

    Re: Runtime Connection Switching

    Ah man... and I was just getting so happy when I read that it had been implemented. Wink [;)]

    Do you think it might be a while for this to be done with 0.9.2?  I just want to know how long so I can  decide whether to put a 'work-around' in place.

    Thanks
    Daryl

    • Post Points: 5
  • 04-22-2006 11:30 AM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Runtime Connection Switching

    Here is a link to a thread that shows how to set up the *.config file to support the multiple sections and connections:

    http://community.codesmithtools.com/forums/thread/12847.aspx


    Thanks,
    Bobby


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 5
  • 08-25-2006 2:39 PM In reply to

    Re: Runtime Connection Switching

    I am interested in dynamic connection string switching, but would like to use the service component layer, NOT go down to the DAL.  Can this be done with the curent templates (2.0 nightly build 8/9) or do I have to hack into the templates to provide this functionality?  I am trying to keep my devs out of direct DAL access if possible.  Thanks.

    Tom
    • Post Points: 35
  • 08-25-2006 3:17 PM In reply to

    Re: Runtime Connection Switching

    Hi, You would use the ConnectionScope Object.

    Something like this:

    //Intialize dynamic connection a single time in your app
    //Set both of them only once, and make a data call.
    //This will add the key and the connection to the DataRepository.Connections collection
    ConnectionScope.Current.ConnectionStringKey = "MyDynamicConnectionStringKey";
    ConnectionScope.Current.DynamicConnectionString = "server=.;database=Northwind;Integrated Security=true;";

    MyService service = new MyService();
    service.GetAll();


    //// Page/Form Specific Call

    //So long as the key and connection string have been set in that collection,
    // you only have to enter the ConnectionStringKey for future dynamic calls that don't use the default provider.
    ConnectionScope.Current.
    ConnectionStringKey = "MyDynamicConnectionStringKey";
    MyService service = new MyService();
    service.GetAll();




    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 09-02-2006 10:38 PM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    Hi,goofsr

           I added "Page_init()" in the code just like this:

     

             protected void Page_Init(object sender, EventArgs e)
        {
                  DataRepository.AddConnection("test", "Data Source=(local)\normal;Initial Catalog=zs;User ID=sa;Password=sa");
     
            BasZoneDataSource.Provider = DataRepository.Connections["test"].Provider.BasZoneProvider;
                                          
        }

    "BasZoneDataSource" is a datasourcecontrol and it's the datasource of one gridview.

    But this can't work,  BasZoneDataSource's GetAll() or GetPaged() needs TransactionManager.

     TransactionManager created by "mgr = DataRepository.Provider.CreateTransaction();" in the  EntityTransactionModule.cs.

    So TrasactionManager still use default Provider, default ConnectionsString.
    How can i change the TransactionManager's ConnectionString too?

    • Post Points: 35
  • 09-03-2006 11:35 AM In reply to

    • bdiaz
    • Top 10 Contributor
    • Joined on 02-20-2006
    • Houston, TX
    • Posts 504
    • Points 15,290

    Re: Runtime Connection Switching

    Add the following to your Page_Init as well:

       BasZoneDataSource.EnableTransaction = false;
       BasZoneDataSource.TransactionManager = DataRepository.Connections["test"].Provider.CreateTransaction();

    I just realized this process is a bit long winded, so I will see if I can come up with a better way to handle it!

    Hope that helps.


    Bobby Diaz ------------------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------------------
    • Post Points: 65
  • 09-03-2006 9:13 PM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    thank you, bdiaz.

      I added the sentences what you said, and then got an error like this
    if (!transactionManager.IsOpen) throw new DataException("Transaction must be open before executing a query.");

     If i add 
           BasZoneDataSource.EnableTransaction = True;
       BasZoneDataSource.TransactionManager = DataRepository.Connections["test"].Provider.CreateTransaction();

     TransactionManager still use default connection. I got the same error like before.

     
    • Post Points: 5
  • 09-03-2006 9:31 PM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    In "EntityTransactionModule.cs",

    HttpContext context = HttpContext.Current;
        TransactionManager mgr = null;

        if ( context != null )
        {
         mgr = context.Items[ManagerKey] as TransactionManager;

         if ( mgr == null )
         {
                                mgr = DataRepository.Provider.CreateTransaction();
                                mgr.BeginTransaction();

          context.Items[ManagerKey] = mgr;
         }
        }

        return mgr;

    So if we haven't set the context.Items[ManagerKey],  EntityTransactionModule still use default provider.

    • Post Points: 5
  • 09-06-2006 4:02 AM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    Can somebody change Datasourcecontrol's ConnectionString?
    • Post Points: 35
  • 09-06-2006 11:19 AM In reply to

    Re: Runtime Connection Switching

    What do you mean, can someone change the connection string of a datasource control?  Can you clarify? Smile [:)]

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 09-06-2006 9:25 PM In reply to

    • NetFan
    • Top 500 Contributor
    • Joined on 07-20-2006
    • Posts 10
    • Points 270

    Re: Runtime Connection Switching

    Hi, Robert

          My mean is how to change the connectionstring of  DataSourceControl's Provider at runtime.
    I need this, Can you help me!

    Thanks!

    • Post Points: 35
  • 09-06-2006 10:00 PM In reply to

    Re: Runtime Connection Switching

    This is a two step process, first you add the dynamic connection string to the Connections dictionary.  If this has already been added then you can simply reference that connection string by key.

    //in some method as soon as you find out hte connection
    //string for this user
    Session["DynamicConnection"] = "test";
    DataRepository.AddConnection("test", "Data Source=(local)\normal;Initial Catalog=zs;User ID=sa;Password=sa");

    Page_Init(...)
    {
    BasZoneDataSource.EnableTransaction = false;
    BasZoneDataSource.TransactionManager =     DataRepository.Connections[Session["DynamicConnection"].ToString()].Provider.CreateTransaction(); 
        BasZoneDataSource.Provider = DataRepository.Connections["test"].Provider;
    }


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 75
Page 1 of 2 (21 items) 1 2 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems