C# 在Global.asax中向服务层添加静态引用可以吗?
我正在单个项目ASP.NET Web表单应用程序的上下文中评估该包 我想轻松访问我的服务层。以下是全球战略的相关部分:C# 在Global.asax中向服务层添加静态引用可以吗?,c#,asp.net,unity-container,C#,Asp.net,Unity Container,我正在单个项目ASP.NET Web表单应用程序的上下文中评估该包 我想轻松访问我的服务层。以下是全球战略的相关部分: public class Global : HttpApplication { private static readonly IUnityContainer Container = new UnityContainer(); public static IEmployeeService Service { get; private set; } p
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>();
}
}
工作很好,但我以前没有采取这种方法,只是因为它似乎是好的。。。是吗?如果没有,我该怎么办
[编辑]为了要求的清晰
服务:
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>);
}
}
公共类EmployeeService:ieemployeeservice
{
私有只读存储库;
公共雇员服务(IRepository存储库)
{
this.repository=存储库;
}
公共IEnumerable GetEmployees()
{
返回this.repository.Find(
新的AllEmployeesQuery())
托利斯先生()
.Select(ObjectMapper.Map);
}
}
AllEmployeesQuery是一个规范。业务对象由AutoMapper映射到EF实体,反之亦然
谢谢,
理查德你想要像瘟疫一样避免这种方法。不要在应用程序级别共享数据库连接或资源;这将导致糟糕的性能和微妙的错误
我发现的最好的模型是为每个请求创建一个连接,并将其存储在当前请求中,以使该连接可用于请求生存期内可能发生的任何其他调用 以下代码是数据层中的静态属性。默认行为是在第一次获取时实例化连接并将其存储在请求中,随后对该属性的访问将返回先前创建的实例。该属性还允许用户在没有HttpContext的情况下显式重写该属性
protected static dataLayer DataContext
{
get
{
if (HttpContext.Current == null)
{
if (StaticContext == null)
StaticContext = new dataLayer();
return StaticContext;
}
if (HttpContext.Current.Items["dataLayer"] == null)
HttpContext.Current.Items["dataLayer"]= new dataLayer();
return (dataLayer)HttpContext.Current.Items["dataLayer"];
}
}
如何使用此.service.GetEmployees();?访问它??是不是应该是Global.Service.GetEmployees()没错,Global.Service.GetEmployees()你从哪里得知OP试图“共享数据库连接或资源”?
IEmployeeService
是否意味着共享资源一点也不清楚。“为每个请求创建一个连接并将其存储在当前请求中”-您何时释放该请求所持有的资源?请不要说GC。数据/服务层实现idisposable并具有终结器。当请求完成时,它将脱离上下文。共享资源是服务层,而不是数据层。服务层所做的就是接收和传回业务对象。存储库负责数据访问,必要时,它们都通过Unity和自定义的per HTTP请求连接起来,以确保在必要时丢弃资源。如果您的服务层只是一个代理,它只是将调用编组到远程服务,只要这些调用没有阻塞您的服务线程,那么共享引用在创建对您的服务的共享引用时应该不会对性能产生什么影响。
protected static dataLayer DataContext
{
get
{
if (HttpContext.Current == null)
{
if (StaticContext == null)
StaticContext = new dataLayer();
return StaticContext;
}
if (HttpContext.Current.Items["dataLayer"] == null)
HttpContext.Current.Items["dataLayer"]= new dataLayer();
return (dataLayer)HttpContext.Current.Items["dataLayer"];
}
}