简体   繁体   English

ASP.NET MVC应用程序和依赖项注入的适当分层

[英]ASP.NET MVC proper layering of application and dependency injection

I have a project that has three layer. 我有一个包含三层的项目。

1st is the DAL 第一是DAL

2nd is the Domain 第二是领域

3rd is the Presentation 第三是演讲

I created an interface in my Domain Layer (ICategoryRepository) here is the Code 我在域层(ICategoryRepository)中创建了一个接口,这里是代码

public interface ICategoryRepository
{
    List<CategoryDTO> GetCategory();
}

and I created a class in my DAL to implement the ICategoryRepository in my Domain. 我在DAL中创建了一个类,以在我的域中实现ICategoryRepository。

public class CategoryRepository : ICategoryRepository
{                     
    BookInfoContext _context;

    public List<CategoryDTO> GetCategory()
    {
        _context = new BookInfoContext();

        var categoryDto = _context.Categories
                            .Select(c => new CategoryDTO
                            {
                                CategoryId = c.CategroyId,
                                CategoryName = c.CategoryName
                            }).ToList();
        return categoryDto;
    }
}

Then I create a class in my Domain and pass the ICategoryRepository as parameter in my constructor. 然后,我在Domain中创建一个类,并在构造函数中将ICategoryRepository作为参数传递。

public class CategoryService
{

    ICategoryRepository _categoryService;

    public CategoryService(ICategoryRepository categoryService)
    {
        this._categoryService = categoryService;
    }

    public List<CategoryDTO> GetCategory()
    {
        return _categoryService.GetCategory();
    }
}

I do this to invert the control. 我这样做是为了反转控件。 Instead of my domain will depend on DAL I invert the control so that myDAL will depend on my DOMAIN. 而不是我的域将取决于DAL,而是反转控件,以便myDAL将取决于我的DOMAIN。

My Problem is, everytime I call the CategoryService in the Presentation Layer I need to pass ICategoryRepository as parameter of constructor which is in the DAL. 我的问题是,每次我在表示层中调用CategoryService时,都需要传递ICategoryRepository作为DAL中构造函数的参数。 I don't want my Presentation Layer to be dependent in my DAL. 我不希望我的Presentation Layer依赖于我的DAL。

Any suggestion? 有什么建议吗?

Thanks, 谢谢,

You could use a dependency injection. 您可以使用依赖项注入。 In asp.net mvc we have a IDepencyResolver interface that inject dependencies on controllers dependencies and its dependencies. 在asp.net mvc中,我们有一个IDepencyResolver接口,该接口注入对控制器依赖及其依赖的依赖。 To do this, you will need a container to be easy to inject your dpendencies, for sample, MS Unity, Ninject, etc.. and register all types on your container to it knows how to resolve your dependencies. 为此,您将需要一个容器来轻松注入样本,MS Unity,Ninject等的依赖关系,并在容器上注册所有类型,以了解如何解决依赖关系。

With a Container and DependencyResolver setted, you can have a dependency of your service on your controller , for sample: 设置了ContainerDependencyResolver ,您就可以在示例中对controllerservice进行依赖:

public class CategoryController
{
   private readonly ICategoryService _categoryService;

   // inject by constructor..
   public CategoryController(ICategoryService categoryService)
   {
       _categoryService = categoryService;
   }


   public ActionResult Index()
   {
      var categories = _categoryService.GetCategory();

      return View(categories);
   }

}

In this case, the Container will see the controller need the service and this service need a repository. 在这种情况下,容器将看到控制器需要该服务,而该服务需要一个存储库。 It will resolve everything for your, since your have registred these types. 由于您已经注册了这些类型,因此它将为您解决所有问题。

Take a look at this article: http://xhalent.wordpress.com/2011/01/17/using-unity-as-a-dependency-resolver-asp-net-mvc-3/ 看看这篇文章: http : //xhalent.wordpress.com/2011/01/17/using-unity-as-a-dependency-resolver-asp-net-mvc-3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM