如何使用ASP.NET MVC的通用控制器来填充正确的模式 [英] How to use ASP.NET MVC Generic Controller to populate right model

查看:127
本文介绍了如何使用ASP.NET MVC的通用控制器来填充正确的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问一个关于ASP.NET MVC的通用控制器的问题,这个答案显示控制器是这样的:


 公共抽象类GenericController< T>
    其中T:类
{
    公共虚拟的ActionResult详细信息(INT ID)
    {
        VAR模型= _repository.Set< T>()找到(ID);
        返回查看(模型);
    }
}


  
  

可以实施这样的。

 公共类FooController的:GenericController<富>
{}

现在,当有人请求/美孚/细节/ 42,entitiy从_repository的设置&LT拉;美孚>(),而无需编写任何东西,在 FooController的


他解释说,这样是好的,但觉得我要开发产品和客户的通用控制器,在那里我不会用EF加载产品&安培;客户模式,而使用MS数据访问应用程序块。

 公共抽象类GenericController< T>
        其中T:类
    {
        公共虚拟的ActionResult详细信息(INT ID)
        {
            // VAR模型= _repository.Set< T>()找到(ID);
            VAR模型= customer.load(ID);
            要么
            VAR模型= product.load(ID);
            返回查看(模型);
        }
    }

所以,当收到请求时,比如 /客户/细节/ 42或/产品/细节/ 11 然后通用控制器的详细方法将调用但如何才能检测请求来自该控制器和相应的实例正确的类加载正确的模式。

如果请求来自于客户,然后我需要加载从具体的操作方法客户详细信息,或者请求来自于产品的话,我需要从通用控制器的详细操作方法加载产品的详细信息。

我如何使用泛型类型获取的数据集 T 实体框架的数据块?


解决方案

您的拒绝创建一组资源库与您的实体的工作,像CustomerRepository,ProductRepository从基接口像

 公共接口IBaseRepository
{
  吨得到< T>(INT ID);
  而无效,< T>(T实体);
}

,然后扩展与库类型的基础类以及它与任何DI框架实例

 公共抽象类GenericController< T,TREPO>
        其中T:类
        其中,TREPO:IBaseRepository,新的()
    {
        私人IBaseRepository库;        公共GenericController()
        {
            库=新TREPO();
        }        公共虚拟的ActionResult详细信息(INT ID)
        {
           VAR模型= repository.Get< T>(ID);
           返回查看(模型);
        }
    }

举例CustomerController

 公共类CustomerController:GenericController<客户,CustomerRepository>
{}

其中CustomerRepository:

 公共类CustomerRepository:IBaseRepository
{
  公众吨得到< T>(INT ID)
  {
    从DB //加载数据
    返回新客户();
  }
}

I asked a question about ASP.NET MVC Generic Controller and this answer shows a controller like this:

public abstract class GenericController<T> 
    where T : class
{
    public virtual ActionResult Details(int id)
    {
        var model = _repository.Set<T>().Find(id);
        return View(model);
    }
}

Which can be implemented like this.

public class FooController : GenericController<Foo>
{

}

Now when someone requests /Foo/Details/42, the entitiy is pulled from the _repository's Set<Foo>(), without having to write anything for that in the FooController.

The way he explain that is good but think i want to develop a generic controller for product and customer where i will not use EF to load product & customer model rather use MS data access application block.

public abstract class GenericController<T> 
        where T : class
    {
        public virtual ActionResult Details(int id)
        {
            //var model = _repository.Set<T>().Find(id);
            var model =customer.load(id);
            or 
            var model =product.load(id);
            return View(model);
        }
    }

So when request comes like /Customer/Details/42 or /product/Details/11 then generic controller's details method will call but how we can detect that request comes from which controller and accordingly instantiate right class to load right model.

If request comes for Customer then I need to load customer details from detail action method or if request comes for Product then I need to load Product details from detail action method of generic controller.

How do I use generics to get the dataset of type T with the Entity Framework Data block?

解决方案

You nay create a set of repositories for working with your entities, like CustomerRepository, ProductRepository from base interface like

public interface IBaseRepository
{
  T Get<T>(int id);
  void Save<T>(T entity);
}

and then extend your base controller class with repository type and its instantiation with any of DI frameworks

public abstract class GenericController<T, TRepo> 
        where T : class
        where TRepo : IBaseRepository, new()
    {
        private IBaseRepository repository;

        public GenericController() 
        {
            repository = new TRepo();
        }

        public virtual ActionResult Details(int id)
        {
           var model =repository.Get<T>(id);
           return View(model);
        }
    }

Example for CustomerController

public class CustomerController : GenericController<Customer, CustomerRepository>
{

}

where CustomerRepository:

public class CustomerRepository : IBaseRepository
{
  public T Get <T>(int id) 
  {
    // load data from DB
    return new Customer();
  }
}

这篇关于如何使用ASP.NET MVC的通用控制器来填充正确的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆