C# 如何通过MVC应用程序传播自定义对象?

C# 如何通过MVC应用程序传播自定义对象?,c#,asp.net-mvc,concurrency,webforms,C#,Asp.net Mvc,Concurrency,Webforms,假设我有一个对象,对于每个试图访问我的网站的用户来说都是相似的。 一种会话范围对象,应该在我整个“应用程序”中的每个视图/模型/控制器上都可见 我希望在调用页面并通过来自我自己数据库的数据填充页面时创建它 然后,在调用myObject.Title的视图上(例如)。 在WebForms上,我正在扩展UserControl类,例如: public class iUserControl : System.Web.UI.UserControl { protected MyCurrentPage

假设我有一个对象,对于每个试图访问我的网站的用户来说都是相似的。 一种会话范围对象,应该在我整个“应用程序”中的每个视图/模型/控制器上都可见

我希望在调用页面并通过来自我自己数据库的数据填充页面时创建它

然后,在调用myObject.Title的视图上(例如)。 在WebForms上,我正在扩展UserControl类,例如:

public class iUserControl : System.Web.UI.UserControl
{
    protected MyCurrentPage myCurrentPage;

    public iUserControl()
    {

    }

    protected override void OnLoad(EventArgs e)
    {
        myCurrentPage = new MyCurrentPageWrapper();
    }
}
而对于每个用户控件,都有如下内容:

public partial class context_pippo_MyOwnUserControl : iUserControl
public interface IControllerBaseService 
{
   IUserService UserService {get;set;}
   ShoppingMode ShoppingMode {get;set;}
   ...
}

public abstract class ControllerBase : Controller, IControllerBaseService 
{
   public IUserService UserService {get;set;} // this is injected by IoC
   public ShoppingMode ShoppingMode 
   {
      get 
      {
           return UserService.CurrentShoppingMode; // this uses injected instance to get value
      }
   ...
}
(IControllerBaseService)ViewContext.Controller
 public static ShoppingMode CurrentShoppingMode(this HtmlHelper helper)
 {
     return ((IContollerBaseService)helper.ViewContext.Controller).ShoppingMode;
 }

在MVC上,我看不到每个控件都有任何扩展,那么如何实现这种过程呢?我不想在会话中存储元素。

若我正确理解了这个问题,我想我在一个项目中也做过类似的事情。 我有这样的想法:

public partial class context_pippo_MyOwnUserControl : iUserControl
public interface IControllerBaseService 
{
   IUserService UserService {get;set;}
   ShoppingMode ShoppingMode {get;set;}
   ...
}

public abstract class ControllerBase : Controller, IControllerBaseService 
{
   public IUserService UserService {get;set;} // this is injected by IoC
   public ShoppingMode ShoppingMode 
   {
      get 
      {
           return UserService.CurrentShoppingMode; // this uses injected instance to get value
      }
   ...
}
(IControllerBaseService)ViewContext.Controller
 public static ShoppingMode CurrentShoppingMode(this HtmlHelper helper)
 {
     return ((IContollerBaseService)helper.ViewContext.Controller).ShoppingMode;
 }
只要我使用IoC容器创建控制器实例,容器就会注入UserService属性

您现在可以从以下视图访问界面:

public partial class context_pippo_MyOwnUserControl : iUserControl
public interface IControllerBaseService 
{
   IUserService UserService {get;set;}
   ShoppingMode ShoppingMode {get;set;}
   ...
}

public abstract class ControllerBase : Controller, IControllerBaseService 
{
   public IUserService UserService {get;set;} // this is injected by IoC
   public ShoppingMode ShoppingMode 
   {
      get 
      {
           return UserService.CurrentShoppingMode; // this uses injected instance to get value
      }
   ...
}
(IControllerBaseService)ViewContext.Controller
 public static ShoppingMode CurrentShoppingMode(this HtmlHelper helper)
 {
     return ((IContollerBaseService)helper.ViewContext.Controller).ShoppingMode;
 }
为了在
IControllerBaseService
中提供最常用属性的快捷方式,我有几种扩展方法,如下所示:

public partial class context_pippo_MyOwnUserControl : iUserControl
public interface IControllerBaseService 
{
   IUserService UserService {get;set;}
   ShoppingMode ShoppingMode {get;set;}
   ...
}

public abstract class ControllerBase : Controller, IControllerBaseService 
{
   public IUserService UserService {get;set;} // this is injected by IoC
   public ShoppingMode ShoppingMode 
   {
      get 
      {
           return UserService.CurrentShoppingMode; // this uses injected instance to get value
      }
   ...
}
(IControllerBaseService)ViewContext.Controller
 public static ShoppingMode CurrentShoppingMode(this HtmlHelper helper)
 {
     return ((IContollerBaseService)helper.ViewContext.Controller).ShoppingMode;
 }

因此在视图中,它看起来像
@Html.CurrentShoppingMode()

让我们假设我有
FamilyComponents
类,我应该在任何地方访问它(在每个视图上):我在哪里创建
FamilyComponents
的单个实例?在ControllerBase内部?你能给我举个聪明的例子吗?谢谢,我已经更新了我的答案,但一般来说,如果我在IControllerBaseService中有复杂属性,它要么由IoC容器注入,要么使用注入的属性解析,如示例所示。