public class CoursesDataContext : IUpdatable
{
#region Members
private static List<Course> _courses;
#endregion
#region Properties
/// <summary>
/// Return the courses in a IQueryable format
/// </summary>
public IQueryable<Course> Courses
{
get
{
return _courses.AsQueryable();
}
}
#endregion
#region Ctor
/// <summary>
/// Construct a new CoursesDataContext object
/// </summary>
static CoursesDataContext()
{
_courses = Course.GetCourses();
}
#endregion
#region IUpdatable Members
public object CreateResource(string containerName,
string fullTypeName)
{
// create the object using reflection
var objType = Type.GetType(fullTypeName);
var resourceToAdd = Activator.CreateInstance(objType);
// add the course to the courses in-memory list
_courses.Add((Course)resourceToAdd);
return resourceToAdd;
}
public void DeleteResource(object targetResource)
{
// remove the course form the courses list
_courses.Remove((Course)targetResource);
}
public object GetResource(IQueryable query, string fullTypeName)
{
object result = null;
var enumerator = query.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current != null)
{
result = enumerator.Current;
break;
}
}
if (fullTypeName != null &&
!fullTypeName.Equals(result.GetType().FullName))
{
throw new DataServiceException();
}
return result;
}
public object GetValue(object targetResource, string propertyName)
{
// get the property info using reflection
var targetType = targetResource.GetType();
var targetProperty = targetType.GetProperty(propertyName);
// retrun the value of the property
return targetProperty.GetValue(targetResource, null);
}
public void SetValue(object targetResource,
string propertyName, object propertyValue)
{
// get the property info using reflection
Type targetType = targetResource.GetType();
PropertyInfo property = targetType.GetProperty(propertyName);
// set the property value
property.SetValue(targetResource, propertyValue, null);
}
public object ResolveResource(object resource)
{
// nothing to do just return the resource
return resource;
}
public void SaveChanges()
{
// object in memory - do nothing
}
public void SetReference(object targetResource,
string propertyName, object propertyValue)
{
throw new NotImplementedException();
}
public object ResetResource(object resource)
{
throw new NotImplementedException();
}
public void ClearChanges()
{
throw new NotImplementedException();
}
public void AddReferenceToCollection(object targetResource,
string propertyName, object resourceToBeAdded)
{
throw new NotImplementedException();
}
public void RemoveReferenceFromCollection(object targetResource,
string propertyName, object resourceToBeRemoved)
{
throw new NotImplementedException();
}
#endregion
}
class Program
{
static void Main(string[] args)
{
// build the proxy
var proxy = new CoursesDataContext(
new Uri("http://localhost:4205/CoursesService.svc/"));
proxy.MergeOption = MergeOption.AppendOnly;
var CGCourse = new Course
{
CourseID = 4,
Creadit = 5,
Days = 3,
Title = "Computer Graphics"
};
proxy.AddToCourses(CGCourse);
proxy.SaveChanges();
PrintCoursesToConsule(proxy);
CGCourse.Creadit = 3;
proxy.UpdateObject(CGCourse);
proxy.SaveChanges();
PrintCoursesToConsule(proxy);
proxy.DeleteObject(CGCourse);
proxy.SaveChanges();
PrintCoursesToConsule(proxy);
Console.Read();
}
private static void PrintCoursesToConsule(CoursesDataContext proxy)
{
var courses = from course in proxy.Courses
select course;
foreach (Course course in courses)
{
Console.WriteLine(
"Course name: {0}, Course duration: {1}, Course credit: {2}",
course.Title,
course.Days,
course.Creadit
);
}
Console.WriteLine();
}
}