[英]Base class constructor parameter
I have a base class: 我有一个基类:
public BaseData()
{
var dbConnection = "BasicFinanceEntities";
if (Session != null && Session.TestModeActive)
{
dbConnection = "TestConnection";
}
Context = new BasicFinanceEntities(dbConnection);
Context.Configuration.LazyLoadingEnabled = false;
}
All by data access classes use Base class. 所有通过数据访问的类都使用Base类。
Eg: 例如:
public class AuthenticationData : BaseData
{
public AuthenticationData(SessionObject session)
{
Session = session;
}
.....
Note, 'Session' is declared in the Base class. 注意,“会话”在基类中声明。
All my data classes take a Session object, so that I know if the user is authenticated, his id (For last_update_user type things), as well as some security checks (Can this user see this account..). 我所有的数据类都带有一个Session对象,这样我就知道用户是否已通过身份验证,其ID(对于last_update_user类型的东西)以及一些安全性检查(该用户可以看到此帐户。)。
So they take a parameter, which is the session object. 因此,它们采用一个参数,即会话对象。
The problem is, I need my base class to know this session object - but it seems the base class has a null value for Session (which is a public declaration IN the base class, and all the child classes of Base have access to it). 问题是,我需要我的基类才能知道此会话对象-但看来基类的Session为空值(这是基类中的一个公共声明,并且Base的所有子类都可以访问它) 。
It seems the base constructor fires, before any code within the child constructor. 看来基本构造函数会在子构造函数中的任何代码之前触发。
Is there a way to get the base class to see the parameter passed into the child class? 有没有办法让基类看到传递给子类的参数?
Make your base class constructor take that session object if you need it there: 如果需要,请让基类构造函数使用该会话对象:
public BaseData(SessionObject session)
{
Session = session;
var dbConnection = "BasicFinanceEntities";
if (Session != null && Session.TestModeActive)
{
dbConnection = "TestConnection";
}
Context = new BasicFinanceEntities(dbConnection);
Context.Configuration.LazyLoadingEnabled = false;
}
and then invoke that constructor from your derived class constructor: 然后从派生类构造函数调用该构造函数:
public class AuthenticationData : BaseData
{
public AuthenticationData(SessionObject session) : base(session)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.