単一のプロジェクトASP.NETWebフォームアプリケーションのコンテキストでHighway.Data.EntityFramework.Unityパッケージを評価しています。
サービスレイヤーに簡単にアクセスしたい。グローバルの関連部分は次のとおりです。
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エンティティにマップされ、その逆も同様です。
ありがとう、リチャード