NHibernate3.2+Asp.net MVC3+Extjs 4.0.2项目实践(三):Services中间业务层
Services中间业务层是供Controller调用,调用数据访问层方法实现业务操作,包括service接口与实现。
ILocation_CountryService.cspublic interface ILocation_CountryService
{
IList<Location_Country> GetLocation_Countries();
IList<Location_Country> GetIndistinctCountries(Country_Query_Index country_Query_Index, object o);
bool CreateLocation_Country(Location_Country location_Country);
bool UpdateLocation_Country(Location_Country location_Country);
bool DeleteLocation_Country(Location_Country location_Country);
Location_Country FindLocation_Country(Country_Query_Index country_Query_Index, object o);
}
public class Location_CountryService : ILocation_CountryService
{
private TESZ.Data.DataAccess.ILocation_CountryRepository _iLocation_CountryRepository;
public Location_CountryService(TESZ.Data.DataAccess.ILocation_CountryRepository iLocation_CountryRepository)
{
_iLocation_CountryRepository = iLocation_CountryRepository;
}
public IList<TESZ.Data.Model.Location_Country> GetLocation_Countries()
{
return _iLocation_CountryRepository.FindAll();
}
public IList<TESZ.Data.Model.Location_Country> GetIndistinctCountries(Country_Query_Index country_Query_Index, object o)
{
return _iLocation_CountryRepository.FindIndistinct(country_Query_Index, o);
}
public TESZ.Data.Model.Location_Country FindLocation_Country(Country_Query_Index country_Query_Index, object o)
{
return _iLocation_CountryRepository.FindOne(country_Query_Index, o);
}
public bool CreateLocation_Country(TESZ.Data.Model.Location_Country location_Country)
{
return _iLocation_CountryRepository.Create(location_Country);
}
public bool UpdateLocation_Country(TESZ.Data.Model.Location_Country location_Country)
{
return _iLocation_CountryRepository.Update(location_Country);
}
public bool DeleteLocation_Country(TESZ.Data.Model.Location_Country location_Country)
{
return _iLocation_CountryRepository.Delete(location_Country);
}
}
再来看一下应用程序如何调用中间业务层。在应用程序启动时,需要装载数据访问层和中间业务层的接口与接口实现,具体是通过Bootstrapper启动装载类来实现,看一下Global.asax.cs和Bootstrapper.cs的代码:
Global.asax.cspublic class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Bootstrapper.Init();
}
}
public static partial class Bootstrapper
{
private static IUnityContainer container;
static Bootstrapper()
{
container = new UnityContainer();
}
public static void Init()
{
//Location
container.RegisterType<ILocation_CountryRepository, Location_CountryRepository>();
container.RegisterType<TESZ.Services.ILocation_CountryService, TESZ.Services.Location_CountryService>();
container.RegisterType<ILocation_StateProvinceRepository, Location_StateProvinceRepository>();
container.RegisterType<TESZ.Services.ILocation_StateProvinceService, TESZ.Services.Location_StateProvinceService>();
//Common
container.RegisterType<ICommon_DateRepository, Common_DateRepository>();
container.RegisterType<TESZ.Services.ICommon_DateService, TESZ.Services.Common_DateService>();
//System
container.RegisterType<ISystem_PurviewRepository, System_PurviewRepository>();
container.RegisterType<TESZ.Services.ISystem_PurviewService, TESZ.Services.System_PurviewService>();
container.RegisterType<IViewPageActivator, ViewPageActivator>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
}
public class UnityDependencyResolver : IDependencyResolver
{
private IUnityContainer container;
public UnityDependencyResolver(IUnityContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
//额外操作,如日志
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
//额外操作,如日志
return Enumerable.Empty<object>();
}
}
}
public class ViewPageActivator : IViewPageActivator
{
public object Create(ControllerContext controllerContext, Type type)
{
return Activator.CreateInstance(type);
}
}
这几个启动配置类都置于Conguration文件夹下。