概述
界面层---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<ModelRegistry>();
});
}
public class ModelRegistry : Registry
{
public ModelRegistry()
{
// registry
ForRequestedType<IProductRepository>().TheDefault.Is.OfConcreteType<ProductRepository>();
// service
ForRequestedType<IProductService>().TheDefault.Is.OfConcreteType<ProductService>();
}
}
}
表示层---TestControllers
IProductService service = ObjectFactory.GetInstance<IProductService>();
IList<Product> products = service.GetAllProductsIn(1);
ViewData["Message"] = products[0].id + products[0].name;
return View();
服务层---TestService
public interface IProductService
{
IList<Product> GetAllProductsIn(int categoryId);
}
public class ProductService : IProductService
{
private IProductRepository _productRepository;
public ProductService(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public IList<Product> GetAllProductsIn(int categoryId)
{
return _productRepository.GetAllProductsIn(categoryId);
}
}
领域层---TestModel
public class Product
{
public string id;
public string name;
}
数据层---TestRepository
public interface IProductRepository
{
IList<Product> GetAllProductsIn(int categoryId);
}
public class ProductRepository : IProductRepository
{
public IList<Product> GetAllProductsIn(int categoryId)
{
IList<Product> products = new List<Product>();
products.Add(new Product { id = "1", name = "liufuchu" });
return products;
}
}
最后
以上就是爱笑秀发为你收集整理的MVC5层架构的全部内容,希望文章能够帮你解决MVC5层架构所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复