// Mappings from ViSi model to FHIR resources

        public IResource MapPatient(ViSiPatient source)
        {
            var patient = new Patient
            {
                Id = source.Id.ToString(),
                BirthDateElement = new Date(source.DateOfBirth.ToString("yyyy-MM-dd"))
            };

            patient.Identifier.Add(new Identifier("http://mycompany.org/patientnumber", source.PatientNumber));
            patient.Name.Add(new HumanName().WithGiven(source.FirstName).AndFamily(source.FamilyName));
            if (source.EmailAddress != null)
            {
                patient.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Email, ContactPoint.ContactPointUse.Home, source.EmailAddress));
            }
            return(patient.AsIResource());
        }
        // Mappings from ViSi model to FHIR resources

        public IResource MapPatient(ViSiPatient source)
        {
            var patient = new Patient
            {
                Id        = source.Id.ToString(),
                BirthDate = source.DateOfBirth.ToFhirDate() //Time part is not converted here, since the Birthdate is of type date
                                                            //If you have it present in the source, and want to communicate it, you
                                                            //need to add a birthtime extension.
            };

            patient.Identifier.Add(new Identifier("http://mycompany.org/patientnumber", source.PatientNumber));
            patient.Name.Add(new HumanName().WithGiven(source.FirstName).AndFamily(source.FamilyName));
            if (source.EmailAddress != null)
            {
                patient.Telecom.Add(new ContactPoint(ContactPoint.ContactPointSystem.Email, ContactPoint.ContactPointUse.Home, source.EmailAddress));
            }
            return(patient.ToIResource());
        }
        // Mappings from FHIR resources to ViSi model

        public ViSiPatient MapViSiPatient(IResource source)
        {
            var fhirPatient = source.ToPoco <Patient>();
            var visiPatient = new ViSiPatient();

            if (source.Id != null)
            {
                visiPatient.Id = int.Parse(source.Id);
            }

            // This code expects all of the values for the required fields of the database to be present
            // and as such is not too robust
            visiPatient.PatientNumber = fhirPatient.Identifier.FirstOrDefault(i => (i.System == "http://mycompany.org/patientnumber"))?.Value;
            visiPatient.FirstName     = fhirPatient.Name.FirstOrDefault()?.Given?.FirstOrDefault();
            visiPatient.FamilyName    = fhirPatient.Name.FirstOrDefault()?.Family;
            visiPatient.DateOfBirth   = Convert.ToDateTime(fhirPatient.BirthDate);
            visiPatient.EmailAddress  = fhirPatient.Telecom.FirstOrDefault(t => (t.System == ContactPoint.ContactPointSystem.Email))?.Value;

            return(visiPatient);
        }