NHibernate3.2+Asp.net MVC3+Extjs 4.0.2项目实践(三):Services中间业务层

  Services中间业务层是供Controller调用,调用数据访问层方法实现业务操作,包括service接口与实现。

ILocation_CountryService.cs
隐藏行号 复制代码
  1. public interface ILocation_CountryService
    
  2.     {
    
  3.         IList<Location_Country> GetLocation_Countries();
    
  4.         IList<Location_Country> GetIndistinctCountries(Country_Query_Index country_Query_Index, object o);
    
  5.         bool CreateLocation_Country(Location_Country location_Country);
    
  6.         bool UpdateLocation_Country(Location_Country location_Country);
    
  7.         bool DeleteLocation_Country(Location_Country location_Country);
    
  8.         Location_Country FindLocation_Country(Country_Query_Index country_Query_Index, object o);
    
  9.     }
    
ILocation_CountryService.cs
隐藏行号 复制代码
  1. public class Location_CountryService : ILocation_CountryService
    
  2.     {
    
  3.         private TESZ.Data.DataAccess.ILocation_CountryRepository _iLocation_CountryRepository;
    
  4. 
    
  5.         public Location_CountryService(TESZ.Data.DataAccess.ILocation_CountryRepository iLocation_CountryRepository)
    
  6.         {
    
  7.             _iLocation_CountryRepository = iLocation_CountryRepository;
    
  8.         }
    
  9. 
    
  10.         public IList<TESZ.Data.Model.Location_Country> GetLocation_Countries()
    
  11.         {
    
  12.             return _iLocation_CountryRepository.FindAll();
    
  13.         }
    
  14. 
    
  15.         public IList<TESZ.Data.Model.Location_Country> GetIndistinctCountries(Country_Query_Index country_Query_Index, object o)
    
  16.         {
    
  17.             return _iLocation_CountryRepository.FindIndistinct(country_Query_Index, o);
    
  18.         }
    
  19. 
    
  20.         public TESZ.Data.Model.Location_Country FindLocation_Country(Country_Query_Index country_Query_Index, object o)
    
  21.         {
    
  22.             return _iLocation_CountryRepository.FindOne(country_Query_Index, o);
    
  23.         }
    
  24. 
    
  25.         public bool CreateLocation_Country(TESZ.Data.Model.Location_Country location_Country)
    
  26.         {
    
  27.             return _iLocation_CountryRepository.Create(location_Country);
    
  28.         }
    
  29. 
    
  30.         public bool UpdateLocation_Country(TESZ.Data.Model.Location_Country location_Country)
    
  31.         {
    
  32.             return _iLocation_CountryRepository.Update(location_Country);
    
  33.         }
    
  34. 
    
  35.         public bool DeleteLocation_Country(TESZ.Data.Model.Location_Country location_Country)
    
  36.         {
    
  37.             return _iLocation_CountryRepository.Delete(location_Country);
    
  38.         }
    
  39.     }
    

    再来看一下应用程序如何调用中间业务层。在应用程序启动时,需要装载数据访问层和中间业务层的接口与接口实现,具体是通过Bootstrapper启动装载类来实现,看一下Global.asax.cs和Bootstrapper.cs的代码:

 Global.asax.cs
隐藏行号 复制代码
  1. public class MvcApplication : System.Web.HttpApplication
    
  2.     {
    
  3.         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    
  4.         {
    
  5.             filters.Add(new HandleErrorAttribute());
    
  6.         }
    
  7. 
    
  8.         public static void RegisterRoutes(RouteCollection routes)
    
  9.         {
    
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
  11. 
    
  12.             routes.MapRoute(
    
  13.                 "Home", // Route name
    
  14.                 "{controller}/{action}/{id}", // URL with parameters
    
  15.                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    
  16.             );
    
  17. 
    
  18.         }
    
  19. 
    
  20.         protected void Application_Start()
    
  21.         {
    
  22.             AreaRegistration.RegisterAllAreas();
    
  23. 
    
  24.             RegisterGlobalFilters(GlobalFilters.Filters);
    
  25.             RegisterRoutes(RouteTable.Routes);
    
  26. 
    
  27.             Bootstrapper.Init();
    
  28.         }
    
  29.     }
    
 Bootstrapper.cs
隐藏行号 复制代码
  1. public static partial class Bootstrapper
    
  2.     {
    
  3.         private static IUnityContainer container;
    
  4. 
    
  5.         static Bootstrapper()
    
  6.         {
    
  7.             container = new UnityContainer();
    
  8.         }
    
  9. 
    
  10.         public static void Init()
    
  11.         {
    
  12.             //Location
    
  13.             container.RegisterType<ILocation_CountryRepository, Location_CountryRepository>();
    
  14.             container.RegisterType<TESZ.Services.ILocation_CountryService, TESZ.Services.Location_CountryService>();
    
  15.             container.RegisterType<ILocation_StateProvinceRepository, Location_StateProvinceRepository>();
    
  16.             container.RegisterType<TESZ.Services.ILocation_StateProvinceService, TESZ.Services.Location_StateProvinceService>();
    
  17. 
    
  18.             //Common
    
  19.             container.RegisterType<ICommon_DateRepository, Common_DateRepository>();
    
  20.             container.RegisterType<TESZ.Services.ICommon_DateService, TESZ.Services.Common_DateService>();
    
  21. 
    
  22.             //System
    
  23.             container.RegisterType<ISystem_PurviewRepository, System_PurviewRepository>();
    
  24.             container.RegisterType<TESZ.Services.ISystem_PurviewService, TESZ.Services.System_PurviewService>();
    
  25. 
    
  26.             container.RegisterType<IViewPageActivator, ViewPageActivator>();
    
  27.             DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    
  28.         }
    
  29.     }
    
 UnityDependencyResolver.cs
隐藏行号 复制代码
  1. public class UnityDependencyResolver : IDependencyResolver
    
  2.     {
    
  3.         private IUnityContainer container;
    
  4. 
    
  5.         public UnityDependencyResolver(IUnityContainer container)
    
  6.         {
    
  7.             this.container = container;
    
  8.         }
    
  9. 
    
  10.         public object GetService(Type serviceType)
    
  11.         {
    
  12.             try
    
  13.             {
    
  14.                 return container.Resolve(serviceType);
    
  15.             }
    
  16.             catch (ResolutionFailedException)
    
  17.             {
    
  18.                 //额外操作,如日志
    
  19.                 return null;
    
  20.             }
    
  21.         }
    
  22. 
    
  23.         public IEnumerable<object> GetServices(Type serviceType)
    
  24.         {
    
  25.             try
    
  26.             {
    
  27.                 return container.ResolveAll(serviceType);
    
  28.             }
    
  29.             catch (ResolutionFailedException)
    
  30.             {
    
  31.                 //额外操作,如日志
    
  32.                 return Enumerable.Empty<object>();
    
  33.             }
    
  34.         }
    
  35.     }
    
 ViewPageActivator.cs
隐藏行号 复制代码
  1. public class ViewPageActivator : IViewPageActivator 
    
  2.     {
    
  3.         public object Create(ControllerContext controllerContext, Type type)
    
  4.         {
    
  5.             return Activator.CreateInstance(type);
    
  6.         }
    
  7.     }
    

     这几个启动配置类都置于Conguration文件夹下。

posted @ 2011-10-31 13:43  Leo Wuang  阅读(2004)  评论(0编辑  收藏  举报