jeudi 6 juillet 2017

C# Unity Registration By Convention not working anymore after changes

I need to registare dynamically all ViewModels, Services and Repositories! I had everything working last week, until I changed some BaseClasses and Base Interfaces! Below the rules on how we define all Layers, and also our RegistrationByConvention, which is not working anymore!

//ViewModel Interface:

public interface IUserRoleManagerMainViewModel : IViewModelBase<IUserRoleManagerMainViewModel>, INavCommand

//ViewModelClass

public partial class UserRoleManagerMainViewModel : ViewModelBase <IUserRoleManagerMainViewModel>, IUserRoleManagerMainViewModel

//Service Interface

public interface IUserRoleService : IBusinessServiceBase<IRepositoryBase<ISystemUser>, ISystemUser>

//Service Class

public class UserRoleService : BusinessServiceBase<IRepositoryBase<ISystemUser>,ISystemUser>,IUserRoleService

//ServiceBase Interface

public interface IBusinessServiceBase<R,E> where R : IRepositoryBase<E> where E : IEntityBase
    {
        [Dependency]
        IRepositoryBase<E> Repository { get; set; }
    }

//ServiceBase Class:

    /// <summary>
    /// Abstract Service Base class that allows to create different Repositories techonlogies for different Entities!
    /// </summary>
    /// <typeparam name="R"></typeparam>
    /// <typeparam name="E"></typeparam>
    public abstract class BusinessServiceBase<R, E> : IBusinessServiceBase<R,E> where R : IRepositoryBase<E> where E : IEntityBase
    {

        /// <summary>
        /// Repository will be injected at runtime! It could be NHibernate Repository, SQL or Entityframework repository, depending on the Repository registered!
        /// The Repository dependency shall be property injected, rather then constructor injected because it is an architectural choice, whic is hidden to the developer, on purpose!
        /// </summary>
        [Dependency]
        public IRepositoryBase<E> Repository { get; set; }

        public BusinessServiceBase()
        {

        }
    }

//Repository Interface

public interface INHibernateRepository<T> : IRepositoryBase<T> where T : IEntityBase

//Registration by convention which is not working anymore!!

public SettingsRegistrationConvention(IEnumerable<Type> scanTypes)
    {
        if (scanTypes == null)
            throw new ArgumentNullException("scanTypes");

        IEnumerable<Type> entityTyeps = scanTypes.Where(entity => entity.GetInterfaces().Any(i => !i.IsGenericType && i == typeof(IEntityBase) && i.IsAssignableFrom(entity)));
        IEnumerable<Type> repositoryTypes = scanTypes.Where(repository => repository.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRepositoryBase<>)));
        IEnumerable<Type> genericSerivceTypes = scanTypes.Where(service => service.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IBusinessServiceBase<,>)));            
        IEnumerable<Type> viewModelTypes = scanTypes.Where(viewModel => viewModel.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IViewModelBase<>)));

        IEnumerable<Type> combined = entityTyeps.Concat(repositoryTypes);
        combined = combined.Concat(genericSerivceTypes);            
        combined = combined.Concat(viewModelTypes);
        _scanTypes = combined;            
    }

After changing soem base classes of ViewModels and BusinesServices, the Registration by convention is not working properly anymore!

This is the Test (out of many other with the same error):

//Creates the Test Environment!
container = TestsBootStrapper.Initialize(testConfig);
userRoleViewModel = container.Resolve<IUserRoleManagerMainViewModel>();

Error: The current Type "IUserRoleManagerMainViewModel" is an interface and cannot be constructed! Are you missing a type mapping?

Below a ServiceTest! It resolves the Service, but does not inject the repository![Repository=Null]

//Creates the Test Environment!
container = TestsBootStrapper.Initialize(testConfig);
// resolve the services need for the tests
userRoleService = container.Resolve<IUserRoleService>();

Can't post debugging pictures! Sorry! I do not have enough credits!

Any help on the modification to be done in the registration settings would be really helpfull! Thank you very much!

Regards, G





Aucun commentaire:

Enregistrer un commentaire