public IList<Profile> SearchProfiles(ProfileFilter filter)
 {
     return Repository.SearchProfiles(filter);
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        private IQuery BuildQuery(ISession session, ProfileFilter filter)
        {
            StringBuilder queryString = new StringBuilder();
            IDictionary<string, string> parameters = new Dictionary<string, string>();

            queryString.Append("from Profile p where ");
            bool first = true;

            if (!string.IsNullOrEmpty(filter.Username))
            {
                if (!first)
                {
                    queryString.Append("and ");
                }
                queryString.Append("p.Username like :user");
                parameters["user"] = string.Format("%{0}%", filter.Username);
                first = false;
            }

            if (!string.IsNullOrEmpty(filter.Name))
            {
                if (!first)
                {
                    queryString.Append("and ");
                }
                queryString.Append("p.Name like :name");
                parameters["name"] = string.Format("%{0}%", filter.Name);
                first = false;
            }

            if (!string.IsNullOrEmpty(filter.Surname))
            {
                if (!first)
                {
                    queryString.Append(" and ");
                }
                queryString.Append("p.Surname like :surname");
                parameters["surname"] = string.Format("%{0}%", filter.Surname);
                first = false;
            }

            if (!string.IsNullOrEmpty(filter.Email))
            {
                if (!first)
                {
                    queryString.Append(" and ");
                }
                queryString.Append("p.Email like :email");
                parameters["email"] = string.Format("%{0}%", filter.Email);
                first = false;
            }

            if (!string.IsNullOrEmpty(filter.Street))
            {
                if (!first)
                {
                    queryString.Append(" and ");
                }
                queryString.Append("p.Address.Street like :street");
                parameters["street"] = string.Format("%{0}%", filter.Street);
                first = false;
            }

            if (!string.IsNullOrEmpty(filter.City))
            {
                if (!first)
                {
                    queryString.Append(" and ");
                }
                queryString.Append("p.Address.City like :city");
                parameters["city"] = string.Format("%{0}%", filter.City);
                first = false;
            }

            // Create query
            IQuery query = session.CreateQuery(queryString.ToString());

            foreach (string key in parameters.Keys)
            {
                query.SetString(key, parameters[key]);
            }

            return query;
        }
        public void SearchProfilesTest()
        {
            IProfileRepository repository = new ProfileRepository();

            // Search profiles with username like "bat"
            ProfileFilter filter = new ProfileFilter()
            {
                Username = "******"
            };

            IList<Profile> searchResult = repository.SearchProfiles(filter);

            Assert.AreEqual<int>(1, searchResult.Count);
            Assert.AreEqual<string>("spiderman", searchResult[0].Username);
            Assert.AreEqual<string>("Peter", searchResult[0].Name);

            // Search profiles with email like "gmail"
            filter = new ProfileFilter()
            {
                Email = "gmail"
            };

            searchResult = repository.SearchProfiles(filter);

            Assert.AreEqual<int>(4, searchResult.Count);

            // Search profiles with city like "New"
            filter = new ProfileFilter()
            {
                City = "New"
            };

            searchResult = repository.SearchProfiles(filter);

            Assert.AreEqual<int>(2, searchResult.Count);
            Assert.AreEqual<string>("New York", searchResult[0].Address.City);
            Assert.AreEqual<string>("New York", searchResult[1].Address.City);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public IList<Profile> SearchProfiles(ProfileFilter filter)
        {
            IList<Profile> searchResult;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                IQuery query = BuildQuery(session, filter);
                searchResult = query.List<Profile>();
            }

            return searchResult;
        }