使用MVVM Light创建具有依赖注入的连接字符串

时间:2014-08-06 14:10:22

标签: c# wpf mvvm dependency-injection mvvm-light

我正在使用WPF,MVVM和依赖注入模式编写登录窗口。我的问题是我必须使用登录和密码创建连接字符串,用户在我的登录表单中写入,我不知道如何按照良好的做法进行操作。对象由SimpleIoc类创建,我在初始化期间传递部分连接字符串,如数据库地址和端口。当用户写下他的登录名和密码时,我需要将此数据传递给数据库管理器以创建完整的连接字符串。每次调用某个函数连接部分连接字符串与用户和密码时,我都不想传递登录名和密码。我可以在接口中创建函数,比如Initialize,但在我看来,这不是一个好主意,我认为还有更好的方法。

以下是我如何做的示例:

public interface ILoginService
{
    bool SomeAction(string parameter);
}

public class LoginService : ILoginService
{
    private string _connectionString;

    public LoginService(string connectionStringPart)
    {
        _connectionString = connectionStringPart;
    }

    public bool SomeAction(string parameter)
    {
        //Create connection, execute query etc.
        return true;
    }
}

public class MainViewModel : ViewModelBase
{
    private ILoginService _loginService;
    private string _login;

    public string Login
    {
        get { return _login; }
        set
        {
            _login = value; 
            RaisePropertyChanged("Login");
        }
    }

    private string _password;

    public string Password
    {
        get { return _password; }
        set
        {
            _password = value; 
            RaisePropertyChanged("Password");
        }
    }
    public MainViewModel(ILoginService loginService)
    {
        _loginService = loginService;
    }

    private RelayCommand _loginCommand;

    public ICommand LoginCommand
    {
        get { return _loginCommand ?? (_loginCommand = new RelayCommand(ExecuteLogin)); }
    }

    private void ExecuteLogin()
    {
        //And here I must add login and password to _loginService but I don't want to do it by passing them to SomeAction method 
        _loginService.SomeAction("some parameter");
    }
}

public class ViewModelLocator
{
    public ViewModelLocator()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
        SimpleIoc.Default.Register<ILoginService>(()=>{return new LoginService("Server=myServerAddress;Database=myDataBase;");});
        SimpleIoc.Default.Register<MainViewModel>();
    }

    public MainViewModel Main
    {
        get
        {
            return ServiceLocator.Current.GetInstance<MainViewModel>();
        }
    }

    public static void Cleanup()
    {
        // TODO Clear the ViewModels
    }
}

1 个答案:

答案 0 :(得分:1)

如何:

public interface ILoginService
{
    bool SomeAction(string parameter);
    string Password  {set; }
    string UserName {set; }
}

public class LoginService : ILoginService
{

    private System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();

    private string _connectionString
    {
       get
       { return builder.ConnectionString;}
    }

    public LoginService(string connectionStringPart)
    {
        _connectionString = connectionStringPart;
    }

    public string Password
    {
        set { builder["Password"] =value; }
    }

    public string UserName
    {
        set { builder["user"] =value; }
    }

    public bool SomeAction(string parameter)
    {
        //Create connection, execute query etc.
        return true;
    }
}

public class MainViewModel : ViewModelBase
{
    private ILoginService _loginService;
    private string _login;

    public string Login
    {
        get { return _login; }
        set
        {
            _login = value;
            _loginService.UserName = value; 
            RaisePropertyChanged("Login");
        }
    }

    private string _password;

    public string Password
    {
        get { return _password; }
        set
        {
            _password = value;
            _loginService.Password= value; 
            RaisePropertyChanged("Password");
        }
    }
    public MainViewModel(ILoginService loginService)
    {
        _loginService = loginService;
    }

    private RelayCommand _loginCommand;

    public ICommand LoginCommand
    {
        get { return _loginCommand ?? (_loginCommand = new RelayCommand(ExecuteLogin)); }
    }

    private void ExecuteLogin()
    {

        _loginService.SomeAction("some parameter");
    }