MVC5层架构

界面层---TestMvc(IOC-StructureMap)

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            BootStrapper.ConfigureStructureMap(); 
            RegisterRoutes(RouteTable.Routes);
        }


 

    public class BootStrapper
    {
        public static void ConfigureStructureMap()
        {
            // Initialize the registry
            ObjectFactory.Initialize(x =>
            {
                x.AddRegistry();
            });
        }

        public class ModelRegistry : Registry
        {
            public ModelRegistry()
            {
                // registry
                ForRequestedType().TheDefault.Is.OfConcreteType();
                // service
                ForRequestedType().TheDefault.Is.OfConcreteType();
            }
        }
    }


 

表示层---TestControllers

 

            IProductService service = ObjectFactory.GetInstance();
            IList products = service.GetAllProductsIn(1);
            ViewData["Message"] = products[0].id + products[0].name;
            return View();

 

服务层---TestService

 

    public interface IProductService
    {
        IList GetAllProductsIn(int categoryId);
    }

 

    public class ProductService : IProductService
    {
        private IProductRepository _productRepository;

        public ProductService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        public IList GetAllProductsIn(int categoryId)
        {
            return _productRepository.GetAllProductsIn(categoryId);
        }
    }

 

领域层---TestModel

    public class Product
    {
        public string id;
        public string name;
    }

 

数据层---TestRepository

   public interface IProductRepository
    {
        IList GetAllProductsIn(int categoryId);
    }


 

   public class ProductRepository : IProductRepository
    {
        public IList GetAllProductsIn(int categoryId)
        {
            IList products = new List();
            products.Add(new Product { id = "1", name = "liufuchu" });
            return products;
        }
    }

你可能感兴趣的:(重构与模式)