Autofac configuration problems trying to integrate Quartz - entity-framework

I am working with the Owned type as found here: Strong reference of Autofac 2
I'm also using Quartz scheduler, MSMQ, and EF.
My config looks as follows. I've clearly got something wrong as the context that gets injected to the repositories is a different instance than the one given to the service.
builder.RegisterType<EmailAllocationJob>();
builder.RegisterGeneric(typeof(JobWrapper<>));
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>();
builder.RegisterType<DataContext>().As<IUnitOfWork>();
builder.RegisterType<EmailAccountRepository>().As<IEmailAccountRepository>();
builder.RegisterType<EmailMessageRepository>().As<IEmailMessageRepository>();
builder.RegisterType<EmailMessageQueue>().As<IEmailMessageQueue>();
builder.RegisterType<EmailAllocationService>().As<IEmailAllocationService>();
I can't for the life of me figure out how to get the configuration fixed. I'd reckon it's the line:
builder.RegisterType<DataContext>().As<IUnitOfWork>();
What I want to say is something like:
builder.RegisterType<DataContext>().As<IUnitOfWork>().InstancePerOwned<EmailAllocationJob>();
Thanks in advance if you can help.

Ok I got it. Needed the line:
builder.RegisterType<DataContext>().InstancePerOwned<EmailAllocationJob>()
.As<IUnitOfWork>().AsSelf();
So it seems important that the DataContext features as the generic argument to RegisterType ONCE, and that the method calls to As<>() and AsSelf() are to be daisy chained in a single statement. Seems obvious now, with a fresh head following yesterday evening.

Related

EF - Eager include grandchildren not working, does not contain definition for 'Select'

I am working in VS 2015 solution with an MVC 5 project and a code library project using EF 6.1. I have redone the entire project from a previous version in VS 2013 hoping it would resolve this problem, but no luck. I am just trying to eager load a child with grandchildren. I tried this:
Test t = db.Tests
.Include("TestsRemoteOBD")
.Include("TestsRemoteOBD.TestsRemoteOBDDtcs")
.FirstOrDefault();
and this:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.Select(s=>s.TestsRemoteOBDDtcs))
.FirstOrDefault();
Include using the string works for the child, but not the grandchildren. And with the 2nd query, I'm getting this error:
'TestsRemoteOBD' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'TestsRemoteOBD' could be found (are you missing a using directive or an assembly reference?)
I've seen the same question resolved by adding "using System.Data.Entity;", but I had done that a long time ago. No help. What else could I be missing?
Thanks!
Thanks so much, to all those who commented. I would have been banging my head for another day, at least. The way I had written the second statement would have worked fine if the parent [Tests] had a 1-to-many relationship with [TestRemoteOBD], but since the latter was not a collection, there was no 'Select' defined. I have not tested, but I believe it should be something like:
Test t = db.Tests
.Include(i=>i.TestsRemoteOBD)
.Include(i=>i.TestsRemoteOBD.TestsRemoteOBDDtcs)
.FirstOrDefault();
Bump. Found this and thought I'd throw in my own research.
Microsoft's
Documentation
states: To include a reference and then a reference one level down: query.Include(e => e.Level1Reference.Level2Reference)
-- but this doesn't seem to work under even the simplest use case. Executing the query with a .ToList() does not actually include anything. Smells like an obvious bug to me!
I can see 2 workarounds.
First, as Ben here suggests, you can manually iterate over the entities to force EF to load them.
Second, if scoping permits, you can forget the 'using' block and keep a more permanent instance of the dbcontext as a class member so that it's still available, not disposed (System.ObjectDisposedException), when the members are accessed.

Having AutoMapper to inject dependencies using an IoC Container when needed

I have tried almost everything, but I cannot get AutoMapper to map A => B when B doesn't have a parameterless constructor.
I'm using Unity and all the dependencies are registered conveniently but, how do I say to AutoMapper "hey, if the target instance needs some dependency in the constructor, ask Unity to build it, and do the mapping later.
I've tried with
Mapper.Initialize(configuration =>
{
configuration.ConstructServicesUsing(container.Resolve);
configuration.CreateMap<Person, PersonViewModel>();
});
But it doesn't work :(
EDIT: In fact, I lied a bit. I'm not using Unity. I'm using Grace, but didn't want to come up with a relatively unknown container asking about advances topics :)
I've solved the problem and it works as smooth as silk. The exact code is like this. Keep in mind that I'm using the Grace IoC Container (which I eagerly recommend).
Bootstrapper.Instance.Configure(new CompositionRoot());
Mapper.Configuration.ConstructServicesUsing(type => Bootstrapper.Instance.Container.Locate(type));
Mapper.CreateMap<Person, PersonViewModel>()
.ConstructUsingServiceLocator();
Like this:
configuration.CreateMap<Person, PersonViewModel>()
.ConstructUsingServiceLocator();
Do this for each mapping that should be created by your service locator.

Play Framework Scala: routes with optional parameters not working in controller subpackages?

I'm trying to set up a route in the Playframework 2.0 (Scala) that includes optional parameters in the query string, following the examples in the documentation:
GET /my/path controllers.foo.Bar.list(offset: Int ?= 0, limit: Int ?= 20)
However when compiling, I get the following error message:
object controllers.foo.Bar does not take parameters
I made sure that the controllers.foo.Bar.list method does in fact take two Ints as parameters. One key observation (I hope) may be that this used to work previously, when I had the Controller class directly in the controllers package, i.e.
controllers.Bar.list
But it ceased working as soon as I introduced a "foo" subpackage in Controllers.
Any input on what I'm doing wrong highly appreciated!
UPDATE: Sorry - I did some more experimenting and it seems the reason is something entirely different (d'oh). In my concrete case, my controller class was
controllers.foo.List.list
and that seemed to cause a name clash. Renaming to something else ("FooList") solved the issue.
For anyone discovering this question, it seems very likely that this was due to a bug in the Play Framework.
You can follow its progress on the issue tracker ticket.

JBehave getting started issues BeforeAndAfterSteps

I am new to JBehave having started to use it yesterday.
There seems to be a typo in the getting started pages I hope someone
can help with.
In "Developing Stories' section the example of configuration has the line:
addSteps(new InstanceStepsFactory(new TraderSteps(), new
BeforeAndAfterSteps()).createCandidateSteps());
However, there is no class called BeforeAndAfterSteps. The nearest I
found was BeforeOrAfterSteps but it requires parameters in the
constructor and I'm not sure what to use.
Thanks
Correct - the InstanceStepsFactory takes your POJOs with JBehave annotated methods and creates Steps out of them. There are also StepsFactories that can create the Steps classes from a container (Pico, Guice, Spring or Weld).

WCF Data Service with EF fails to expose imported functions

(I am also using .NET 4.0 and VS 2010.)
I created a function import returning a complex type, as explained at http://msdn.microsoft.com/en-us/library/bb896231.aspx. The function import and new complex type appear in my .edmx file and in the Designer.cs file. However, the function does not appear when I view the service in the browser, and when I add or update a service reference in the client project, the function does not appear there either - as is to be expected, given the first result.
Creating an imported function and using it seems conceptually very simple and straightforward, and one would think it would just work, as Microsoft's step-by-step instructions appear to suggest: http://msdn.microsoft.com/en-us/library/cc716672.aspx#Y798 (which article shows the SP returning entity types - I tried this also, and it doesn't work for me either).
This blog post shows the addition of a method to the DataService class, which Microsoft's instructions omit: http://www.codegain.com/articles/wcf/miscellaneous/how-to-use-stored-procedure-in-wcf-data-service.aspx I tried adding one method returning a list of entity types and another returning a list of complex types, and still had no success. I still could not access the functions, either directly via the browser or from the client application via a service reference.
Thanks in advance for any help with this.
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
MS would do well to add a note to the walkthroughs stating that the above bit of code must be there. (It may be better to enable each operation explicitly than to use "*".)
http://www.codegain.com/articles/wcf/miscellaneous/how-to-use-stored-procedure-in-wcf-data-service.aspx shows that line of code. Also, something it is there in the code, commented out, when one creates the WCF Data Service. Some of us like to delete commented-out code that we aren't using and that seems irrelevant - perhaps doing so a bit prematurely, sometimes.