This project is read-only.

Orchard data in Azure Worker / a class without DI contructor

Topics: Customizing Orchard, Writing modules
Mar 18, 2014 at 12:30 AM

I'm currently extending Orchard with a small module which stores data in the database.
I created data record class, a migration and a controller.
I added the dependency injection to the controller and so I am able to store data in the db.

But now I need to work with the data in a worker role. So I cannot use the dependency injection.
I tried to resolve the dependency but unfortunately only get an error.
var builder = new ContainerBuilder();
var container = builder.Build();
var x = container.Resolve<IRepository<MyRecord>>();
In the line where I try to resolve, I receive the error:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Orchard.Data.Repository`1[testModule.Models.MyRecord]' can be invoked with the available services and parameters:

Cannot resolve parameter 'Orchard.Data.ISessionLocator sessionLocator' of constructor 'Void .ctor(Orchard.Data.ISessionLocator)'.

How do I have to resolve that correctly?
Mar 19, 2014 at 2:55 AM
You could try to build up the whole DI container needed for IRepository (it's a loooot more than this) but I think it's just easier to simply write to the DB directly e.g. through the standard ADO.NET API or even using some ORM. The task is so small that it's a huge overhead to try to it in such a complicated way.

If you need a lot more than just data access from Orchard in a framework-y way, you could try using just Orchard.Framework. I think there were people trying to do that.
Mar 19, 2014 at 8:36 AM
Thank you Piedone.

I thought it might be possible without too much overhead and I just missed something.
Of course I would have preferred to use only one way of reading/writing to the database to have only one point of failure for this.

It looks like using e.g. Entity Framework or direct NHibernate would be much faster then.
Mar 19, 2014 at 12:36 PM
Yes, I think so. Until Orchard.Framework is supported to be used stand-alone.