如何使用 Unity 将连接字符串和其他变量从 MVC 传递到 Class 库
How to pass connection string and other variables from MVC to Class Library using Unity
我正在使用与 BusinessServices(CLASS 库)对话的 MVC 5。我正在使用 Unity4(基本上是 Unity.Mvc5)自动创建 BusinessLayer 对象。我不想使用 Unity 来管理 DbContext 的生命周期,但确实想将 connectionString 发送到 BusinessServices class,以便将正确的连接字符串从 MVC 传递到 Class 库。
我不知道如何使用 Unity 将 connectionString 和其他所需变量(例如 logPath 等)传递到我的 Class库。有什么建议吗?
实现这一目标的可能性有很多种。您可以像这样使用注入构造函数:
class Service
{
Service(string connectionString, string logPath)
{
}
}
myContainer.RegisterType<Service>(
new InjectionConstructor("Data Source=ServerName;..", "/logs"));
或者您注册一个服务实例:
myContainer.RegisterInstance<Service>(
new Service("Data Source=ServerName;..", "/logs"));
我正在使用与 BusinessServices(CLASS 库)对话的 MVC 5。我正在使用 Unity4(基本上是 Unity.Mvc5)自动创建 BusinessLayer 对象。我不想使用 Unity 来管理 DbContext 的生命周期,但确实想将 connectionString 发送到 BusinessServices class,以便将正确的连接字符串从 MVC 传递到 Class 库。
我不知道如何使用 Unity 将 connectionString 和其他所需变量(例如 logPath 等)传递到我的 Class库。有什么建议吗?
实现这一目标的可能性有很多种。您可以像这样使用注入构造函数:
class Service
{
Service(string connectionString, string logPath)
{
}
}
myContainer.RegisterType<Service>(
new InjectionConstructor("Data Source=ServerName;..", "/logs"));
或者您注册一个服务实例:
myContainer.RegisterInstance<Service>(
new Service("Data Source=ServerName;..", "/logs"));