界面层---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;
}
}