Autofac parameter passing - autofac

I've been trying to integrate the latest version of autofac (1.3.3.54), and I'm running into the following problem.
The recommended way of consuming parameters in the Register callback, per the Google code wiki for the project is as follows:
builder.Register((c, p) => new Foo(p.Get("arg1")));
However this won't compile with the mentioned version of the autofac code. I looked through the source and I see that p is an IEnumerable (ComponentActivatorWithParameters). Is the code out of date with respect to the documentation?

It appears that the code has changed and the documentation on the Wiki has not been updated. The "Get" method is now "Named" and the "Parameter" class is now "NamedParameter". See the following example:
var builder = new ContainerBuilder();
builder.Register((c, p) => new Person(p.Named<string>("name")));
using (var container = builder.Build())
{
var person = container.Resolve<Person>(new NamedParameter("name", "Fred"));
}
Hopefully someone can update the documentation soon.

I've attached freshly built documentation for AutoFac 1.3 to AutoFac issue #121. I hope they'll resume posting official 1.3 documentation at least until they retire the 1.3 branch and, with it, support for .NET 2.0.

Related

How to resolve dependency in dependency register time in Autofac

earlier I used Microsoft unity IOC. Unity container has RegisterType & Resolve method. So I able to resolve same interface with multiple implementation. but latest autofac (var builder = new ContainerBuilder();) builder hasn't resolve() method and we can't manually build (builder.Build();).
Anyone has any idea, how to convert below code base to autofac or how to use resolve method with dependency registration.
container.RegisterType<IQueryManager, FirstManager>(new TypedParameter(typeof(GeneralEnumerations.DatabaseType), GeneralEnumerations.DatabaseType.First));
container.RegisterType<IQueryManager, SecondManager>(new TypedParameter(typeof(GeneralEnumerations.DatabaseType), GeneralEnumerations.DatabaseType.Two));
container.RegisterType<IQueryManager, ThirdManager>(new TypedParameter(typeof(GeneralEnumerations.DatabaseType), GeneralEnumerations.DatabaseType.Three));
Func<DatabaseType, IQueryManager> priceFactory = (databaseType) => container.Resolve<IQueryManager>(databaseType.ToString());
container.RegisterInstance<IQueryManagerFactory<IQueryManager>>(new QueryManagerFactory<IQueryManager>(priceFactory));
I try this but not working. any suggestion how to solve this with"builder.RegisterInstance".
builder.Register<IQueryManagerFactory<IQueryManager>>(container => new QueryManagerFactory<IQueryManager>(databaseType =>
container.Resolve<IQueryManager>(new TypedParameter(typeof(GeneralEnumerations.DatabaseType), databaseType))
));

Is it possible to use Autofac somehow within Unity3d?

I am using Unity3d 2018.1.0f3 which is using .net 4.6x and has netstandard 2.0. Does anyone know if its possible to get Autofac working within Unity? Currently, I get an error saying that it unloaded the assemblies due to possible runtime issues.
Thanks,
MH
I have no idea about the other stuff, and I use an older version of it, but I use AutoFac (3.5.2) with Unity with no issues at all. I think it needs slightly different setup for unity but not really sure. but I have an editor window system that uses ScriptableObjects for databases and seperate pages for different item types, and use autofac to inject the databases, and find all the sub pages and auto load them.
this is how I do it if this helps:
private ISWeaponDatabase weaponDb;
private ISQualityDatabase qualityDb;
private ISArmorDatabase armorDb;
private int currentTab;
private ContainerBuilder builder;
private BasePage[] pages;
DatabaseConfig dbConfig = Resources.Load<DatabaseConfig>("DatabaseConfig");
if (dbConfig == null) return;
if (weaponDb == null) weaponDb = ISWeaponDatabase.GetDatase<ISWeaponDatabase>(dbConfig.DataFolder, dbConfig.WeaponDatabase.DatabaseFile);
if (qualityDb == null) qualityDb = ISWeaponDatabase.GetDatase<ISQualityDatabase>(dbConfig.DataFolder, dbConfig.QualityDatabase.DatabaseFile);
if (armorDb == null) armorDb = ISArmorDatabase.GetDatase<ISArmorDatabase>(dbConfig.DataFolder, dbConfig.ArmorDatabase.DatabaseFile);
builder = new ContainerBuilder();
builder.RegisterInstance(weaponDb).As<ISWeaponDatabase>();
builder.RegisterInstance(armorDb).As<ISArmorDatabase>();
builder.RegisterInstance(qualityDb).As<ISQualityDatabase>();
builder.RegisterAssemblyTypes(Assembly.GetAssembly(GetType())).Where(t => t.Name.EndsWith("EditorPage")).As<BasePage>().SingleInstance();
IContainer container = builder.Build();
pages = container.Resolve<IEnumerable<BasePage>>().ToArray();
Yes, it is possible, would be nice to know what you tried until now, but basically, you'll import it as you'd import every other 3rd party library.
I assume you are on an up-to-date Windows Machine and you are using VS because you didn't give us any information on that.
Fire up your NuGet PM in VS (Tools > NuGet Package Manager > Console)
Command: Install-Package Autofac -Version 4.8.1
Or your preferred way to get a NuGet package https://www.nuget.org/packages/Autofac/ (VS is of course also working)
Now you should have a package in /Packages/Autofac.4.8.1/lib/net45
Copy the DLL over to your assets/lib folder (or wherever you want to have it)
If you go back to Unity it will load Autofac now
Now you can go to any class you have and type "using Autofac" and get started.

What happened to AddExportedObject?

we have source code from a long time ago .NET 2.0 which looks like:
DirectoryCatalog catalog =
new DirectoryCatalog(Path.Combine(Path.Combine(applicationDirectory, providersDirectory), providerSubdirectory));
CompositionContainer container = new CompositionContainer(catalog);
CompositionBatch batch = new CompositionBatch();
batch.AddPart(this);
batch.AddExportedObject(container);
container.Compose(batch);
This has worked fine for years, but after upgrading visual studio and the framework to .NET 4.0, the call to AddExportedObject no longer seems to be supported by MEF.
As you can read here,
While CompositionBatch itself does not contain an AddExportedObject<T>
there is an extension method for it. The extension methods are in the
root System.ComponentModel.Composition namespace so ensure you are
including that in your file's using statements. If you cannot get the
extension method to work you can always call the method directly on
AttributedModelServices.

Phonegap and Nova Data Framework -

I am learning PhoneGap for an app project and need to use the database for certain aspects, I am trying out the Nova Data framework,
https://cordova.codeplex.com/wikipage?title=How%20to%20use%20nova.data
I am trying to use my code to put together a test entity, but I am getting a db error telling me there is a missing table. The documentation does not specify that the database should be created beforehand, but I am starting to think that may be the case. Has anyone out there used the Nova framework in a project? I just need a little guidance.
Here is my code I am using to kick off the DB Context:
var DataContext = function () {
nova.data.DbContext.call(this, "HealthDb", "1.0", "Health DB", 1000000);
this.Temperatures = new nova.data.Repository(this, Temperature, "Temperatures");
};
DataContext.prototype = new nova.data.DbContext();
DataContext.constructor = DataContext;
And my entity (Temperature) :
var Temperature = function () {
nova.data.Entity.call(this);
this.Value = 101;
};
Temperature.prototype = new nova.data.Entity();
Temperature.constructor = Temperature;
It is creating an empty database with the proper name, just no tables! I am grateful for any assistance!
Thanks for using our library. I have made the html5 sqlite as a standalone library. Please get it from github.
A live demo link is also available there. And the documentation is more complete. The lib itself has also been updated and a few bugs fixed.
Thanks,
Leo
Turns out I was trying to start up the dbcontext before I defined my entity classes....
Changed the order of my js files and it works.

QBO Queries and SpecifyOperatorOption

I'm trying to query QBO for, among other entities, Accounts, and am running into a couple of issues. I'm using the .Net Dev Kit v 2.1.10.0 (I used NuGet to update to the latest version) and when I use the following technique:
Intuit.Ipp.Data.Qbo.AccountQuery cquery = new Intuit.Ipp.Data.Qbo.AccountQuery();
IEnumerable<Intuit.Ipp.Data.Qbo.Account> qboAccounts = cquery.ExecuteQuery<Intuit.Ipp.Data.Qbo.Account>(context);
(i.e. just create a new AccountQuery of the appropriate type and call ExecuteQuery) I get an error. It seems that the request XML is not created properly, I just see one line in the XML file. I then looked at the online docs and tried to emulate the code there:
Intuit.Ipp.Data.Qbo.AccountQuery cquery = new Intuit.Ipp.Data.Qbo.AccountQuery();
cquery.CreateTime = DateTime.Now.Date.AddDays(-20);
cquery.SpecifyOperatorOption(Intuit.Ipp.Data.Qbo.FilterProperty.CreateTime,
Intuit.Ipp.Data.Qbo.FilterOperatorType.AFTER);
cquery.CreateTime = DateTime.Now.Date;
cquery.SpecifyOperatorOption(Intuit.Ipp.Data.Qbo.FilterProperty.CreateTime,
Intuit.Ipp.Data.Qbo.FilterOperatorType.BEFORE);
// Specify a Request validator
Intuit.Ipp.Data.Qbo.AccountQuery cquery = new Intuit.Ipp.Data.Qbo.AccountQuery();
IEnumerable<Intuit.Ipp.Data.Qbo.Account> qboAccounts = cquery.ExecuteQuery<Intuit.Ipp.Data.Qbo.Account>(context);
unfortunately, VS 2010 insists that AccountQuery doesn't contain a definition for SpecifyOperatorOption and there is no extension method by that name. So I'm stuck.
Any ideas how to resolve this would be appreciated.