Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Building WPF Applications with Self-Tracking Entity Generator - Project Setup

, 20 Feb 2012
This article describes the project setup of building a WPF sample application with Self-Tracking Entity Generator for WPF/Silverlight.
SchoolSample_20111201.zip
SchoolSample
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
Assemblies
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
ComponentModel.Composition.Initialization.Desktop
Microsoft
ComponentModel
Composition
Hosting
Internal
Properties
System
ComponentModel
Composition
Hosting
System.Windows.Controls.Data.Input
Common
Properties
Settings.settings
themes
Validation
Database
SchoolSample
SchoolSample.csproj.user
Asset
Control
Properties
Settings.settings
View
SchoolSample.Common
Model
Properties
Resource
SchoolSample.Data
EntityModel
Properties
Validation
SchoolSample.Data.Wcf
EntityModel
SchoolModel.edmx
Properties
Validation
SchoolSample.Model
Properties
SchoolSample.ViewModel
Properties
SchoolSample.Wcf
SchoolSample.Wcf.csproj.user
Properties
Service
SchoolService.svc
SchoolSample.WCFService
SchoolSample.WCFService.csproj.user
Properties
Service References
SchoolService
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
Test.SchoolSample.Model
Properties
Test References
SchoolSample.Model.accessor
Test.SchoolSample.ViewModel
Properties
Test References
SchoolSample.ViewModel.accessor
SchoolSample_20120117.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
SchoolSample_20120216.zip
Local.testsettings
SchoolSample.vsmdi
TraceAndTestImpact.testsettings
GalaSoft.MvvmLight.Extras.WPF4.dll
GalaSoft.MvvmLight.WPF4.dll
Moq.dll
System.Windows.Interactivity.dll
WPFToolkit.Extended.dll
Settings.settings
SchoolSample.csproj.user
Settings.settings
SchoolModel.edmx
SchoolSample.Wcf.csproj.user
SchoolService.svc
SchoolSample.WCFService.csproj.user
configuration.svcinfo
configuration91.svcinfo
Reference.svcmap
SchoolService.disco
SchoolService.wsdl
SchoolSample.Model.accessor
SchoolSample.ViewModel.accessor
using System;
using System.Collections.Generic;
using System.Linq;
using SchoolSample.EntityModel;
using System.Data;

namespace SchoolSample.Wcf.Service
{
    public class SchoolService : ISchoolService
    {
        public List<Student> GetStudents(string includeOption)
        {
            using (var context = new SchoolEntities())
            {
                switch (includeOption)
                {
                    case "None":
                        return context.People.OfType<Student>()
                            .ToList();
                    case "Enrollments.Course":
                        var studentList = new List<Student>();
                        foreach (var student in context.People.OfType<Student>())
                        {
                            var currentStudent = student;
                            using (var innerContext = new SchoolEntities())
                            {
                                studentList.Add(
                                    innerContext.People.OfType<Student>()
                                        .Include("Enrollments.Course")
                                        .Single(n => n.PersonId == currentStudent.PersonId));
                            }
                        }
                        return studentList;
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        public Student GetStudentById(int studentId)
        {
            using (var context = new SchoolEntities())
            {
                return context.People.OfType<Student>()
                    .Include("Enrollments.Course")
                    .FirstOrDefault(n => n.PersonId == studentId);
            }
        }

        public List<Instructor> GetInstructors(string includeOption)
        {
            using (var context = new SchoolEntities())
            {
                switch (includeOption)
                {
                    case "None":
                        return context.People.OfType<Instructor>()
                            .ToList();
                    case "Courses":
                        return context.People.OfType<Instructor>()
                            .Include("Courses")
                            .ToList();
                    default:
                        throw new NotImplementedException();
                }
            }
        }

        public Instructor GetInstructorById(int instructorId)
        {
            using (var context = new SchoolEntities())
            {
                return context.People.OfType<Instructor>()
                    .Include("Courses")
                    .FirstOrDefault(n => n.PersonId == instructorId);
            }
        }

        public List<Course> GetCourses()
        {
            using (var context = new SchoolEntities())
            {
                var courseList = new List<Course>();
                foreach (var course in context.Courses)
                {
                    var currentCourse = course;
                    using (var innerContext = new SchoolEntities())
                    {
                        courseList.Add(
                            innerContext.Courses
                                .Include("Enrollments.Student")
                                .Single(n => n.CourseId == currentCourse.CourseId));
                    }
                }
                return courseList;
            }
        }

        public Course GetCourseById(int courseId)
        {
            using (var context = new SchoolEntities())
            {
                return context.Courses
                    .Include("Enrollments.Student")
                    .FirstOrDefault(n => n.CourseId == courseId);
            }
        }

        public List<object> UpdateInstructor(Instructor item)
        {
            var returnList = new List<object>();
            if (item == null)
            {
                returnList.Add("Instructor cannot be null.");
                returnList.Add(0);
                return returnList;
            }
            try
            {
                using (var context = new SchoolEntities())
                {
                    switch (item.ChangeTracker.State)
                    {
                        case ObjectState.Added:
                            // server side validation
                            item.ValidateObjectGraph();
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        case ObjectState.Deleted:
                            // verify whether there is any course assigned to this instructor
                            var courseExists = context.Courses.Any(n => n.InstructorId == item.PersonId);
                            if (courseExists)
                            {
                                returnList.Add("Cannot delete, there still exists course assigned to this instructor.");
                                returnList.Add(item.PersonId);
                                return returnList;
                            }
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        default:
                            // server side validation
                            item.ValidateObjectGraph();
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                    }
                }
                returnList.Add(string.Empty);
                returnList.Add(item.PersonId);
            }
            catch (OptimisticConcurrencyException)
            {
                var errorMessage = "Instructor " + item.PersonId + " was modified by another user. " +
                                   "Refresh that item before reapply your changes.";
                returnList.Add(errorMessage);
                returnList.Add(item.PersonId);
            }
            catch (Exception ex)
            {
                Exception exception = ex;
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }
                var errorMessage = "Instructor " + item.PersonId + " has error: " + exception.Message;
                returnList.Add(errorMessage);
                returnList.Add(item.PersonId);
            }
            return returnList;
        }

        public List<object> UpdateStudent(Student item)
        {
            var returnList = new List<object>();
            if (item == null)
            {
                returnList.Add("Student cannot be null.");
                returnList.Add(0);
                return returnList;
            }
            try
            {
                using (var context = new SchoolEntities())
                {
                    switch (item.ChangeTracker.State)
                    {
                        case ObjectState.Added:
                            // server side validation
                            item.ValidateObjectGraph();
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        case ObjectState.Deleted:
                            // verify whether there is any course enrollment for this student
                            var courseExists = context.Enrollments.Any(n => n.StudentId == item.PersonId);
                            if (courseExists)
                            {
                                returnList.Add("Cannot delete, there still exists course enrollment for this student.");
                                returnList.Add(item.PersonId);
                                return returnList;
                            }
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        default:
                            // server side validation
                            item.ValidateObjectGraph();
                            // save changes
                            context.People.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                    }
                }
                returnList.Add(string.Empty);
                returnList.Add(item.PersonId);
            }
            catch (OptimisticConcurrencyException)
            {
                var errorMessage = "Student " + item.PersonId + " was modified by another user. " +
                                   "Refresh that item before reapply your changes.";
                returnList.Add(errorMessage);
                returnList.Add(item.PersonId);
            }
            catch (Exception ex)
            {
                Exception exception = ex;
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }
                var errorMessage = "Student " + item.PersonId + " has error: " + exception.Message;
                returnList.Add(errorMessage);
                returnList.Add(item.PersonId);
            }
            return returnList;
        }

        public List<object> UpdateCourse(Course item)
        {
            var returnList = new List<object>();
            if (item == null)
            {
                returnList.Add("Course cannot be null.");
                returnList.Add(0);
                return returnList;
            }
            try
            {
                using (var context = new SchoolEntities())
                {
                    switch (item.ChangeTracker.State)
                    {
                        case ObjectState.Added:
                            // server side validation
                            item.ValidateObjectGraph();
                            // verify whether the instructor for this course exists
                            bool instructorExists = context.People.OfType<Instructor>()
                                .Any(n => n.PersonId == item.InstructorId);
                            if (!instructorExists)
                            {
                                returnList.Add("Cannot add, there does not exist this instructor for the course.");
                                returnList.Add(item.CourseId);
                                return returnList;
                            }
                            // verify all enrollments for this course have valid students
                            bool enrollmentsValid = item.Enrollments
                                .Aggregate(true, (current, enrollment) =>
                                                 current && context.People.OfType<Student>()
                                                                .Any(n => n.PersonId == enrollment.StudentId));
                            if (!enrollmentsValid)
                            {
                                returnList.Add("Cannot add, some enrollment does not have valid student.");
                                returnList.Add(item.CourseId);
                                return returnList;
                            }
                            // save changes
                            context.Courses.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        case ObjectState.Deleted:
                            // save changes
                            context.Courses.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                        default:
                            // server side validation
                            item.ValidateObjectGraph();
                            // verify whether the instructor for this course exists
                            instructorExists =
                                context.People.OfType<Instructor>()
                                    .Any(n => n.PersonId == item.InstructorId);
                            if (!instructorExists)
                            {
                                returnList.Add("Cannot update, there does not exist this instructor for the course.");
                                returnList.Add(item.CourseId);
                                return returnList;
                            }
                            // verify all enrollments for this course have valid students
                            enrollmentsValid = item.Enrollments
                                .Aggregate(true, (current, enrollment) =>
                                                 current && context.People.OfType<Student>()
                                                                .Any(n => n.PersonId == enrollment.StudentId));
                            if (!enrollmentsValid)
                            {
                                returnList.Add("Cannot update, some enrollment does not have valid student.");
                                returnList.Add(item.CourseId);
                                return returnList;
                            }
                            // save changes
                            context.Courses.ApplyChanges(item);
                            context.SaveChanges();
                            break;
                    }
                }
                returnList.Add(string.Empty);
                returnList.Add(item.CourseId);
            }
            catch (OptimisticConcurrencyException)
            {
                var errorMessage = "Course " + item.CourseId + " was modified by another user. " +
                                   "Refresh that item before reapply your changes.";
                returnList.Add(errorMessage);
                returnList.Add(item.CourseId);
            }
            catch (Exception ex)
            {
                Exception exception = ex;
                while (exception.InnerException != null)
                {
                    exception = exception.InnerException;
                }
                var errorMessage = "Course " + item.CourseId + " has error: " + exception.Message;
                returnList.Add(errorMessage);
                returnList.Add(item.CourseId);
            }
            return returnList;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Weidong Shen
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 20 Feb 2012
Article Copyright 2011 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid