使用MVVM Light创建带有依赖注入的连接字符串
本文关键字:注入 连接 字符串 依赖 MVVM Light 创建 使用 | 更新日期: 2023-09-27 18:16:43
我正在使用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
}
}
How About:
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");
}