Skip to content

Instantly share code, notes, and snippets.

@KodrAus
Created July 10, 2016 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KodrAus/6b375b76707d35a4dc839f81ded8175f to your computer and use it in GitHub Desktop.
Save KodrAus/6b375b76707d35a4dc839f81ded8175f to your computer and use it in GitHub Desktop.
Domain with Explicitly-typed Child Collections
/*
Entity Framework has a problem: there is 1 object graph to rule them all, and that object graph is available
to anybody, whether or not they actually want/need it. This makes things ambiguous and leads to errors and
crap code.
It's saner to be able to constrain the data you read/write to exactly what you expect to work with. But
this fragments your entities. The idea here is to try and get the best of both worlds; a single base definition
of a given entity, but only exposing the bits that are relevant to a particular projection.
It also has a cheeky workaround for defaulting generics in C# :)
*/
using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
//Teacher
interface ITeacher { }
class TeacherBase<TStudent, TClass> : ITeacher
where TStudent : IStudent
where TClass : IClass
{
public TeacherBase(Guid id)
{
Id = id;
}
public Guid Id { get; private set; }
public void UpdateName(string first, string last)
{
FirstName = first;
LastName = last;
}
protected string FirstName { get; private set; }
protected string LastName { get; private set; }
protected ICollection<TStudent> Students { get; private set; }
protected ICollection<TClass> Classes { get; private set; }
}
class NamedTeacher : TeacherBase<NamedStudent, _>
{
public NamedTeacher(Guid id, string first, string last, params NamedStudent[] students) : base(id)
{
FirstName = first;
LastName = last;
Students = students;
}
public new string FirstName { get; }
public new string LastName { get; }
public new ICollection<NamedStudent> Students { get; }
}
//Class
interface IClass { }
class ClassBase : IClass
{
protected string Name { get; private set; }
}
//Student
interface IStudent { }
class StudentBase : IStudent
{
public StudentBase(Guid id)
{
Id = id;
}
public Guid Id { get; private set; }
protected string FirstName { get; private set; }
protected string LastName { get; private set; }
}
class NamedStudent : StudentBase
{
public NamedStudent(Guid id, string first, string last) : base(id)
{
FirstName = first;
LastName = last;
}
public new string FirstName { get; }
public new string LastName { get; }
public override string ToString()
{
return string.Concat(FirstName, " ", LastName);
}
}
//Default generic :)
partial class _ : IClass, IStudent, ITeacher
{
}
public class Program
{
public static void Main(string[] args)
{
var teacher = new NamedTeacher(
Guid.NewGuid(),
"Mr", "Shanks",
new NamedStudent(Guid.NewGuid(), "Amandra", "Hugandkiss"),
new NamedStudent(Guid.NewGuid(), "Jock", "Strap")
);
foreach (var student in teacher.Students)
{
Console.WriteLine(student.ToString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment