troubleshooting Question

error on resolving type when using Unity IoC

Avatar of sagir
sagir asked on
.NET ProgrammingC#ASP.NET
4 Comments1 Solution4168 ViewsLast Modified:
Hi,

I am getting an error:
The type name or alias ICategoryService could not be resolved. Please check your configuration file and verify this type name.

When trying to run this code from a website default.aspx.cs file:
protected void Page_Load(object sender, EventArgs e)
{
     var catService = IoCWorker.Resolve<ICategoryrService>();
}

This is the web.config registration, and attached below are the classes involved.
Also I should mention that everything compiles great and I added references to the website for all the projects involved in this.
<unity>
		<typeAliases>
			<!-- Lifetime manager types -->
			<typeAlias alias="singleton" 
                       type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity"/>
			<typeAlias alias="perThread" 
                       type="Microsoft.Practices.Unity.PerThreadLifetimeManager, Microsoft.Practices.Unity"/>
			<typeAlias alias="external" 
                       type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager, Microsoft.Practices.Unity"/>

            <typeAlias alias="ICategoryService"
                       type="XComSolutions.FB.Application.Interfaces.Categories.ICategoryService, XComSolutions.FB.Application.Interfaces.Categories"/>
            <typeAlias alias="CategoryService"
                       type="XComSolutions.FB.Application.Services.Categories.CategoryService, XComSolutions.FB.Application.Services.Categories"/>
            
			<!-- User-defined type aliases -->
			<typeAlias alias="IRepositoryContext" 
                       type="XComSolutions.FB.Domain.Repository.IRepositoryContext, XComSolutions.FB.Domain.Repository"/>
			<typeAlias alias="RepositoryContext" 
                       type="XComSolutions.FB.Repository.EF.RepositoryContext, XComSolutions.FB.Repository.EF"/>
            
			<typeAlias alias="IGenericRepository" 
                       type="XComSolutions.FB.Domain.Repository.IGenericRepository`1, XComSolutions.FB.Domain.Repository"/>
			<typeAlias alias="GenericRepository" 
                       type="XComSolutions.FB.Repository.EF.GenericRepository`1, XComSolutions.FB.Repository.EF"/>
            
            <typeAlias alias="ICategoryRepository" 
                       type="XComSolutions.FB.Domain.Repository.Repositories.Categories.ICategoryRepository, XComSolutions.FB.Domain.Repository.Repositories.Categories"/>
			<typeAlias alias="CategoryRepository" 
                       type="XComSolutions.FB.Repository.EF.Repositories.Categories.CategoryRepository, XComSolutions.FB.Repository.EF.Repositories.Categories"/>
		</typeAliases>
		<containers>
			<container name="containerOne">
				<types>
					<type type="IRepositoryContext" mapTo="RepositoryContext">
						<lifetime type="singleton"/>
					</type>
					<type type="IGenericRepository" mapTo="GenericRepository">
						<lifetime type="singleton"/>
					</type>
					<!-- Categories -->
					<type type="ICategoryService" mapTo="CategoryService">
						<lifetime type="singleton"/>
					</type>
                    <type type="ICategoryRepository" mapTo="CategoryRepository">
                        <lifetime type="singleton"/>
                    </type>
				</types>
			</container>
		</containers>
	</unity>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XComSolutions.FB.Application.Interfaces.Categories;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Model;
using XComSolutions.FB.Domain.Repository.Repositories.Categories;

namespace XComSolutions.FB.Application.Services.Categories
{ 
    public class CategoryService : ICategoryrService
    {
        private ICategoryRepository _categoryRepository;

        public CategoryService(ICategoryRepository categoryRepository)
        {
            _categoryRepository = categoryRepository;
        }

        public List<Category> GetAll()
        {
            return _categoryRepository.GetAll();
        }


        public bool InsertCategory(string Name, int parent)
        {
            try
            {
                Category newCat = new Category();
                newCat.Description = "";
                newCat.Name = Name;
                newCat.ParentCategoryID = parent;
                newCat.Status = BaseStatus.Status.Active;
                newCat.LinkText = "";
                newCat.UpdatedBy = 100;
                newCat.CreatedBy = 100;
                newCat.UpdatedOn = DateTime.Now;
                newCat.CreatedOn = DateTime.Now;

                _categoryRepository.Add(newCat);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Threading;
using System.Web;
using XComSolutions.FB.Domain.Model.Entities;
using XComSolutions.FB.Domain.Model;
using XComSolutions.FB.Domain.Repository;
using XComSolutions.FB.Infrastructure.IoC;

namespace XComSolutions.FB.Repository.EF
{
    public class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity
    {

        /// <summary>
        /// Returns the active object context
        /// </summary>
        private ObjectContext ObjectContext
        {
            get
            {
                return ObjectContextManager.GetObjectContext();
            }
        }

        public T Load(Expression<Func<T, bool>> whereCondition)
        {
            return default(T);
        }

        public IQueryable<T> GetQuery()
        {
            // TODO Check this works with inheritance
            return this.ObjectContext.CreateObjectSet<T>();
        }

        /// <summary>
        /// Provides explicit loading of object properties (EF v4.0 feature)
        /// </summary>
        public void LoadProperty(T entity, Expression<Func<T, object>> selector)
        {
            this.ObjectContext.LoadProperty(entity, selector);
        }

        public List<T> GetAll()
        {
            return GetQuery().ToList();
        }

        public List<T> GetAll(int index, int count)
        {
            return GetQuery().Skip(index).Take(count).ToList();
        }

        public List<T> FindBy(Func<T, bool> expression)
        {
            return GetQuery().Where(expression).ToList();
        }

        public List<T> FindBy(Func<T, bool> expression, int index, int count)
        {
            return GetQuery().Where(expression).Skip(index).Take(count).ToList();
        }

        public void Add(T entity)
        {
            // TODO - check if this works
            this.ObjectContext.AddObject(GetBaseType(typeof(T)).Name.ToString(), entity);
        }

        public void Delete(T entity)
        {
            this.ObjectContext.DeleteObject(entity);
        }

        private static Type GetBaseType(Type type)
        {
            Type baseType = type.BaseType;
            if (baseType != null && baseType != typeof(BaseEntity))
            {
                return GetBaseType(type.BaseType);
            }
            return type;
        }

        private static bool HasBaseType(Type type, out Type baseType)
        {
            Type originalType = type.GetType();
            baseType = GetBaseType(type);
            return baseType != originalType;
        }

    }
}
using System;
using XComSolutions.FB.Domain.Model.Entities.Categories;

namespace XComSolutions.FB.Domain.Repository.Repositories.Categories
{
    public interface ICategoryRepository : IGenericRepository<Category>
    {

    }
}
using System;
using System.Linq;
using System.Linq.Expressions;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Repository.Repositories.Categories;

namespace XComSolutions.FB.Repository.EF.Repositories.Categories
{
    public class CategoryRepository : GenericRepository<XComSolutions.FB.Domain.Model.Entities.Categories.Category>, ICategoryRepository
    {
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XComSolutions.FB.Domain.Model.Entities.Categories;
using XComSolutions.FB.Domain.Repository;

namespace XComSolutions.FB.Application.Interfaces.Categories
{
    public interface ICategoryrService
    {
        List<Category> GetAll();
        bool InsertCategory(string Name, int parent);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using XComSolutions.FB.Domain.Model.Entities;
using XComSolutions.FB.Domain.Model;

namespace XComSolutions.FB.Domain.Repository
{
    public interface IGenericRepository<T> : IRepositoryBase where T : BaseEntity
    {

        /// <summary>
        /// Return strongly typed IQueryable
        /// </summary>
        IQueryable<T> GetQuery();

        /// <summary>
        /// Load entity from the repository (always query store)
        /// </summary>
        /// <typeparam name="T">the entity type to load</typeparam>
        /// <param name="where">where condition</param>
        /// <returns>the loaded entity</returns>
        T Load(Expression<Func<T, bool>> whereCondition);

        /// <summary>
        /// Provides explicit loading of object properties
        /// </summary>
        void LoadProperty(T entity, Expression<Func<T, object>> selector);

        /// <summary>
        /// Returns all entities for a given type
        /// </summary>
        /// <returns>All entities</returns>
        List<T> GetAll();

        /// <summary>
        /// Returns all entities for a given type
        /// </summary>
        /// <returns>All entities</returns>
        List<T> GetAll(int index, int count);

        /// <summary>
        /// Returns all entities for a given type which meet a certain boolean expression
        /// </summary>
        /// <returns>All entities</returns>
        List<T> FindBy(Func<T, bool> expression);

        /// <summary>
        /// Returns #count entities from #index entity 
        /// for a given type which meet a certain boolean expression
        /// </summary>
        /// <returns>All entities</returns>
        List<T> FindBy(Func<T, bool> expression, int index, int count); 

        /// <summary>
        /// Add entity to the repository
        /// </summary>
        /// <param name="entity">the entity to add</param>
        /// <returns>The added entity</returns>
        void Add(T entity);

        /// <summary>
        /// Mark entity to be deleted within the repository
        /// </summary>
        /// <param name="entity">The entity to delete</param>
        void Delete(T entity);

    }
}
ASKER CERTIFIED SOLUTION
sagir

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros