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: 04-Sep-2012 at 11:11pm
Sorry filled,
the combobox not show the state from Adresses
 
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:45pm
I put a TextBlock in StaffingResourceAddressListView
  <TextBlock Text="{Binding Item.StateId}" />

everything work fine
ComboBox have problem

<ComboBox Grid.Row="4"
                                  Grid.Column="1"
                                  Width="140"
                                  Height="25"
                                  Margin="2"
                                  HorizontalAlignment="Left"
                                  DisplayMemberPath="Name"
                                  ItemsSource="{Binding ElementName=LayoutRoot,
                                                        Path=DataContext.States}"
                                  MaxDropDownHeight="200"
                                  SelectedValue="{Binding Item.StateId,
                                                          ValidatesOnDataErrors=True,
                                                          ValidatesOnExceptions=True,
                                                          Mode=TwoWay}"
                                  SelectedValuePath="Id" />
 
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: 05-Sep-2012 at 10:14am
Not sure without debugging the code. Are you logging out  before switching the database?
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: 06-Sep-2012 at 7:42am
test by your self (Cocktail ver 19545) I'll only take 2 minutes

namespace Common {
    public interface IUserSession   {
        string Name { get; set; }
    }
}
namespace Common{
    [Export(typeof(IUserSession)), PartCreationPolicy(CreationPolicy.Shared)]
    public class UserSession : IUserSession    {
        private string _name;
        public UserSession()        {        }
        public string Name {
            get { return _name; }
            set { _name = value; }
        }
    }
}
namespace TempHire {
  public class CustomDataSourceKeyResolver : IDataSourceKeyResolver {
    public IdeaBlade.EntityModel.IDataSourceKey GetKey(string keyName, string keyExtension, bool onServer) {
      if (!onServer) { return null; }
      if (string.IsNullOrEmpty(keyExtension)) { return null; }
      if (keyName != "TempHireEntities") { return null; }
      String connectionString = "";
      if (keyExtension == "PROD")      {          connectionString = @"Data Source=|DataDirectory|TempHire_Prod.sdf";      }
      if (keyExtension == "TEST")      {          connectionString = @"Data Source=|DataDirectory|TempHire_Test.sdf";      }    
      string name = string.IsNullOrEmpty(keyExtension) ? keyName : keyName + "_" + keyExtension;
      return new ClientEdmKey(name, connectionString, false, "FromResolver");
    }
  }
}
namespace TempHire {
    public class EnvironmentResolver : IConnectionOptionsResolver    {
        private static ConnectionOptions TestEnv = ConnectionOptions.Default.WithDataSourceExtension("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)  {
                        case "Test": return TestEnv.WithName(ConnectionOptions.Default.Name);
                        case "Prod": return ProdEnv.WithName(ConnectionOptions.Default.Name);

                        default: return null;
                    }
                    //return ConnectionOptions.Default.WithDataSourceExtension(_userSession.Name.ToUpper()).WithName(ConnectionOptions.Default.Name);
                }
            return null;
        }
    }
}

namespace TempHire.ViewModels.Login
{  using System.Linq;

    [Export]
    public class LoginViewModel : Screen, IResult   {
        .......
        private IUserSession _userSession;
        private string _displayMemberPath;
        private BindableCollection<object> _items;
        private object _selectedItem;

        [ImportingConstructor]
        public LoginViewModel(IAuthenticationService authenticationService, IWindowManager windowManager,
                              [Import(AllowDefault = true)] IGlobalCache globalCache, IUserSession userSession)    {
            _userSession = userSession;
            DisplayMemberPath = "WithName";
            Items = new BindableCollection<object>() { new Connections() { WithName = "Test" }, new Connections() { WithName = "Prod" } };
        }
        private class Connections   {
            public Connections() { }
            public string WithName { get; set; }
        }       
        public string DisplayMemberPath        {
            get { return _displayMemberPath; }
            set  {
                _displayMemberPath = value;
                NotifyOfPropertyChange(() => DisplayMemberPath);
            }
        }
        public object SelectedItem        {
            get { return _selectedItem; }
            set  {
                _selectedItem = value;
                NotifyOfPropertyChange(() => SelectedItem);
                _userSession.Name = ((Connections)SelectedItem).WithName;
            }
        }
        public BindableCollection<object> Items    {
            get { return _items; }
            set     {
                _items = value;
                NotifyOfPropertyChange(() => Items);
                SelectedItem = _items.FirstOrDefault();
            }
        }

           LoginView after password
            <ComboBox x:Name="Items"
                  Grid.Row="3" Grid.Column="1"
                  Height="25"
                  Margin="2,2,81,2"
                  VerticalAlignment="Center"
                  DisplayMemberPath="{Binding DisplayMemberPath}" Width="137" />
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: 12-Sep-2012 at 9:41am
hello Marcel
you found time for analysis
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: 12-Sep-2012 at 10:13am
Sorry, I have not. I should have time next week.
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-Sep-2012 at 1:59am
Sorry it took me a while to look into this. The issue here is that the States entities are cached by the StaffingResourceAddressListViewModel between sessions. You have to clear them when the VM closes, so they get loaded fresh from the correct data source, otherwise they don't match. Modify StaffingResourceAddressListViewModel.OnDeactivate as follows:
 
        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
 
            if (!close) return;
 
            ClearAddresses();
            States = null;
        }
Back to Top
 Post Reply Post Reply Page  <12

Forum Jump Forum Permissions View Drop Down