2017-03-07 39 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>();