2016-09-19 7 views
0

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(); 
+0

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. –

+0

http://autofac.readthedocs.io/en/latest/faq/select-by-context.html –

Antwort

0

Dies scheint zu arbeiten, aber nicht zuversichtlich, dass es am besten möglich ist. Ich habe eine neue IMyDbContextReadonly-Schnittstelle für die Klasse MyDbContextReadonly mit dem Konstruktor erstellt, den ich verwenden möchte.

public interface IMyDbContextReadonly : IMyDbContext { } 

public class MyDbContextReadonly : MyDbContext, IMyDbContextReadonly 
{ 
    public MyDbContextReadonly(string connectionString, bool proxyCreationEnabled, bool lazyLoadingEnabled, bool autoDetectChangesEnabled) 
     : base(connectionString) 
    {    
     this.Configuration.ProxyCreationEnabled = proxyCreationEnabled; 
     this.Configuration.LazyLoadingEnabled = lazyLoadingEnabled; 
     this.Configuration.AutoDetectChangesEnabled = autoDetectChangesEnabled; 
    } 
} 

Dann in der Autofac Konfiguration I wie ..

  builder.RegisterType<MyDbContextReadonly>().As<IMyDbContextReadonly>() 
       .WithParameter((pi, c) => pi.Name == "connectionString", (pi, c) => c.Resolve<IConnectionStringProvider>().ConnectionString) 
       .WithParameter("proxyCreationEnabled", false) 
       .WithParameter("lazyLoadingEnabled", false) 
       .WithParameter("autoDetectChangesEnabled", false) 
      .InstancePerLifetimeScope(); 

Dies funktioniert, aber wieder registriert, dann ist dies der richtige Ansatz? thx

Verwandte Themen