Base solution for your next web application

Activities of "sayram"

I have a CategoryService I'm using a method like below

public IEnumerable<Category> GetCategories(ContentState state)
        {
            var categories = _categoryRepository.GetAll().Where(c => c.CategoryState == state.ToString());

            //var categories = _categoryRepository.GetAllList(c => c.CategoryState == state.ToString());
            return categories;
        }

this is the AddCategory method in controller

public ActionResult AddCategory()
        {
            var categories = _categoryService.GetCategories(ContentState.Published);
            var viewModel = new CreateCategoryViewModel
            {
                Categories = categories.ToSelectListItems(-1),
            };

            return View(viewModel);
        }

Btw this is the ContentState

public enum ContentState
    {
        Published,
        Unpublished,
        Trashed,
        Pending
    }

and thi is the Category entity

public partial class Category : Entity
    {
        public Category()
        {
            this.SubCategories = new List<Category>();
            this.Videos = new List<Video>();
        }

        public int? ParentId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string MetaKeys { get; set; }
        public string MetaDescription { get; set; }
        public string CategoryState { get; set; }
        public virtual ICollection<Category> SubCategories { get; set; }
        public virtual Category ParentCategory { get; set; }
    }

Viewmodel

public class CreateCategoryViewModel 
    {
        public int Id { get; set; }
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string MetaKeys { get; set; }
        public string MetaDescription { get; set; }
        public string CategoryState { get; set; }
        public string Button { get; set; }
        //public string ActionMessage { get; set; }

        public IEnumerable<SelectListItem> Categories { get; set; }

    }

this is the where i get the error

@helper PrintCategories(dynamic categories)
{
    foreach (var item in categories)
    {
        @item.Title
        <br />
        var subCategories = item.SubCategories;
        if (subCategories != null && subCategories.Count > 0)
        {
            PrintCategories(subCategories);
        }
    }
}
                            <div class="form-group">
                                <div class="col-lg-10">
                                    @PrintCategories(Model.Categories)
                                </div>
                            </div>

so when im trying to access navigation property i get this error

The operation cannot be completed because the DbContext has been disposed. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.

Stack Trace

[InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.]
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +762
   System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() +28
   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator() +52
   ASP.<>c__DisplayClassd.<PrintCategories>b__c(TextWriter __razor_helper_writer) in c:\Users\saYRam\Documents\Visual Studio 2013\Projects\Index Projects\Index.Web\Areas\Administrator\Views\Category\AddCategory.cshtml:82
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +10
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +80
   ASP._Page_Areas_Administrator_Views_Category_AddCategory_cshtml.Execute() in c:\Users\saYRam\Documents\Visual Studio 2013\Projects\Index Projects\Index.Web\Areas\Administrator\Views\Category\AddCategory.cshtml:71
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c() +173
   System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.&lt;BeginExecute&gt;b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9723757
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

I think its related Lazy Loading which is i realy scared when i hear its name. I have no idea how can i get rid of this.

In my categoryService im adding category entity with this codes.

    public void CreateCategory(Category entity)
    {
       _categoryRepository.Insert(entity);
    }

I need the Id number of the inserted entity. How to get it? Ty.

I dont want to use GetCategoryByTitle or something like that because there can be same named categories.

K. I'm trying to build my web application. It's a normal Mvc web application.

Well i created a model named Category. here it is.

public class Category : Entity<int>, ISoftDelete, IPassivable
    {
        public Category()
        {
            this.SubCategories = new List<Category>();
            IsActive = true;
            IsDeleted = false;
        }

        [ForeignKey("ParentId")]
        public int? ParentId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool IsActive { get; set; }
        public bool IsDeleted { get; set; }
        public virtual ICollection<Category> SubCategories { get; set; }
        public virtual Category ParentCategory { get; set; }
    }

well i created ICategoryService interface and CategoryService class.

public interface ICategoryService : IApplicationService
    {
        GetCategoryOutput GetCategories(GetCategoryInput input);
        void UpdateCategory(UpdateCategoryInput input);
        void CreateCategory(CreateCategoryInput input);
    }
public class CategoryService : ApplicationService, ICategoryService
    {
        private readonly  ICategoryRepository _categoryRepository;
        private readonly ICategoryService _categoryService;
        
        public CategoryService(ICategoryService categoryService, ICategoryRepository categoryRepository)
        {
            _categoryService = categoryService;
            _categoryRepository = categoryRepository;
        }

        public GetCategoryOutput GetCategories(GetCategoryInput input)
        {
            var categories = _categoryRepository.GetCategories(input.IsActive, input.IsDeleted);

            return new GetCategoryOutput
            {
                Categories = Mapper.Map<List<CategoryDto>>(categories)
            };
        }

        public void CreateCategory(CreateCategoryInput input)
        {
            Logger.Info("Creating a category for input: " + input);

            //Creating a new category with given input's parametes
            var category = new Category
            {
                ParentId = input.ParentId,
                Title = input.Title,
                Description = input.Description
            };

            if (input.ParentId.HasValue)
            {
                category.ParentCategory = _categoryRepository.Load((input.ParentId.Value));
            }

            _categoryRepository.Insert((category));
        }


        public void UpdateCategory(UpdateCategoryInput input)
        {
            Logger.Info("Updatinga category for input: " + input);

            var category = _categoryRepository.Get(input.CategoryId);


            if (input.ParentId.HasValue)
            {
                category.ParentCategory = _categoryRepository.Load((input.ParentId.Value));
            }

            //We even do not call Update method of the repository.
            //Because an application service method is a 'unit of work' scope as default.
            //ABP automatically saves all changes when a 'unit of work' scope ends (without any exception).
        }
    }

I've added an Area named Administrator and a Controller named CategoryController.

public class CategoryController : BisahneControllerBase
    {
        private ICategoryService _categoryService;

        public CategoryController(ICategoryService categoryService)
        {
            _categoryService = categoryService;
        }

        //
        // GET: /Administrator/Category/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(CategoryDto categoryDto)
        {
            return View();
        }

	}

when i run the application and browse the Administrator/Category/Create i'm getting an error like below

Can't create component 'Bisahne.Categories.CategoryService' as it has dependencies to be satisfied.

'Bisahne.Categories.CategoryService' is waiting for the following dependencies:

  • Service 'Bisahne.Categories.ICategoryService' which points back to the component itself. A dependency cannot be satisfied by the component itself, did you forget to register other components for this service?

Do i have to register something anywhere?

Showing 11 to 13 of 13 entries