我正在研究 C# 中的 MVVM。
我想使用控制反转(IoC)。我使用框架 Unity。
我不明白如何处理可能从数据访问层引发的异常。
这是我为学习做的一个简单的例子:
-- 我省略了模型验证(IDataErrorInfo)和 ViewModel 服务(例如:DialogService)的管理 --
XAML 视图
<TextBox ... Text="{Binding Path=Id}" />
<TextBox ... Text="{Binding Path=Name}"/>
设计应用
模型
{
public class User : INotifyPropertyChanged
{
private int _id;
public int Id
{
get{return _id;}
set
{
_id = value;
this.OnPropertyChanged("Id");
}
}
private string _name;
public string Name
{
get{return _name;}
set
{
_name = value;
this.OnPropertyChanged("Name");
}
}
public User(int i, string n)
{
_id = i;
_name = n;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
数据访问层
界面
public interface IDataAccessLayer
{
Model.User GetUser();
}
具体类
public class ConcreteDataAccessLayer : IDataAccessLayer
{
public ConcreteDataAccessLayer(){}
Model.User IDataAccessLayer.GetUser()
{
//could throw Exception connecting with data source
}
}
业务层
public class BusinessLayer
{
public BusinessLayer(IDataAccessLayer dataAccessLayer)
{
if (dataAccessLayer == null)
{
throw new ArgumentNullException("dataAccessLayer");
}
this._dataAccessLayer = dataAccessLayer;
}
private IDataAccessLayer _dataAccessLayer;
private QuestionStak.Model.User _user;
internal QuestionStak.Model.User User
{
get
{
if (_user == null)
_user = _dataAccessLayer.GetUser();
return _user;
}
}
}
视图模型
public class ViewModel : INotifyPropertyChanged
{
public ViewModel(BusinessLayer bl)
{
if (bl == null)
{
throw new ArgumentNullException("BusinessLayer");
}
_businessLayer = bl;
}
private BusinessLayer _businessLayer;
public int Id
{
get
{
return _businessLayer.User.Id;
}
set
{
_businessLayer.User.Id = value;
}
}
public string Name
{
get
{
return _businessLayer.User.Name;
}
set
{
_businessLayer.User.Name = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
应用
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//Inversion of control
IUnityContainer container = new UnityContainer();
//Created as singleton
container.RegisterType<IDataAccessLayer, ConcreteDataAccessLayer>(new ContainerControlledLifetimeManager());
container.RegisterType<BusinessLayer, BusinessLayer>(new ContainerControlledLifetimeManager());
MainWindow win = container.Resolve<MainWindow>();
win.DataContext = container.Resolve<ViewModel>();
win.Show();
}
}
遵循原则
- 简单的构造函数(Unity 捕获所有异常并遵循http://blog.ploeh.dk/2011/03/03/InjectionConstructorsshouldbesimple/)
- ViewModel 必须隐藏 Model 结构
所以我有一个我不明白如何解决的问题:
- 如果 ConcreteDataAccessLayer 在加载 ViewModel 期间无法加载数据(例如:服务器不可用),则语句 _dataAccessLayer.GetUser() 会抛出异常并且无法管理它(由 Unity 容器捕获)
- 如果在加载过程中的某个地方我管理异常,则数据绑定会导致抛出空异常,因为 _businessLayer.User 为空(无法加载视图)
请问,有人能解决这个问题吗?
谢谢!