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
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 Topic: Manual Custom Connection without having to leave the application
    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
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: 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: 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: 23-May-2012 at 4:06pm
many thanks  beautiful
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
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-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 4:13pm
DataSourceKeyResolver run only once, how do I switch?
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-Jul-2012 at 7:39pm
I'm not sure I follow. IDataSourceKeyResolver.GetKey gets called for each unique keyName/keyExtension pair. So, if we go back to my earlier example. The first time you connect to DEV it gets called with keyName="TempHireEntities" and keyExtension="DEV". The first time you connect to TEST it gets called with keyName="TempHireEntities" and keyExtension="TEST" and so on. In each case you return the appropriate connection string to the corresponding physical DB.
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: 28-Aug-2012 at 4:21pm
Dear Marcel so far no so good,

my steps

a. public interface IUserSession   {
        string Name { get; set; }
    }

    [Export(typeof(IUserSession)), PartCreationPolicy(CreationPolicy.Shared)]
    public class UserSession : IUserSession    {
        public UserSession()        {   }
        public string Name { get; set; }
    }

b.public class EnvironmentResolver : IConnectionOptionsResolver  {     
        public ConnectionOptions GetConnectionOptions(string name)
        {
            IUserSession userSession = Composition.GetInstance<IUserSession>(CreationPolicy.Shared);
            if (name == userSession.Name)
                return ConnectionOptions.Default.WithDataSourceExtension(
                                userSession.Name.ToUpper()).WithName(userSession.Name);
            return null;
        }
    }
c. <connectionStrings>
      <add name="SecurityEntities" connectionString=...
  </connectionStrings>

d.class CustomDataSourceKeyResolver : IDataSourceKeyResolver {
    public GetKey(string keyName, string keyExtension, bool onServer) {
      if (!onServer) {return null;}
      if (keyName != "TempHireEntities" || string.IsNullOrEmpty(keyExtension)) { return null; }
      String connectionString = "";
      if (keyExtension == "PROD")   {
          connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TempHire_Production; ...
      }
      if (keyExtension == "TEST")    {
          connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TempHire_Test; ...
      }

      if(string.IsNullOrEmpty(connectionString ) { return null; }

      // Now build and return a "ClientEdmKey". 
      string name = string.IsNullOrEmpty(keyExtension) ? keyName : keyName + "_" + keyExtension;
      return new ClientEdmKey(name, connectionString, false, "FromResolver");
    }
  }

e.[Export]
    public IEntityManagerProvider<TempHireEntities> TempHireEntityManagerProvider    {
            get    {
                IUserSession userSession = Composition.GetInstance<IUserSession>(CreationPolicy.Shared);
                var provider = new EntityManagerProvider<TempHireEntities>()
                    .Configure(config => config.WithConnectionOptions(userSession.Name));
                return provider;
            }
        }

f. [ImportingConstructor]
   public LoginViewModel(IAuthenticationService authenticationService, IWindowManager windowManager,
                              [Import(AllowDefault = true)] IPreLoader preLoader, IUserSession userSession
           ....
            _userSession = userSession;
           ....

   _userSession.Name = filled from one new field named KeyName
    public string KeyName
        {
            get { return _keyName; }
            set
            {
                _keyName= value;
                _userSession.Name = value;
                NotifyOfPropertyChange(() => KeyName);
                NotifyOfPropertyChange(() => CanLogin);
            }
        }


g. before Login
    public IEnumerable<IResult> Login()  {
            _authenticationService. ??? There is no implementation to configure ConnectionOptions
            using (Busy.GetTicket()) ...

I am stuck

ps. If CustomDataSourceKeyResolver return null how can it manage from LoginViewModel





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: 04-Sep-2012 at 12:17am
Sorry, I was swamped. What exactly are you stuck on? You can make this a lot simpler by writing a ConnectionOptionsResolver that simply replaces the system default ConnectionOptions based on the user's selection. 


public class EnvironmentResolver : IConnectionOptionsResolver
{
private static ConnectionOptions DevEnv = ConnectionOptions.Default.WithDataSourceExtension("DEV");

        private static ConnectionOptions TestEnv = ConnectionOptions.Default.WithDataSourceExtensioin("TEST");

        private static ConnectionOptions ProdEnv = ConnectionOptions.Default.WithDataSourceExtension("PROD");

        private IUserSession _userSession;

        [ImportingConstructor]
        public EnvironmentResolver(IUserSession userSession)
        {
              _userSession = userSession;
        }

        public ConnectionOptions GetConnectionOptions(string name)
        {
if (name == ConnectionOptions.Default.Name)
                {
switch (_userSession.Name)
                        {
                               "DEV" : return DevEnv.WithName(ConnectionOptions.Default.Name);
                               "TEST" : return TestEnv.WithName(ConnectionOptions.Default.Name);
                               "PROD" : return ProdEnv.WithName(ConnectionOptions.Default.Name);

                                default : return null;
                        }
                }

                return null;
        }
}

Now you don't have to worry about configuring the EntityManagerProvider or the AuthenticationService. They'll keep looking for the default ConnectionOptions and the above resolver simply feeds a properly configured ConnectionOptions instead of the system default.
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: 04-Sep-2012 at 10:28pm
perfect
I noticed when  GetConnectionOptions take value is always "Default" and ConnectionOptions.Default.Name having the same value , always , that is correct?
because in this case we do not need to check out.

Also when we change the connection not fill the comboboxes at second database,
when change again the first database the comboboxes is correct


Edited by giotis - 04-Sep-2012 at 10:46pm
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: 04-Sep-2012 at 10:37pm
Well, Default is used if the EntityManagerProvider and AuthenticationSerivce are not configured with another ConnectionOptions. You should always check what name needs to be resolved. You may decide later on to use different ConnectionOptions names. If you can't resolve the name than you return null in which case Cocktail looks for another resolver that might now how to resolve the name. There's a default resolver that knows how to resolve Default, Fake and DesignTime if all fails.
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: 04-Sep-2012 at 10:53pm
thank you very much
Also when we change the connection not fill the comboboxes at second database,
when change again the first database the comboboxes is correct why?

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: 04-Sep-2012 at 10:56pm
I'm not following your question. Please elaborate.
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: 04-Sep-2012 at 11:07pm
After selecting base (Prod) and connect all is well
When after changing the base(Test) the combobox(State) from Adresses are not filled,
If we go back to first base again all well
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down