ich neueste Autofac bin mit und möchte die gleiche Art und Schnittstelle zweimal auf verschiedenen Konstrukteuren basierend registrierenAutofac registrieren gleiche Schnittstelle mit unterschiedlichen Herstellern
Meine Klasse/Schnittstelle
public partial class MyDbContext : System.Data.Entity.DbContext, IMyDbContext
{
public MyDbContext(string connectionString)
: base(connectionString)
{
InitializePartial();
}
public MyDbContext(string connectionString, bool proxyCreationEnabled, bool lazyLoadingEnabled, bool autoDetectChangesEnabled)
: base(connectionString)
{
this.Configuration.ProxyCreationEnabled = proxyCreationEnabled;
this.Configuration.LazyLoadingEnabled = lazyLoadingEnabled;
this.Configuration.AutoDetectChangesEnabled = autoDetectChangesEnabled;
InitializePartial();
}
}
In meinem Autofac Setup i bin Registrierung über ..
builder.RegisterType<MyDbContext>().As<IMyDbContext>()
.WithParameter((pi, c) => pi.Name == "connectionString", (pi, c) => c.Resolve<IConnectionStringProvider>().ConnectionString)
.InstancePerLifetimeScope();
Wie kann ich den zweiten Konstruktor, mit Autofac registrieren, so dass ich es über Konstruktor Injektion auf verschiedenen Klassen verwenden kann? Ich dachte an etwas wie das Folgende, aber wie weiß Autofac, welche Klasse er injizieren soll.
//builder.RegisterType<MyDbContext>().As<IMyDbContext>()
// .WithParameter((pi, c) => pi.Name == "connectionString", (pi, c) => c.Resolve<IConnectionStringProvider>().ConnectionString)
// .WithParameter((pi, c) => pi.Name == "proxyCreationEnabled", (pi, c) => false)
// .WithParameter((pi, c) => pi.Name == "lazyLoadingEnabled", (pi, c) => false)
// .WithParameter((pi, c) => pi.Name == "autoDetectChangesEnabled", (pi, c) => false)
// .Named<MyDbContext>("MyDbContextReadOnly")
// .InstancePerLifetimeScope();
Ich denke, Ihre Frage sollte wie kann ich Typ mit Parametern registrieren. Sie sollten einen Konstruktor auswählen. Wenn Sie mehrere erstellen möchten, erstellen Sie eine Factory-Klasse und registrieren Sie die Factory neben Ihrer Klasse. –
http://autofac.readthedocs.io/en/latest/faq/select-by-context.html –