New Posts New Posts RSS Feed: Manual Custom Connection without having to leave the application
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Manual Custom Connection without having to leave the application

 Post Reply Post Reply Page  <12
Author
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Topic: Manual Custom Connection without having to leave the application
    Posted: 22-Jul-2012 at 2:14pm
1. Setup a global shared UserSession object or something like that where you can store the current selected connection. You can then inject the UserSession into the LoginViewModel were you set it and in the EntityManagerFactory to read it in order to configure the EntityManagerProvider.

2. Before you call login, configure the AuthenticationService with the correct ConnectionOptions.

3. By implementing a DataSourceKeyResolver. Here's an example: http://drc.ideablade.com/xwiki/bin/view/Documentation/code-sample-custom-datasourcekeyresolver
Back to Top
giotis View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Apr-2012
Location: Greece
Posts: 53
Post Options Post Options   Quote giotis Quote  Post ReplyReply Direct Link To This Post Posted: 22-Jul-2012 at 1:59pm
1. How do I switch between two or more connections?
       start app
            login (in LoginView we have one ComboListBox with Connections)
                 -  Production
                 -  Development
                
x Test
            logout
            login
                 -  Production
                 x  Development
                
- Test
            logout
       exit  

2. How configure the authentication service with the same ConnectionOptions?

3. If we dont have the connections in app.config how we can to load them? (I mean with code)

Back to Top
giotis View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Apr-2012
Location: Greece
Posts: 53
Post Options Post Options   Quote giotis Quote  Post ReplyReply Direct Link To This Post Posted: 23-May-2012 at 4:06pm
many thanks  beautiful
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 22-May-2012 at 7:20pm
Default, Fake and DesignTime are just three out-of-the-box preconfigured ConnectionOptions that cover the standard use cases. You can create as many of your own as you need. The easiest way is to clone an existing one if you only need to change one or two things. So, let's say I want to setup TempHire with a Development, Test and Production DB and I want to be able to specify at runtime, which of the three DBs I want to connect to. To keep it simple, I have hardcoded connection strings in my Web.config for each of the three environments:
  <connectionStrings>
    <add name="TempHireEntities_DEV" connectionString="..." />
    <add name="TempHireEntities_TEST" connectionString="..." />  
<
add name="TempHireEntities_PROD" connectionString="..." />   </connectionStrings>
 
The part after the underscore is the aforementioned DataSourceExtension, which lets you choose at runtime from a list of connections.
 
The first thing I do is create myself a ConnectionOptionsResolver that can produce the appropriate ConnectionOptions object for each of the environments.
 
    public class EnvironmentResolver : IConnectionOptionsResolver
    {
        public ConnectionOptions GetConnectionOptions(string name)
        {
            if (name == "Development")
                return ConnectionOptions.Default.WithDataSourceExtension("DEV").WithName("Development");
            if (name == "Test")
                return ConnectionOptions.Default.WithDataSourceExtension("TEST").WithName("Test");
            if (name == "Production")
                return ConnectionOptions.Default.WithDataSourceExtension("PROD").WithName("Production");
 
            return null;
        }
 
I simply clone ConnectionOptions.Default and change the DataSourceExtension and Name, because everything else stays the same in this case. The DataSourceExtension needs to match the part after the underscore in the connection strings above.
 
Now, when I create the EntityManagerProvider in the EntityManagerProviderFactory, I simply configure it with the correct environment.
 
        [Export]
        public IEntityManagerProvider<TempHireEntities> TempHireEntityManagerProvider
        {
            get
            {
                var provider = new EntityManagerProvider<TempHireEntities>()
                    .Configure(config => config.WithConnectionOptions("Test"));
                return provider;
            }
        }
 
Instead of hardcoding the configuration, I would of course have a drop down on my login screen to let the user choose the environment and I store the user's selection somewhere where I can grab it from the EntityManagerProviderFactory.
 
Also, don't forget to configure the authentication service with the same ConnectionOptions, so you login against the correct environment.
 
 
 
Back to Top
giotis View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Apr-2012
Location: Greece
Posts: 53
Post Options Post Options   Quote giotis Quote  Post ReplyReply Direct Link To This Post Posted: 22-May-2012 at 5:50pm
I followed the debugging steps I have found that the "EntityManagerProvider" is the
class responsible the possible initialization of selected of "ConnectionOptions".
I only have three choices Default Fake and DesignTime, whether to change something ?

Two days I trying and I still did not get anything,
I have read all the relevant issues help documentation samples examples ... nothing,
can someone help me here in the forum because my next option is to go to mediums.

Thanks

Edited by giotis - 22-May-2012 at 5:53pm
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 21-May-2012 at 11:14pm
Using a DataSourceExtension you can connect to different environments/DBs at runtime. That's typically what you use if the user can select different environments/DBs from a drop down during login. In Cocktail you specify the DataSourceExtension through the ConnectionOptions. You can implement an IConnectionOptionsResolver to dynamically initialize the proper ConnectionOptions based on information that the user supplies during login. More on the DataSourceExtension can be found here:


To supply the connection string information for your environments/DBs programmatically from an external file for example see the following topic. 


In most case you'll be implementing a custom DataSourceKeyResolver. You can find an example here:

Back to Top
giotis View Drop Down
Groupie
Groupie
Avatar

Joined: 26-Apr-2012
Location: Greece
Posts: 53
Post Options Post Options   Quote giotis Quote  Post ReplyReply Direct Link To This Post Posted: 21-May-2012 at 3:14pm
1.how we can import connections from an external file like

    connections.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
          <connectionStrings>
                <add name="admin_TempHireEntities" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TempHire1;Integrated ...     
                <add name="admin_TempHireEntities" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TempHire2;Integrated ...     .
                <add name="user_TempHireEntities"  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TempHire3;Integrated ...         
                <add name="user_TempHireEntities"  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TempHire4;Integrated  ...    
        </connectionStrings>

    </configuration>

  a)so that it reads from an external source which preferably will change outside
  b)be filtered with a value, for example only admin_ for this only user_ for that
  c)maybe a ComboBox at LoginView the final choice

2.any time after Login to choose connection without having to leave the application
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down