2017-03-07 41 views
0

我想實現一個類,第一次調用時從數據庫中獲取一些數據,隨後對該類的調用將在應用程序的生命週期中返回相同的數據,即使此類的新實例是調用。這可能嗎?Autofac類只實現

這裏是我的示例嘗試,問題是我得到Object not set to an instance of the Object

Public Class session 
    Implements IContainerProviderAccessor 

    Shared _containerProvider As IContainerProvider 
    Private _IUserSessionService As IUserSessionService ' interface to stores data 
    Public Property Usersession As IUserSessionService 
     Get 
      Return _IUserSessionService 
     End Get 
     Set(value As IUserSessionService) 
      _IUserSessionService = value 
     End Set 
    End Property 

    Public ReadOnly Property ContainerProvider As IContainerProvider Implements IContainerProviderAccessor.ContainerProvider 
     Get 
      Return _containerProvider 
     End Get 
    End Property 


    Public Function GetConnection() As String 
     Dim UserSessionDetail As New UserSessionDetails 
     ' Do we have a container if not register one 
     If IsNothing(_containerProvider) Then 
      RegisterConnection() 
      Dim UserSessionDetail As New UserSessionDetails 
      UserSessionDetail.connection_string = GoAndGetOneFromOtherSource 
      Usersession.AddSession(UserSessionDetail) 
      Return UserSessionDetail.connection_string 
     Else 
      Usersession.GetUserSession() 
      Return UserSessionDetail.connection_string 
     End If 
    End Function 


    Private Sub RegisterConnection() 
     Dim builder As ContainerBuilder = New ContainerBuilder() 

     builder.RegisterType(Of UserSessionService).As(Of IUserSessionService).InstancePerRequest() 

     'Auto Fac Binding 
     _containerProvider = New ContainerProvider(builder.Build()) 

    End Sub 

End Class 
+0

呀,當然這是可能的,定義數據和方法爲靜態,請檢查您是否已經有了數據,如果這樣返回,如果不是,那麼請在取... – Trey

+1

在Autofac而言,這可以通過依賴單身人士對象,或使你的對象成爲單身人士來完成。每當另一個對象聲明對'session'的依賴時,它就會使用同一個對象。說實話,這看起來有點亂。 'IContainerProviderAccessor'應該只用於擴展HttpApplication對象,而不是像這樣。也許回顧[整合文檔](http://autofac.readthedocs.io/en/latest/integration/webforms.html)? –

回答

0

您需要註冊您的IUserSessionService作爲一個單獨的Autofac,在你SessionStore類的構造函數注入它(我用的SessionStore代替Session有與本地Session階級衝突)。 這樣,每次獲得SessionStore實例時,它將只使用IUserSessionService實現的一個實例。

SessionStore看起來就像這樣:

public class SessionStore : ISessionStore 
{ 
    public SessionStore(IUserSessionService userSessionService) 
    { 
     this._userSessionService = userSessionService; 
    }  

    private readonly IUserSessionService _userSessionService; 

    public String ConnectionString 
    { 
     get 
     { 
      // do what you want with _userSessionService 
     } 
    } 
} 

,你的註冊將這個樣子。

builder.RegisterType<XUserSessionService>().As<IUserSessionService>().SingleInstance(); 
builder.RegisterType<SessionStore>().As<ISessionStore>().InstancePerRequest(); 

要獲得ISessionStore例如,您可以使用屬性注入的Page對象

看到structuring pages and user controls for Dependency Injection從文檔的詳細信息。

// MyPage.aspx.cs 
public partial class MyPage : Page 
{ 
    // This property will be set for you by the PropertyInjectionModule. 
    public ISessionStore SessionStore { get; set; } 

    protected void Page_Load(object sender, EventArgs e) 
    { 
    // Now you can use the property that was set for you. 
    label1.Text = this.SessionStore.ConnectionString; 
    } 
} 

或者如果您嘗試訪問頁面以外的會話,請訪問IContainerProviderAccessor

ILifetimeScope lifetimeScope = ((IContainerProviderAccessor)HttpContext.Current.ApplicationInstance).ContainerProvider.RequestLifetime; 
ISession session = lifetimeScope.Resolve<ISessionStore>();