我正在评估单个项目ASP.NET Web窗体应用程序上下文中的Highway.Data.EntityFramework.Unity程序包。在Global.asax中添加静态引用到服务层可以吗?
我想要轻松访问我的服务层。这里是全球的相关部分:
public class Global : HttpApplication
{
private static readonly IUnityContainer Container = new UnityContainer();
public static IEmployeeService Service { get; private set; }
protected void Application_Start(object sender, EventArgs e)
{
Container.BuildHighway();
Container.RegisterType<IMappingConfiguration, EntityMapping>();
Container.RegisterType<IEmployeeService, EmployeeService>(
new PerRequestLifetimeManager());
Container.RegisterType<IRepository, Repository>(
new PerRequestLifetimeManager());
Container.RegisterType<IDataContext, DataContext>(
new PerRequestLifetimeManager(),
new InjectionConstructor("DataContext", new EntityMapping()));
Service = Container.Resolve<IEmployeeService>();
}
}
在我的客户,我可以像这样访问:
this.Employees.DataSource = this.service.GetEmployees();
this.Employees.DataBind();
工作正常,但我之前只是因为它似乎并没有采取这种方法好的...好吧,是吗?如果不是,我该怎么办?
[编辑]请求清晰。
服务:
public class EmployeeService : IEmployeeService
{
private readonly IRepository repository;
public EmployeeService(IRepository repository)
{
this.repository = repository;
}
public IEnumerable<Employee> GetEmployees()
{
return this.repository.Find(
new AllEmployeesQuery())
.ToList()
.Select(ObjectMapper.Map<EmployeeEntity, Employee>);
}
}
AllEmployeesQuery是一个规范。业务对象由AutoMapper映射到EF实体,反之亦然。
感谢, 理查德
如何通过this.service.GetEmployees()访问它; ??不应该是Global.Service.GetEmployees() – kabaros
没错,Global.Service.GetEmployees() – Richard