Dependency injection 将用户信息向下传递到数据访问
我正在开发一个桌面应用程序,它为数据库访问生成代码,并使用静态对象进行用户标识 现在,我们需要通过webservice公开一些逻辑,我们正在寻找侵入性最小的表单,以便将用户信息通过管道推送到数据库访问类 我们想到的是向Insert/Update方法传递一个委托,如下所示:Dependency injection 将用户信息向下传递到数据访问,dependency-injection,data-access,Dependency Injection,Data Access,我正在开发一个桌面应用程序,它为数据库访问生成代码,并使用静态对象进行用户标识 现在,我们需要通过webservice公开一些逻辑,我们正在寻找侵入性最小的表单,以便将用户信息通过管道推送到数据库访问类 我们想到的是向Insert/Update方法传递一个委托,如下所示: public delegate string GetLogin(); public class BaseEntity : BaseNotifiableEntity, System.ComponentModel.IDataEr
public delegate string GetLogin();
public class BaseEntity : BaseNotifiableEntity, System.ComponentModel.IDataErrorInfo
{
public GetLogin Login { get; set; }
(...)
}
public static class BaseEntityHelper
{
public static SqlCommand buildUpdateCommand(BaseEntity entity)
{
UpdateDefaultValues(entity, false);
(...)
}
public static void UpdateDefaultValues(BaseEntity entity, bool affectCreationFields)
{
if (entity.Login == null && AppServer.RunningApplication.CurrentUser == null)
throw new Exception("Something went wrong");
(...)
}
}
public class Service
{
T_DIST_Service record;
(...)
public bool Update(DataAccess.Base.GetLogin login)
{
record.Login = login;
(...)
record.Update();
}
}
因此,在我们的逻辑中,将有如下内容:
public delegate string GetLogin();
public class BaseEntity : BaseNotifiableEntity, System.ComponentModel.IDataErrorInfo
{
public GetLogin Login { get; set; }
(...)
}
public static class BaseEntityHelper
{
public static SqlCommand buildUpdateCommand(BaseEntity entity)
{
UpdateDefaultValues(entity, false);
(...)
}
public static void UpdateDefaultValues(BaseEntity entity, bool affectCreationFields)
{
if (entity.Login == null && AppServer.RunningApplication.CurrentUser == null)
throw new Exception("Something went wrong");
(...)
}
}
public class Service
{
T_DIST_Service record;
(...)
public bool Update(DataAccess.Base.GetLogin login)
{
record.Login = login;
(...)
record.Update();
}
}
当然,这涉及到在应用程序中更改许多方法。
所以我想知道是否有一种无缝的方法可以通过依赖注入(例如)来实现这一点
也许你们中的一些人已经走上了这条路,并且有一些见解可以分享。
谢谢你抽出时间
编辑1:
在体系结构级别上使用.NET,您试图将不属于该层的逻辑放入数据访问层,这听起来像是我的想法。一个数据访问组件应该是一个,所以理想情况下,任何逻辑都应该在调用层实现 但是,如果您现在想要更直接的修复,最好使用内置的环境上下文
如果您有用户对象必须携带的特殊信息,可以使用的自定义实现创建自定义。My bad,它是.NET平台