在表示层中访问BLL的功能

时间:2013-07-03 10:47:55

标签: asp.net asp.net-mvc-3

我创建了一个应用程序,用户可以注册自己,注册后他导航到主页。我在MVC3中这样做,但是将它转换为3Tier。为此,我将原始项目命名为Presentation并制作了DAL和添加了模型文件夹和DbContext以及从演示文稿中删除模型。现在我必须创建BLL,我想编写所有插入,删除和更新的逻辑,并希望在My Controller中访问它我的演讲。我怎么能这样做? 请指导我这个!!!

1 个答案:

答案 0 :(得分:0)

好像你要我们为你重写整个解决方案?

网上有许多入门套件可以为您提供如何完成工作的提示。有许多不同的方法可以实现相同的结果。我将简要介绍一下我的工作方式。

对于此示例,我将引用客户,您可以对其进行修改以适应您的解决方案。

我有一个名为MyProject.DomainModel的解决方案。在这个项目中,我有我的客户类:

public class Customer
{
     public int Id { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }

     public int Age { get; set; }
}

我有一个名为MyProject.EntityFramework的项目。这里我有我的所有存储库类。客户存储库方法的示例:

public IEnumerable<Customer> FindAll()
{
     return DatabaseContext.Customers;
}

然后我有另一个名为MyProject.Services的项目。客户服务部门会调用您的客户存储库。服务层是不允许的,但是当我需要一些逻辑或需要调用其他存储库时,我使用服务层。这就是我从服务层调用存储库方法的方法:

public interface ICustomerService
{
     IEnumerable<Customer> FindAll();
}

public class CustomerService : ICustomerService
{
     private readonly ICustomerRepository customerRepository;

     public CustomerService(ICustomerRepository customerRepository)
     {
          this.customerRepository = customerRepository;
     }

     public IEnumerable<Customer> FindAll()
     {
          return customerRepository.FindAll();
     }
}

您将看到CustomerService构造函数接收ICustomerRepository的实例。这由Autofac等依赖注入框架处理。

在您的控制器中,您将拥有一个列表操作方法,该方法具有列表视图以显示您的所有客户:

public class CustomerController : Controller
{
     private readonly ICustomerService customerService;

     public CustomerController(ICustomerService customerService)
     {
          this.customerService = customerService;
     }

     public ActionResult List()
     {
          IEnumerable<Customer> customers = customerService.FindAll();

          return View(customers);
     }
}

除此之外还有更多。您必须在线下载一些入门套件并通过它来帮助您走上正确的道路。我所提供的只是一个大纲。

我希望这会有所帮助。

相关问题