0

I have a project that has three layer.

1st is the DAL

2nd is the Domain

3rd is the Presentation

I created an interface in my Domain Layer (ICategoryRepository) here is the Code

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

and I created a class in my DAL to implement the ICategoryRepository in my Domain.

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.

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.

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. I don't want my Presentation Layer to be dependent in my DAL.

Any suggestion?

Thanks,

0

1 Answer 1

1

You could use a dependency injection. In asp.net mvc we have a IDepencyResolver interface that inject dependencies on controllers dependencies and its dependencies. 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.

With a Container and DependencyResolver setted, you can have a dependency of your service on your controller, for sample:

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Do I need to change anything in my categoryService..? I try that code and it will return null value. I use ninject to resolve the dependencies but when I try to bind I get errors.. I am binding using this code kernel.Bind<ICategoryService>().To<CategoryService>();
I do not know about ninject, but as any container, you have to register all dependencies: Repositories and Services, to Container know how to resolve all dependencies on the tree: controller -> service -> repository. Did you register your repository?
You're right I just need to register my repository.. kernel.Bind<ICategoryRepository>().To<CategoryRepository>(); <br/>. My problem now is my Presentation Layer has a dependency to my DAL.. Thanks bro for the help
Nice, but just a tip, does not create any dependency on your View, just ViewModels geekswithblogs.net/michelotti/archive/2009/10/25/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.