开发者

How to convert Mvc Movie aaplication to 3 Layer (Project: Data,Business and Web)

开发者 https://www.devze.com 2023-03-16 15:37 出处:网络
I have created sample Movie application from http开发者_运维问答://www.asp.net/mvc in this sample application contain Single Layer. But I would like to make 3 Layer applications are DataLayer, Busines

I have created sample Movie application from http开发者_运维问答://www.asp.net/mvc in this sample application contain Single Layer. But I would like to make 3 Layer applications are DataLayer, BusinessLayer and WebLayer.

Is anybody has idea for that?

Any answer or suggestion would be appreciated!

Thanks, Imdadhusen


You can use any of the new MVC wizards in Visual Studio to create your MVC application. This would become the UI layer. You can name this project, for example, Movie.UI. Once you do this, you can go to File | Add | New Project... and add a class library project for your data access layer. You can name this project Movie.Data. You then repeat this step, and add another class library project to your solution, which will be for your business logic layer (i.e. Movie.Business).

Once you have all three projects in your solution, you add the necessary references between them. Normally your UI project will reference your business layer project, and your business layer project will in turn reference your data access project.

Update

This communication you are referring to is precisely what you achieve by referencing a project. When you add a reference from project A to project B, you establish a way for project A to access public types from project B.

I've set up the simplest possible sample I could come with to show how to communicate between projects:

In your Movie.Data project, you add a MovieRepository class:

MovieRepository.cs

namespace Movie.Data
{
    public class MovieRepository
    {
        public string[] GetMovies()
        {
            return new[] 
            { 
                "Gone with the Wind", 
                "Back to the Future", 
                "The Godfather", 
            };
        }
    }
}

Then in your Movie.UI project, you first need to add a reference to your Movie.Data project:

  1. Right click on References
  2. Click on Add Reference...
  3. Under Project References you select Movie.Data

Finally, your Movie.UI project (for simplicity I made it a console app):

Program.cs

using System;
using Movie.Data;

namespace Movie.UI
{
    class Program
    {
        private static MovieRepository _repo = new MovieRepository();

        static void Main(string[] args)
        {
            foreach (var movie in _repo.GetMovies())
            {
                Console.WriteLine(movie);
            }
        }
    }
}

Hope this helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消