私は以下のようにDI、UnitOfWork、およびリポジトリパターンを実装しています。この実装には専門家の意見が必要です。また、エラーに直面しています。
**Business Domain(Class Lib)**
-Contracts
-IWelcomeService.cs
-Repositories
-BaseRepository.cs
-DataRepository.cs
-IDataRepository.cs
**EF project Lib (I have autogenerated code for Existing Oracle DB)**
-MyEntities.cs (This is additional file I have added for IUnitOfWork implementation
**MVC3 Project (Left default folders for simplicity)**
-Factories
- UnityDependencyResolver.cs
UnityContainerSetup.cs
これらのカスタム実装のコードは次のとおりです
MyEntities.cs
public partial class MyEntities : ObjectContext, IUnitOfWork
{
#region IUnitOfWork Members
public void Save()
{
base.SaveChanges();
}
#endregion
}
IWelcomeService.cs
public interface IWelcomeService
{
string GetMessage();
}
BaseRepository.cs
public class BaseRepository
{
protected IUnitOfWork UnitOfWork { get; set; }
public BaseRepository(IUnitOfWork unitOfWork)
{
this.UnitOfWork = unitOfWork;
}
}
public interface IUnitOfWork
{
void Save();
}
DataRepository.cs
public class DataRepository : BaseRepository, IDataRepository
{
public DataRepository(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
#region IDataRepository Members
public string ChangeMessage()
{
return "Changed by repository";
}
#endregion
}
IDataRepository.cs
public interface IDataRepository
{
string ChangeMessage();
}
UnityDependencyResolver.cs
public class UnityDependencyResolver : IDependencyResolver
{
IUnityContainer _container;
public UnityDependencyResolver(IUnityContainer container)
{
_container = container;
}
#region IDependencyResolver Members
public object GetService(Type serviceType)
{
object instance;
try
{
instance = _container.Resolve(serviceType);
if (serviceType.IsAbstract || serviceType.IsInterface)
return null;
return instance;
}
catch (Exception)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.ResolveAll(serviceType);
}
#endregion
}
UnityContainerSetup.cs
public static void Setup()
{
var container = new UnityContainer();
container.RegisterType<IWelcomeService, WelcomeServices>();
container.RegisterType<IDataRepository, DataRepository>();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
HomeController.cs public class HomeController:Controller {private IWelcomeService _service;
public HomeController(IWelcomeService service)
{
_service = service;
}
public ActionResult Index()
{
ViewBag.Message = _service.GetMessage();
return View();
}
public ActionResult About()
{
return View();
}
}
問題:アプリケーションを実行すると、次のエラーが発生します:「このオブジェクトにパラメーターなしのコンストラクターが定義されていません」
質問:UnitOfWorkはいつ開始されますか?Unityコンテナのセットアップ中にIUnitOfWorkを注入するにはどうすればよいですか?