public IForumThread Select()
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: Start", ForumThread.Id));

            var serviceContext = Dependencies.GetServiceContext();

            var entity = SelectEntity(serviceContext);

            if (entity == null)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: Not Found", ForumThread.Id));

                return(null);
            }

            var securityProvider = Dependencies.GetSecurityProvider();

            if (!securityProvider.TryAssert(serviceContext, entity, CrmEntityRight.Read))
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: Not Found", ForumThread.Id));

                return(null);
            }

            var viewEntity      = new PortalViewEntity(serviceContext, entity, securityProvider, Dependencies.GetUrlProvider());
            var website         = Dependencies.GetWebsite();
            var threadInfo      = serviceContext.FetchForumThreadInfo(entity.Id, website.Id);
            var counterStrategy = Dependencies.GetCounterStrategy();

            var thread = new ForumThread(
                entity,
                viewEntity,
                threadInfo,
                // Only lazily get the post count, because it's unlikely to be used in the common case.
                // SelectPostCount will generally be used instead.
                () => counterStrategy.GetForumThreadPostCount(serviceContext, entity));

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: End", ForumThread.Id));

            if (FeatureCheckHelper.IsFeatureEnabled(FeatureNames.TelemetryFeatureUsage))
            {
                PortalFeatureTrace.TraceInstance.LogFeatureUsage(FeatureTraceCategory.Forum, HttpContext.Current, "read_forum_thread", 1, thread.Entity.ToEntityReference(), "read");
            }

            return(thread);
        }
        public IForumThread GetThread(Guid threadId)
        {
            var serviceContext = Dependencies.GetServiceContext();

            var entity = serviceContext.CreateQuery("adx_communityforumthread")
                         .FirstOrDefault(ft => ft.GetAttributeValue <Guid>("adx_communityforumthreadid") == threadId);

            if (entity == null)
            {
                return(null);
            }

            var securityProvider = Dependencies.GetSecurityProvider();

            if (!securityProvider.TryAssert(serviceContext, entity, CrmEntityRight.Read))
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: Not Found", threadId));

                return(null);
            }

            var viewEntity      = new PortalViewEntity(serviceContext, entity, securityProvider, Dependencies.GetUrlProvider());
            var website         = Dependencies.GetWebsite();
            var threadInfo      = serviceContext.FetchForumThreadInfo(entity.Id, website.Id);
            var counterStrategy = Dependencies.GetCounterStrategy();

            var thread = new ForumThread(
                entity,
                viewEntity,
                threadInfo,
                () => counterStrategy.GetForumThreadPostCount(serviceContext, entity),
                Dependencies.GetUrlProvider().GetUrl(serviceContext, entity));

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Thread={0}: End", threadId));

            return(thread);
        }