opened
ktheeten
username_0
Hi,

I am trying to integrate CQRSlite with a solution that uses NServiceBus.  NServiceBus uses Autofac as its DI container.  To integrate everything in Autofac, I tried the following :

```
builder.RegisterType<Session>().As<ISession>();
builder.RegisterType<SFEventStore>().As<IEventStore>();
builder.RegisterType<MemoryCache>().As<ICache>();
builder.Register(c =>
            {
                return new CacheRepository(new Repository(c.Resolve<IEventStore>()), c.Resolve<IEventStore>(), c.Resolve<ICache>());

            })
            .As<IRepository>();
```

This causes a circular dependency exception on the IRepository.  When an ISession is resolved, Autofac resolves an IRepository to a CacheRepository, which in turn resolves the Repository to a CacheRepository because the constructor of a CacheRepository requires an IRepository:

`public CacheRepository(IRepository repository, IEventStore eventStore, ICache cache);`

Any idea how to solve this?

I really want to use CQRSlite because it contains great functionality for our solution!

Thanks,
Karl
Using Autofac as DI container
issue
created
gautema
username_1
It looks like Autofac has some built in support for decorators. Maybe you can try it? 

I do however find it strange that the builder method don't work as it is. You could try making a builder method for Session instead and see if it works?

[Adapters and Decorators](http://docs.autofac.org/en/latest/advanced/adapters-decorators.html)
comment
created
ktheeten
username_0
Tried it with the Autofac decorator, but still no luck.  I  [opened a question on StackOverflow](https://stackoverflow.com/questions/44671464/autofac-circular-dependency) about this, explaining what I tried.
comment
created
ktheeten
username_0
Hi Gaute,

I was able to resolve the circular dependency stuff (was related to NServicebus).

I have another question.  In the sample implementation, you register a **scoped** instance of the CacheRepository.   Does this ultimately mean that for every command that is handled, a new CacheRepository is created?  So if you then Get an aggregate from the session (and as a consequence from the CacheRepository) would you agree that an aggregate will never be retrieved from the cache, unless you put it in there first within the handler?

So in this code:

```
      public async Task Handle(DeactivateInventoryItem message)
        {
            var item = await _session.Get<InventoryItem>(message.Id, message.ExpectedVersion);
            item.Deactivate();
            await _session.Commit();
        }
```

the InventoryItem will not be in the cache?  It will be retrieved from the event store however.

Regards
Karl
comment
created
gautema
username_1
Even if the Cache is scoped, both System.Runtime.Caching.MemoryCache used by .NET Full framework and Microsoft.Extensions.Caching.Memory.MemoryCache used by .NET Core stores the events in a globally scoped cache. So it will be cached, but feel free to try with a debugger to make sure.
comment
closed
gautema
username_1
issue