我是 .Net 技术的新手,我想从分层应用程序开始,作为 Unity 和 WPF 的测试。执行时出现以下异常:ConfigurationErrorsException:未找到有效属性来构造参数 mefRepository 的值请检查配置文件。在 Unity 配置下方:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<typeAliases>
<typeAlias alias="IUnitOfWork" type="DDDTest.Domain.Core.IUnitOfWork,DDDTest.Domain.Core"/>
<typeAlias alias="UnitOfWork" type="DDDTest.Infrastructure.Data.MainModule.UnitOfWork,DDDTest.Infrastructure.Data.MainModule"/>
<typeAlias alias="IRepository" type="DDDTest.Domain.Core.IRepository,DDDTest.Domain.Core"/>
<typeAlias alias="Repository" type="DDDTest.Infrastructure.Data.MainModule.Repository,DDDTest.Infrastructure.Data.MainModule"/>
<typeAlias alias="IMefRepository" type="DDDTest.Domain.MainModule.Entities.IMefRepository,DDDTest.Domain.MainModule.Entities"/>
<typeAlias alias="MefRepository" type="DDDTest.Infrastructure.Data.MainModule.Repositories.MefRepository,DDDTest.Infrastructure.Data.MainModule.Repositories"/>
<typeAlias alias="IMefService" type="DDDTest.Domain.MainModule.Services.IMefService,DDDTest.Domain.MainModule.Services"/>
<typeAlias alias="MefService" type="DDDTest.Domain.MainModule.Services.MefService,DDDTest.Domain.MainModule.Services"/>
</typeAliases>
<container>
<register type ="IUnitOfWork" mapTo="UnitOfWork" name="UoW"/>
<register type="IRepository" mapTo="Repository">
<constructor>
<param name="uow" dependencyName="UoW"/>
</constructor>
</register>
<register type ="IMefRepository" mapTo="MefRepository" name="MefRepo"/>
<register type ="IMefService" mapTo="MefService">
<constructor>
<param name="mefRepository" dependenceyName="MefRepo"/>
<param name="mefRepository" dependenceyType="MefRepository"/>
</constructor>
</register>
</container>
</unity>
MefService 类如下:
namespace DDDTest.Domain.MainModule.Services
{
public class MefService : IMefService, IDisposable
{
IMefRepository _mefRepo;
public MefService(IMefRepository mefRepository)
{
_mefRepo = mefRepository;
}
//Add New Mef
public void AddMef(misfor _mef)
{
_mefRepo.Add(_mef);
}
//Delete a mef
public void DeleteMef(misfor _mef)
{
_mefRepo.Remove(_mef);
}
//Get a new empty mef
public misfor GetNewObject()
{
return _mefRepo.GetNewEmpty();
}
//Find Mef By Primary Key
public misfor GetById(string ctr, short mef)
{
return _mefRepo.GetById(ctr, mef);
}
public void Dispose()
{
_mefRepo = null;
}
}
}
WPF窗口如下:
namespace DDDTest.Presentation.MainModule
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
IMefRepository _mr;
MefService _ms;
misfor _currentMef;
public IUnityContainer unityContainer;
public MainWindow()
{
InitializeComponent();
}
//Initialisation du container unity pour l'injection des dépendances
public void InitIocContainer()
{
unityContainer = new UnityContainer();
unityContainer.LoadConfiguration();
}
private void MefCode_KeyDown(object sender, KeyEventArgs e)
{
string c = txctrCode.Text;
short m = short.Parse(txMefCode.Text);
if (e.Key == Key.Enter)
{
_currentMef = _ms.GetById(c, m);
if (_currentMef != null)
grdMaster.DataContext = _currentMef;
else
MessageBox.Show("Mise en force introuvable.", "Erreur", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitIocContainer();
_ms = new MefService(_mr);
_currentMef = _ms.GetNewObject();
grdMaster.DataContext = _currentMef;
}
}
}
第一个问题是我不知道在哪里初始化 Unity 容器。我在网上找到了很多示例,但都是关于 Unity 和 WPF/MVVM 模式的。第二个问题是我找不到错误的根源,因为 MefService 类的代码似乎是正确的。欢迎您的帮助。