Extend functionality of IRepository interface

Topics: Core, Customizing Orchard, General
Jul 11, 2013 at 6:40 AM
Hello.

I need to extend existing functionality of IRepository interface.
I have implemented the following code:
    public interface IRep<T> : IRepository<T>, IDependency
    {
        void CreateOrUpdate(T entity);
    }

    public class Rep<T> : Repository<T>, IRep<T> where T : class
    {
        public Rep(ISessionLocator sessionLocator) : base(sessionLocator)
        {}

        public void CreateOrUpdate(T entity)
        {
            Session.SaveOrUpdate(entity);
        }

        public void CreateOrUpdate(List<T> entities)
        {
            foreach (var entity in entities)
            {
                Session.SaveOrUpdate(entity);
            }
        }
    }
My custom repository is used here:
    public interface IPersonService : IDependency
    {
        ...
    }

    public class PersonService : IPersonService 
    {
        public PersonService(IRepository<ProfessionPartRecord> repProfession, 
            IRepository<PersonProfessionRecord> repPersonProf, IContentManager contentManager, 
            IRepository<ServiceRecord> repService, IRepository<PersonServiceRecord> repPersonServ,
            IRep<AddressRecord> repAddress, IRep<PhoneNumberRecord> repPhoneNumber)
        {...}
    }
I am receiving an exception:
Exception Details: Autofac.Core.DependencyResolutionException: None of the constructors found with 'Orchard.Environment.AutofacUtil.DynamicProxy2.ConstructorFinderWrapper' on type 'Person.Services.PersonService' can be invoked with the available services and parameters:
Cannot resolve parameter 'Common.DataAccess.IRep1[Person.Models.AddressRecord] repAddress' of constructor 'Void .ctor(Orchard.Data.IRepository1[Person.Models.ProfessionPartRecord], Orchard.Data.IRepository1[Person.Models.PersonProfessionRecord], Orchard.ContentManagement.IContentManager, Orchard.Data.IRepository1[Person.Models.ServiceRecord], Orchard.Data.IRepository1[Person.Models.PersonServiceRecord], Common.DataAccess.IRep1[Person.Models.AddressRecord], Common.DataAccess.IRep`1[Person.Models.PhoneNumberRecord])'


What I should do to have it working?
Developer
Jul 11, 2013 at 8:49 AM
The repository is not registered in the normal Orchard way, you will need to create a Orchard Autofac module and register it. Check out DataModule.cs
Jul 11, 2013 at 5:00 PM
Thank you for quick answer.

I have created a derived class from Autofac.Module and it started working
    public class BootstrapModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(Rep<>)).As(typeof(IRep<>)).InstancePerDependency();

        }
        protected override void AttachToComponentRegistration(Autofac.Core.IComponentRegistry componentRegistry, Autofac.Core.IComponentRegistration registration)
        {
            if (typeof(IDataServicesProvider).IsAssignableFrom(registration.Activator.LimitType))
            {
                var propertyInfo = registration.Activator.LimitType.GetProperty("ProviderName", BindingFlags.Static | BindingFlags.Public);
                if (propertyInfo != null)
                {
                    registration.Metadata["ProviderName"] = propertyInfo.GetValue(null, null);
                }
            }
        }
    }