Esempio n. 1
0
        /// <summary>
        /// Add Core.Person items to Navigation property of a resource.
        /// </summary>
        /// <param name="context">ZentityContext to search a person in database</param>
        /// <param name="contacts">Navigation property of resource to add persons</param>
        /// <param name="authors">List of persons to add to navigation property.</param>
        protected static void AddPersons(ZentityContext context, EntityCollection <Contact> contacts, IEnumerable <Person> authors)
        {
            foreach (Person author in authors)
            {
                if (!string.IsNullOrEmpty(author.Title))
                {
                    Match nameMatch = Regex.Match(author.Title, AtomPubConstants.PersonNamePattern, RegexOptions.IgnoreCase);
                    author.FirstName  = nameMatch.Groups[AtomPubConstants.FirstName].Value;
                    author.MiddleName = nameMatch.Groups[AtomPubConstants.MiddleName].Value;
                    author.LastName   = nameMatch.Groups[AtomPubConstants.LastName].Value;
                }

                System.Linq.Expressions.Expression filter = null;
                ParameterExpression param = System.Linq.Expressions.Expression.Parameter(typeof(Person), "person");

                if (!string.IsNullOrEmpty(author.Uri))
                {
                    AtomPubHelper.GeneratePersonFilterExpression("Uri", author.Uri, param, ref filter);
                }
                else if (!string.IsNullOrEmpty(author.Email))
                {
                    AtomPubHelper.GeneratePersonFilterExpression("Email", author.Email, param, ref filter);
                }
                else
                {
                    AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.FirstName, author.FirstName, param, ref filter);
                    AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.MiddleName, author.MiddleName, param, ref filter);
                    AtomPubHelper.GeneratePersonFilterExpression(AtomPubConstants.LastName, author.LastName, param, ref filter);
                }

                if (null == filter)
                {
                    continue;
                }

                Expression <Func <Person, bool> > predicate = System.Linq.Expressions.Expression.Lambda(filter, param) as Expression <Func <Person, bool> >;

                Person contact = context.People().FirstOrDefault(predicate);

                if (null == contact)
                {
                    contact = author;
                }

                contacts.Add(contact);
            }
        }