如何在 GenericRepository 和 UnitOfWork 中使用 DependencyInjection

How Use DependencyInjection in GenericRepository and UnitOfWork

我有一个采用这种设计的 WindowsForm 项目:
DAL(GenericRepository => UnitOfWork) => BLL(Service) => UI
并使用 EntityFramWork、Interface、GenericRepository、Dependency Injection

存储库中我的代码(DAL):

public class Repository : RepositoryBase, IDisposable, IRepository where T : class  
{  
       private readonly DbSet dbSet;  
       private bool disposed = false;
       public Repository(GlobalERPEntities dbContext)  
       {  
           DBContext = dbContext;  
           dbSet = DBContext.Set();  
       }    
       public virtual IEnumerable GetAll()  
       {  
           return dbSet.ToList();  
       } 
} 

工作单位(DAL):

public class UnitOfWork : RepositoryBase, IUnitOfWork, IDisposable  
   {  
       private Dictionaryobject> repositories;  
       private bool disposed = false;  

       public UnitOfWork(GlobalERPEntities dbContext)  
       {  
           DBContext = dbContext;  
       }  

       public IRepository Repository() where T : class  
       {  
           if (repositories == null)  
           {  
               repositories = new Dictionaryobject>();  
           }  

           if (repositories.Keys.Contains(typeof(T)) == true)  
           {  
               return repositories[typeof(T)] as Repository;  
           }  
           Repository repo = new Repository(DBContext);  
           repositories.Add(typeof(T), repo);  
           return repo;  
       } 

服务(BLL):

public class Service_HR_Person : IService_HR_Person ,  IDisposable  
   {  
       private readonly IUnitOfWork UnitOfWork;  

       public Service_HR_Person(IUnitOfWork unitOfWork)  
       {  
           UnitOfWork = unitOfWork;  
       }  

       public virtual IEnumerable GetAll()  
       {  
           return UnitOfWork.Repository().GetAll().ToList();  
       } 

我的表格(UI) :

using (Service_HR_Person srvPerson = new Service_HR_Person())  
               {  
                   srvPerson.Delete(base.rowid);  
                   try  
                   {  
                       srvPerson.Save();
                       MessageManager.Show(Enums.MessageBoxType.InformationTransactionSuccessfully);  
                   }  
                   catch (Exception ex)  
                   {  
                       MessageManager.Show(ErrorManager.ProccessException(ex), Enums.MessageBoxType.Error);  
                   }  
               }  

我知道不应该在 UI 层中使用 DAL 层并且 BLL 在 DAL 和 UI 之间 但我在 ui

中有错误
using (Service_HR_Person srvPerson = new Service_HR_Person())

"new Service_HR_Person()" 说在 () 中需要一个 arguman 是 unitofwork 但我们不应该在 UI

中使用 unitofwork

我阅读了一些使用 IOC、ninject、bootstraper 和...的文章和示例,但我无法编写真正的代码
如何使用 Ninject 或 IOC?
请帮我写代码
谢谢

将新项目添加到名称为 "Configure"
的解决方案 从 NuGet 添加 castle.windsor 到所有项目
将 class 添加到此项目,名称为 "Bootstrapper" 并编写此代码

public static WindsorContainer Container = null;

   public static void WireUp()
   {
                    Container = new WindsorContainer();
                    Container.Register(Component.For<GlobalERPEntities>());
                    Container.Register(Component.For<IUnitOfWork>().ImplementedBy<UnitOfWork>());
                    Container.Register(Component.For<IService_HR_Person>().ImplementedBy<Service_HR_Person>());
   }

并在 UI

中编辑您的代码
 using (Service_HR_Person srvPerson = Bootstrapper.Container.Resolve<Service_HR_Person>())
                {
                    srvPerson.Delete(base.rowid);
                    try
                    {
                        srvPerson.Save();
                        RemoveRow();
                        MessageManager.Show(Enums.MessageBoxType.InformationTransactionSuccessfully);
                    }
                    catch (Exception ex)
                    {
                        MessageManager.Show(ErrorManager.ProccessException(ex), Enums.MessageBoxType.Error);
                    }
                }

这一行

 using (Service_HR_Person srvPerson = Bootstrapper.Container.Resolve<Service_HR_Person>())

并使用此代码编辑 Program.cs

 static void Main(string[] argss)
        {
            Bootstrapper.WireUp();

这是正确的工作