Skip to content

Instantly share code, notes, and snippets.

@azterix
Created January 8, 2018 16:24
Show Gist options
  • Save azterix/4298aef6eb7f5f43c42caf3856568142 to your computer and use it in GitHub Desktop.
Save azterix/4298aef6eb7f5f43c42caf3856568142 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
namespace jsl.api.Models.Scheduler {
public partial class GroupActivity {
public GroupActivity() {
Tags = new JoinCollectionFacade<Tag, GroupActivity, GroupActivityTag>(this, GroupActivityTags);
Facilitators = new JoinCollectionFacade<EmployeeFacilitator, GroupActivity, GroupActivityFacilitator>(
this, GroupActivityFacilitators
);
Participants = new JoinCollectionFacade<ParticipationRecord, GroupActivity, GroupActivityParticipant>(
this,
GroupActivityParticipants
);
}
public int Id { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Title { get; set; }
public string Location { get; set; }
public string VolunteerFacilitators { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Modified { get; set; }
public string ModifiedBy { get; set; }
public bool Deleted { get; set; }
[JsonIgnore]
public ICollection<GroupActivityTag> GroupActivityTags { get; } = new List<GroupActivityTag>();
[JsonIgnore]
public ICollection<GroupActivityFacilitator> GroupActivityFacilitators { get; } = new List<GroupActivityFacilitator>();
private ICollection<GroupActivityParticipant> GroupActivityParticipants { get; } = new List<GroupActivityParticipant>();
[NotMapped]
public ICollection<EmployeeFacilitator> Facilitators { get; }
[NotMapped]
public ICollection<Tag> Tags { get; }
[NotMapped]
public ICollection<ParticipationRecord> Participants { get; }
}
public class ParticipationRecord {
public int Id { get; set; }
public int Sequence { get; set; }
public string PatientMRN { get; set; }
public string FullName { get; set; }
public string Unit { get; set; }
public string Participation { get; set; }
public string Note { get; set; }
private ICollection<GroupActivityParticipant> GroupActivityParticipants { get; } = new List<GroupActivityParticipant>();
[NotMapped]
ICollection<GroupActivity> GroupActivities { get; }
public ParticipationRecord() => GroupActivities = new JoinCollectionFacade<GroupActivity, ParticipationRecord, GroupActivityParticipant>(
this,
GroupActivityParticipants
);
}
public class Tag {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Slug { get; set; }
public Tag() => GroupActivities = new JoinCollectionFacade<GroupActivity, Tag, GroupActivityTag>(
this,
GroupActivityTags
);
[JsonIgnore]
private ICollection<GroupActivityTag> GroupActivityTags { get; } = new List<GroupActivityTag>();
[JsonIgnore]
[NotMapped]
public IEnumerable<GroupActivity> GroupActivities { get; }
}
public class EmployeeFacilitator {
public int Id { get; set; }
public string EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string Type { get; set; }
public EmployeeFacilitator() => GroupActivities = new JoinCollectionFacade<GroupActivity, EmployeeFacilitator, GroupActivityFacilitator>(
this,
GroupActivityFacilitators
);
private ICollection<GroupActivityFacilitator> GroupActivityFacilitators { get; } = new List<GroupActivityFacilitator>();
[NotMapped]
ICollection<GroupActivity> GroupActivities { get; }
}
#region IJoinEntities
public class GroupActivityTag : IJoinEntity<GroupActivity>, IJoinEntity<Tag> {
public int TagId { get; set; }
public Tag Tag { get; set; }
Tag IJoinEntity<Tag>.Navigation {
get => Tag;
set => Tag = value;
}
public int GroupActivityId { get; set; }
public GroupActivity GroupActivity { get; set; }
GroupActivity IJoinEntity<GroupActivity>.Navigation {
get => GroupActivity;
set => GroupActivity = value;
}
}
public class GroupActivityParticipant : IJoinEntity<GroupActivity>, IJoinEntity<ParticipationRecord> {
public int GroupActivityId { get; set; }
public GroupActivity GroupActivity { get; set; }
GroupActivity IJoinEntity<GroupActivity>.Navigation {
get => GroupActivity;
set => GroupActivity = value;
}
public int ParticipationRecordId { get; set; }
public ParticipationRecord ParticipationRecord { get; set; }
ParticipationRecord IJoinEntity<ParticipationRecord>.Navigation {
get => ParticipationRecord;
set => ParticipationRecord = value;
}
}
public class GroupActivityFacilitator : IJoinEntity<GroupActivity>, IJoinEntity<EmployeeFacilitator> {
public int EmployeeFacilitatorId { get; set; }
public EmployeeFacilitator EmployeeFacilitator { get; set; }
EmployeeFacilitator IJoinEntity<EmployeeFacilitator>.Navigation {
get => EmployeeFacilitator;
set => EmployeeFacilitator = value;
}
public int GroupActivityId { get; set; }
public GroupActivity GroupActivity { get; set; }
GroupActivity IJoinEntity<GroupActivity>.Navigation {
get => GroupActivity;
set => GroupActivity = value;
}
}
#endregion
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public interface IJoinEntity<TEntity> {
TEntity Navigation { get; set; }
}
public class JoinCollectionFacade<TEntity, TOtherEntity, TJoinEntity>
: ICollection<TEntity>
where TJoinEntity : IJoinEntity<TEntity>, IJoinEntity<TOtherEntity>, new() {
private readonly TOtherEntity _ownerEntity;
private readonly ICollection<TJoinEntity> _collection;
public JoinCollectionFacade(
TOtherEntity ownerEntity,
ICollection<TJoinEntity> collection) {
_ownerEntity = ownerEntity;
_collection = collection;
}
public IEnumerator<TEntity> GetEnumerator() => _collection.Select(e =>((IJoinEntity<TEntity>) e).Navigation).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public void Add(TEntity item) {
var entity = new TJoinEntity();
((IJoinEntity<TEntity>) entity).Navigation = item;
((IJoinEntity<TOtherEntity>) entity).Navigation = _ownerEntity;
_collection.Add(entity);
}
public void Clear() => _collection.Clear();
public bool Contains(TEntity item) => _collection.Any(e => Equals(item, e));
public void CopyTo(TEntity[] array, int arrayIndex) => this.ToList().CopyTo(array, arrayIndex);
public bool Remove(TEntity item) => _collection.Remove(
_collection.FirstOrDefault(e => Equals(item, e)));
public int Count => _collection.Count;
public bool IsReadOnly => _collection.IsReadOnly;
private static bool Equals(TEntity item, TJoinEntity e) => Equals(((IJoinEntity<TEntity>) e).Navigation, item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment