コード例 #1
0
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="personDetails">
        /// The person details.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public void Save(PersonDetails personDetails)
        {
            var personBase = Mapper.Instance.Map <PersonDetails, PersonBase>(personDetails);

            using (var dbContext = new StaplesDatabaseContext())
            {
                if (dbContext.Persons.Any(PersonBase.Comparer(personBase)))
                {
                    // TODO some message ?
                    return;
                }

                dbContext.Persons.Add(personBase);
                dbContext.SaveChanges();
            }
        }
コード例 #2
0
ファイル: XmlStore.cs プロジェクト: Mamelski/ASP.NET-Staples
        /// <summary>
        /// The save.
        /// </summary>
        /// <param name="personDetails">
        /// The person details.
        /// </param>
        public void Save(PersonDetails personDetails)
        {
            // TODO maybe some kind of append would be better instead of deserializing and serializing (no time :(((( ) ?
            var allPersons = new List <PersonBase>();

            if (!File.Exists(fileName))
            {
                File.Create(fileName);
            }

            try
            {
                using (var reader = new StreamReader(fileName))
                {
                    var deserializer = new XmlSerializer(typeof(List <PersonBase>));
                    allPersons = (List <PersonBase>)deserializer.Deserialize(reader);
                }
            }
            catch (InvalidOperationException)
            {
                // TODO this is bad, but for now do nothing, initial empty, can cause this exception
                // TODO this should not be handled like this....
            }

            var personBase = Mapper.Instance.Map <PersonDetails, PersonBase>(personDetails);

            if (allPersons.Any(PersonBase.Comparer(personBase)))
            {
                // TODO some message/exception/log ?
                return;
            }

            allPersons.Add(personBase);

            using (var writer = new StreamWriter(fileName))
            {
                var serializer = new XmlSerializer(typeof(List <PersonBase>));
                serializer.Serialize(writer, allPersons);
            }
        }