如何在GenericRepository和UnitOfWork中使用DependencyInjection

时间:2019-01-15 09:56:14

标签: c# entity-framework dependency-injection unit-of-work generic-repository

我有一个具有这种设计的WindowsForm项目:
DAL(GenericRepository => UnitOfWork)=> BLL(Service)=> UI
并使用EntityFramWork,Interface,GenericRepository,依赖注入

我在存储库(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();  
       } 
} 

UnitOfWork(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;  
       } 

Service(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();  
       } 

MyForm(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()”说需要在()中使用参数单元作为单位工作量,但我们不应该在UI中使用unitofwork

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

1 个答案:

答案 0 :(得分:0)

将新项目添加到名称为“ Configure”的解决方案中
将NuGet中的castle.windsor添加到所有项目中
向该项目添加名称为“ 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>());
   }

并在用户界面中编辑代码

 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();

这是核心工作