C# 工作MvvM和并发性
我正在使用WPF和MvvM开发一个用C#编写的应用程序。我仍然是MvvM和实体框架的一些概念的初学者。除了并发性问题,我的一切似乎都在运行。我经历了这一切,但这对我来说不起作用。因此,与MvvM结构一样,我有一个名为BackflowManagementEntities的模型和实体文件。然后,我有一个存储库来处理测试人员的更新。我在这里试着接球,但是运气不好C# 工作MvvM和并发性,c#,wpf,mvvm,concurrency,C#,Wpf,Mvvm,Concurrency,我正在使用WPF和MvvM开发一个用C#编写的应用程序。我仍然是MvvM和实体框架的一些概念的初学者。除了并发性问题,我的一切似乎都在运行。我经历了这一切,但这对我来说不起作用。因此,与MvvM结构一样,我有一个名为BackflowManagementEntities的模型和实体文件。然后,我有一个存储库来处理测试人员的更新。我在这里试着接球,但是运气不好 private BackflowManagementEntities dbContext; internal void SaveChange
private BackflowManagementEntities dbContext;
internal void SaveChanges()
{
try
{
dbContext.SaveChanges();
}
catch (DBConcurrencyException ex)
{
Console.WriteLine("Concurrency Exception : " + ex.Message);
}
}
所以我有一个三部分的问题:
简言之,将
ConcurrencyMode
更改为Fixed并捕获OptimisticConcurrencyException
异常…我知道我已经很晚了,但这是许多开发人员在使用实体框架和wpf开发多线程应用程序时面临的问题。
现在有几件事你必须注意
public interface IDataRepository
{
void AddAsync<T>(T entity, Action<T>callBack,Action<Exception>exceptionCallback=null) where T : EntityBase;
void DeleteAsync<T>(int[] ids, Action callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void UpdateAsync<T>(T entity, Action<T> callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void FindAsync<T>(int id, Action<T> callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void FindAsync<T>(Action<List<T>> callBack,Expression<Func<T, bool>> predicate=null, int? pageIndex=0, int? pageSize=20, params Expression<Func<T, object>>[] includes) where T : EntityBase;
}
public interface IDataRepository
{
void AddAsync<T>(T entity, Action<T>callBack,Action<Exception>exceptionCallback=null) where T : EntityBase;
void DeleteAsync<T>(int[] ids, Action callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void UpdateAsync<T>(T entity, Action<T> callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void FindAsync<T>(int id, Action<T> callBack, Action<Exception> exceptionCallback = null) where T : EntityBase;
void FindAsync<T>(Action<List<T>> callBack,Expression<Func<T, bool>> predicate=null, int? pageIndex=0, int? pageSize=20, params Expression<Func<T, object>>[] includes) where T : EntityBase;
}
这正是我想要的。非常感谢您的快速响应。请注意,在生产过程中,我使用mef作为DI来导入存储库。事实上,我使用了一种称为RepositoryFactory Patrn的好模式来实现这一点。有关完整的源代码,您可以通过suulisindiyaka@stoicteam.com
public interface IStudentRepository:IDataRepository
{
void AddGuardian(Gurdian gurdian, int studentId,Action<Student> callBack);
void SaveStudent(Student student, Gurdian gurdian,Action<Student>callBack);
void GetPrimaryGurdian(int studentId,Action<Gurdian> callBack );
}
[Export(typeof(IStudentRepository))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class StudentRepository : Repository<StudentContext>, IStudentRepository
{
public void AddGuardian(Gurdian gurdian, int studentId, Action<Student> callBack)
{
FindAsync<Student>(studentId, (student) =>
{
student.Gurdians.Add(gurdian);
UpdateAsync(student, callBack);
});
}
public void SaveStudent(Student student, Gurdian gurdian, Action<Student> callBack)
{
student.Gurdians.Add(gurdian);
AddAsync(student, callBack);
}
public void GetPrimaryGurdian(int studentId, Action<Gurdian> callBack)
{
FindAsync<Student>(studentId,(student)=> { callBack(student.PrimaryGurdian); });
}
}
[Export]
[PartCreationPolicy(CreationPolicy.Shared)]
public class StudentViewModel : ViewModelBase
{
private readonly IStudentRepository _repository;
[ImportingConstructor]
public StudentViewModel()
{
_repository =new StudentRepository();
Student = new Student();
SaveStudentCommand = new RelayCommand(OnStudentSaveExcute, CanSaveStudent);
}
#region Properties
private Student _student;
public Student Student
{
get { return _student; }
set { _student = value; OnPropertyChanged(() => Student); }
}
private ObservableCollection<Student> _students = new ObservableCollection<Student>();
public ObservableCollection<Student> Students
{
get { return _students; }
set { _students = value; }
}
#endregion
#region Commands
public ICommand SaveStudentCommand { get; set; }
private void OnStudentSaveExcute()
{
_repository.SaveStudent(Student,Gurdian, (student) =>
{
_students.Add(student);
});
}
#endregion
private void LoadStudents()
{
_repository.FindAsync<Student>((students) =>
{
foreach(var student in students)
_students.Add(student);
});
}
}
public class EntityBase{
public int get{get;set;}
}