Problem when performing delegate bind on ninject

1

I'm developing an application with ninject for IoC and entity framework, but when I bind with delegate it gives this error:

<Message>An error has occurred.</Message>
<ExceptionMessage>
Error activating IntPtr No matching bindings are available, and the type is not self-bindable. Activation path: 5) Injection of dependency IntPtr into parameter method of constructor of type Func{EventStoreDbContext{Pedido}} 4) Injection of dependency Func{EventStoreDbContext{Pedido}} into parameter contextFactory of constructor of type SqlEventSourcedRepository{Pedido} 3) Injection of dependency IEventSourcedRepository{Pedido} into parameter repository of constructor of type PedidoApplicationService 2) Injection of dependency IPedidoApplicationService into parameter pedidoApplicationService of constructor of type HomeController 1) Request for HomeController Suggestions: 1) Ensure that you have defined a binding for IntPtr. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.
</ExceptionMessage>

The repository implementation:

public class SqlEventSourcedRepository<T> : IEventSourcedRepository<T> where T : class, IEventSourced
    {
        protected static readonly string sourceType = typeof(T).Name;
        protected readonly Func<EventStoreDbContext<T>> contextFactory;
        protected readonly ITextSerializer serializer;
        //private readonly IEventBus eventBus;
        private readonly Func<Guid, IEnumerable<IVersionedEvent>, T> entityFactory;


        public SqlEventSourcedRepository(ITextSerializer serializer, Func<EventStoreDbContext<T>> contextFactory)
        {
            //this.eventBus = eventBus;
            this.contextFactory = contextFactory;
            this.serializer = serializer;

            // TODO: could be replaced with a compiled lambda
            var constructor = typeof(T).GetConstructor(new[] { typeof(Guid), typeof(IEnumerable<IVersionedEvent>) });
            if (constructor == null)
            {
                throw new InvalidCastException("Type T must have a constructor with the following signature: .ctor(Guid, IEnumerable<IVersionedEvent>)");
            }

            this.entityFactory = (id, events) => (T)constructor.Invoke(new object[] { id, events });
        }

        public T Find(Guid id)
        {
            using (var context = this.contextFactory.Invoke())
            {
                var deserialized = context.Set<Event>()
                    .Where(x => x.AggregateId == id && x.AggregateType == sourceType)
                    .OrderBy(x => x.Version)
                    .AsEnumerable()
                    .Select(this.Deserialize)
                    .AsCachedAnyEnumerable();

                if (deserialized.Any())
                {
                    return entityFactory.Invoke(id, deserialized);
                }

                return null;
            }
        }

        public T Get(Guid id)
        {
            throw new NotImplementedException();
        }

        public void Save(T eventSourced, string correlationId)
        {
            var events = eventSourced.Events.ToArray();
            using (var context = this.contextFactory.Invoke())
            {
                var contextSet = context.Set<Event>();
                foreach (var e in events)
                {
                    contextSet.Add(this.Serialize(e, correlationId));
                }

                context.SaveChanges();
            }

            //this.eventBus.Publish(events.Select(e => new Envelope<IEvent>(e) { CorrelationId = correlationId }));
        }

        private Event Serialize(IVersionedEvent e, string correlationId)
        {
            Event serialized;
            using (var writer = new StringWriter())
            {
                this.serializer.Serialize(writer, e);
                serialized = new Event
                {
                    AggregateId = e.SourceId,
                    AggregateType = sourceType,
                    Version = e.Version,
                    Payload = writer.ToString(),
                    CorrelationId = correlationId
                };

                return serialized;
            }
        }

        private IVersionedEvent Deserialize(Event @event)
        {
            using (var reader = new StringReader(@event.Payload))
            {
                return (IVersionedEvent)this.serializer.Deserialize(reader);
            }
        }
    }

The class I'm using with the ninject module (which I can not bind to "EventStoreDbContext < >" which in the repository implementation constructor gets it as "Func >" and I can not pass with the

Bind(typeof(EventStoreDbContext<>)).ToSelf();

):

public class Module : NinjectModule
    {
        public override void Load()
        {
            Bind<ITextSerializer>().To<JsonTextSerializer>();

            Bind(typeof(EventStoreDbContext<>)).ToSelf().InTransientScope().WithConstructorArgument("EventStore");

            Bind<IMessageSender>().To<MessageSender>()
                .WithConstructorArgument("name", "schema")
                .WithConstructorArgument("tableName", "tabela");

            Bind<IDbConnectionFactory>().ToConstant<IDbConnectionFactory>(Database.DefaultConnectionFactory);

            Bind<IEventBus>().To<EventBus>();
            Bind<ICommandBus>().To<CommandBus>();

            Bind<IPedidoApplicationService>().To<PedidoApplicationService>();

            Bind(typeof(IEventSourcedRepository<>)).To(typeof(SqlEventSourcedRepository<>));
        }
    }
    
asked by anonymous 02.09.2015 / 20:19

2 answers

0

I solved this problem by changing the injector from ninject to unity and it managed to resolve this dependency.

    
16.09.2016 / 20:23
0

Does the EventStoreDbContext class have a parameter of type? if you do not try something like this:

Bind<EventStoreDbContext>().ToSelf().InTransientScope().WithConstructorArgument("EventStore");
    
03.09.2015 / 17:30