Skip to content

Instantly share code, notes, and snippets.

@tuesd4y
Created December 1, 2016 09:07
Show Gist options
  • Save tuesd4y/43649224d50272ddde8c9fd5723c0188 to your computer and use it in GitHub Desktop.
Save tuesd4y/43649224d50272ddde8c9fd5723c0188 to your computer and use it in GitHub Desktop.
prhsc
DbContext
=========
class SchoolDb : DbContext {
public DbSet<Class> Classes { get; set; }
public DbSet<Person> Persons { get; set; }
public SchoolDb() : base("Data Source=(localdb)\\schooldb;Initial Catalog=SchoolDb;Integrated Security=True") {
System.Data.Entity.Database.SetInitializer(new SchoolDbInitializer());
}
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
class SchoolDbInitializer : DropCreateDatabaseAlways<SchoolDb> {
protected override void Seed(SchoolDb context) {
Teacher obm = new Teacher { PersonId = 1, Name = "Obermüller", Room = "131" };
new List<Class> {
new Class { Name = "5AHIF",
Pupils = new Pupil[] { new Pupil { PersonId = 4, Name = "MET", CatalogueNumber = 10 } }
Teachers = new Teacher[] { obm, rucki, kain }
},
new Class { Name = "2AHITM" }
}.ForEach(c => context.Classes.Add(c));
context.SaveChanges();
}
}
SMUnitOfWork
============
public class SMUnitOfWork : IDisposable {
[Inject] public IRepository<Teacher> RepTeacher { get; set; }
public void SaveChanges() { Injector.db.SaveChanges(); }
public void Dispose() { GC.SuppressFinalize(this); }
}
Injector
========
static class Injector {
private static IKernel kernel = new StandardKernel();
public static SchoolContext db = new SchoolContext();
static Injector() {
kernel.Bind<IRepository<Teacher>>()
.To<UowRepository<Teacher>>()
.WithConstructorArgument("db", db);
kernel.Bind<ISchoolBusiness>().To<SchoolBusiness>();
}
public static T Get<T>() { return kernel.Get<T>(); }
}
ViewModelBase
=============
public abstract class ViewModelBase : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string properties) {
foreach (var prop in properties.Split(','))
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
Business
========
public interface ISchoolBusiness {
IEnumerable<Pupil> GetPupilsFromForm(Form form);
IEnumerable<Teacher> GetTeachersFromForm(Form form);
IEnumerable<Form> GetFormsFromTeacher(Teacher teacher);
void AddPupilToForm(Pupil pupil, Form form);
void UpdatePupil(Pupil pupil);
void DeletePupil(Pupil pupil);
void AddTeacherToForm(Teacher teacher, Form form);
void RemoveTeacherFromForm(Teacher teacher, Form form);
}
public class SchoolBusiness : ISchoolBusiness {
public IEnumerable<Pupil> GetPupilsFromForm(Form form) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
return form == null ? sm.RepPupil.Get() : sm.RepPupil.Get(p => p.FormId == form.FormId);
}
}
public IEnumerable<Teacher> GetTeachersFromForm(Form form) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
return form == null ? sm.RepTeacher.Get() : sm.RepTeacher.Get(t => t.Forms.Any(f => f.FormId == form.FormId));
}
}
public IEnumerable<Form> GetFormsFromTeacher(Teacher teacher) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
return teacher == null ? sm.RepForm.Get() : sm.RepForm.Get(f => f.Teachers.Any(t => t.PersonId == teacher.PersonId));
}
}
public void AddPupilToForm(Pupil pupil, Form form) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
form = sm.RepForm.GetByKey(form.FormId);
pupil.Form = form;
sm.RepPupil.Create(pupil);
sm.SaveChanges();
}
}
public void AddTeacherToForm(Teacher teacher, Form form) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
teacher = sm.RepTeacher.GetByKey(teacher.PersonId);
form = sm.RepForm.GetByKey(form.FormId);
teacher.Forms.Add(form);
sm.SaveChanges();
}
}
public void RemoveTeacherFromForm(Teacher teacher, Form form) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
teacher = sm.RepTeacher.GetByKey(teacher.PersonId);
form = sm.RepForm.GetByKey(form.FormId);
teacher.Forms.Remove(form);
form.Teachers.Remove(teacher);
sm.SaveChanges();
}
}
public void UpdatePupil(Pupil pupil) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
string newName = pupil.Name;
pupil = sm.RepPupil.GetByKey(pupil.PersonId);
pupil.Name = newName;
sm.SaveChanges();
}
}
public void DeletePupil(Pupil pupil) {
using (SMUnitOfWork sm = Injector.Get<SMUnitOfWork>()) {
pupil = sm.RepPupil.GetByKey(pupil.PersonId);
sm.RepPupil.Delete(pupil);
sm.SaveChanges();
}
}
}
MainWindow
==========
<Window x:Class="EfDemo.MainWindow"
xmlns:vm="clr-namespace:EfDemo.ViewModels"
<Window.DataContext>
<vm:ViewModelMain></vm:ViewModelMain>
</Window.DataContext>
<DockPanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="5"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<local:UserControlProjects DataContext="{Binding ViewModelProjects}"></local:UserControlProjects>
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch"/>
</Grid>
</DockPanel>
</Window>
UserControl
===========
<UserControl x:Class="EfDemo.UserControlProjects"
xmlns:vm="clr-namespace:EfDemo.ViewModels"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
local:DialogBehaviour.DialogBox="{Binding DialogBox}">
<StackPanel>
<ListBox DisplayMemberPath="Name" SelectedItem="{Binding SelectedFreeEmployee}" ItemsSource="{Binding FreeEmployees}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding EditEmployee}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding AddEmployee}">Hinzufügen</Button>
</StackPanel>
</StackPanel>
</UserControl>
ViewModelClasses
================
public class ViewModelClasses : ViewModelBase {
public ISchoolBusiness sb = Injector.Get<ISchoolBusiness>();
public IEnumerable<Class> Classes => sb.GetClasses();
public IEnumerable<Pupil> Pupils => sb.GetPupils(SelectedClass);
Class selectedClass;
public Class SelectedClass {
get { return selectedClass; }
set { selectedClass = value; OnPropertyChanged("Pupils,Teachers,FreeTeachers"); }
}
public void UpdateSelectedPupil() { OnPropertyChanged("Pupils"); }
public ICommand AddTeacher => new RelayCommand(
obj => { sb.AddPerson(SelectedFreeTeacher, SelectedClass); OnPropertyChanged("FreeTeachers,Teachers"); },
obj => SelectedClass != null && SelectedFreeTeacher != null);
}
ViewModelMain
=============
public class ViewModelMain : ViewModelBase {
ViewModelPupil vmPupil = new ViewModelPupil();
public ViewModelPupil ViewModelPupil {
get { return vmPupil; }
set { vmPupil = value; OnPropertyChanged("ViewModelPupil"); }
}
ViewModelForm vmForm = new ViewModelForm();
public ViewModelForm ViewModelForm => vmForm;
public MainViewModel() {
vmForm.PropertyChanged += VmForm_PropertyChanged;
}
private void VmForm_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "SelectedForm" || e.PropertyName == "SelectedPupil")
ViewModelPupil = new ViewModelPupil(ViewModelForm.SelectedPupil, ViewModelForm.SelectedForm, mode => ViewModelForm.UpdatePupils(mode));
}
}
ViewModelPupil
==============
public class ViewModelPupil : BaseViewModel {
ISchoolBusiness sb = Injector.Get<ISchoolBusiness>();
public Form selectedForm;
Pupil newPupil;
Action<int> update;
public ViewModelPupil(Pupil pupil = null, Form form = null, Action<int> update = null) {
selectedForm = form;
newPupil = pupil != null ? pupil : new Pupil { Name = "" };
this.update = update;
}
public string NewName {
get { return newPupil.Name; }
set { newPupil.Name = value; OnPropertyChanged("NewName"); }
}
public ICommand AddPupil => new RelayCommand(
obj => { sb.AddPupilToForm(newPupil, selectedForm); update?.Invoke(177881); },
obj => selectedForm != null && newPupil.Name.Length > 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment