I’m going to do a wpf application using MVVM(It based on http://www.codeproject.com/KB/WPF/MVVMQuickTutorial.aspx
).
This application will be connecting with webservice one per month.
On webservice I have contract
public class Student
{
public string Name {get; set;}
public int Score {get; set;}
public DateTime TimeAdded {get; set;}
public string Comment {get; set;}
}
In WPF application Adding, and removing students will be saving to xml file.
So at wpf application Student would be something like :
public class Student
{
public string Name {get; set;}
public int Score {get; set;}
public DateTime TimeAdded {get; set;}
public string Comment {get; set;}
public Student(string Name, int Score,
DateTime TimeAdded, string Comment) {
this.Name = Name;
this.Score = Score;
this.TimeAdded = TimeAdded;
this.Comment = Comment;
}
}
public class StudentsModel: ObservableCollection<Student>
{
private static object _threadLock = new Object();
private static StudentsModel current = null;
public static StudentsModel Current {
get {
lock (_threadLock)
if (current == null)
current = new StudentsModel();
return current;
}
}
private StudentsModel()
{
// Getting student s from xml
}
}
public void AddAStudent(String Name,
int Score, DateTime TimeAdded, string Comment) {
Student aNewStudent = new Student(Name, Score,
TimeAdded, Comment);
Add(aNewStudent);
}
}
How connect this two classes ?
The worst think I guess is that contract Student from webservice would be use in this wpf application to get students from xml, in other application collection of studetns would be getting from database.
I’m newbie in design patterns so it is very hard for me :/
Example: I click AddUser, and in application A it calls webservice method which adding user to database, in application B it adds user to XML file, and In application.
Base class are contracts at webservice.
Next explanation:
First application uses webservice to save data at database. Second application never save data in xmls and one perm month send this xmls to webservice and convert their to intances of students and save it at database
It is very unclear from your question what the actual problem is. But, I guess I could address a few and show you a way to solve them.
1) The main problem I see in your project is you have two definitions of
Studentclass. You can easily merge them into a single definition. (I will just show you how…)2) It is very unclear whether you want your
WPFclient to save data to aData Source(XML?) or yourWeb Serviceshould do it. And if theWPFclient is supposed to save theStudents then what is theWeb Servicefor?3) You don’t have a
ViewModeldefined anywhere for theStudentclass which in this case isModel.I have created an example with 3 projects.
1)
WebService– A WCF Service Project2)
StudentLib– A Class Library Project (whereStudentclass is defined)3)
DesktopClient– A WPF Application ProjectHere is the source code :
WebService.IStudentService.csWebService.StudentService.csWebService's Web.ConfigStudentLib.Student.csDesktopClient.StudentViewModel.csDesktopClient.MainWindow.xamlDesktopClient.MainWindow.csHere all the above mentioned problems are resolved :
1) The
Studentclass is defined in a common assembly (StudentLib) referenced by bothWebServiceproject andDesktopClientproject. So, while adding aService Reference, that class is reused by the code generator.2) I recommend all the storage related operations be in the
Web Serviceand theClientapp should just useWeb Serviceto store data.3)
StudentViewModelclass is used instead ofStudentclass to display data inMainWindow.