C# 使用Unity容器的IUnitOfWork到DomainService构造函数

C# 使用Unity容器的IUnitOfWork到DomainService构造函数,c#,silverlight,unity-container,data-access-layer,ria,C#,Silverlight,Unity Container,Data Access Layer,Ria,我创建了Silverlight应用程序,我希望通过WCF RIA服务实现它。我的解决方案中有3个项目: 包含所有数据库逻辑和实体的数据访问层库。我将使用IUnitOfWork接口与它进行通信: public interface IUnitOfWork : IDisposable { IRepository<Customer> Customers { get; } IRepository<Product> Products { get; } I

我创建了Silverlight应用程序,我希望通过WCF RIA服务实现它。我的解决方案中有3个项目:

  • 包含所有数据库逻辑和实体的数据访问层库。我将使用IUnitOfWork接口与它进行通信:

    public interface IUnitOfWork : IDisposable
    {
         IRepository<Customer> Customers { get; }
         IRepository<Product> Products { get; }
         IRepository<Order> Orders { get; }
         void Save();
    }
    
  • 客户项目(用WPF编写)


  • 所以,我想使用Unity IoC容器将接口实现传递到服务中。我不知道在哪里需要创建定制服务工厂或类似的东西,以及在哪里注册以供系统使用。例如,我知道在ASP.NET MVC中有一个需要派生的DefaultControllerFactory类。然后将IoC绑定放入其中,然后将其注册到Global.asax.cs文件中。你能帮帮我吗。谢谢。

    域服务
    公开了一个名为
    DomainService.DomainServiceFactory
    的静态属性

    您需要一个实现
    IDomainServiceFactory

    interface IDomainServiceFactory
    {
      DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context);
      void ReleaseDomainService(DomainService domainService)
    }
    
    我复制并粘贴了Fredrik Norman的一篇博客文章

    在您的
    Global.asax中

    protected void Application_Start(object sender, EventArgs e)
    {
       DomainService.Factory = new MyDomainServiceFactory();
    
       UnityContainer.RegisterType<StoreService>();
       UnityContainer.RegisterType<IUnitOfWork, UnitOfWorkImpl>();
    }
    
    受保护的无效应用程序\u启动(对象发送方,事件参数e)
    {
    DomainService.Factory=新的MyDomainServiceFactory();
    UnityContainer.RegisterType();
    UnityContainer.RegisterType();
    }
    
    谢谢!这才是我真正需要的。我以前亲自实施过这种解决方案)
    public class MyDomainServiceFactory : IDomainServiceFactory
    {
    
        public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
        {
            var service = Global.UnityContainer.Resolve(domainServiceType) as DomainService;
            service.Initialize(context);
    
            return service;
        }
    
    
        public void ReleaseDomainService(DomainService domainService)
        {
            domainService.Dispose();
        }
    }
    
    protected void Application_Start(object sender, EventArgs e)
    {
       DomainService.Factory = new MyDomainServiceFactory();
    
       UnityContainer.RegisterType<StoreService>();
       UnityContainer.RegisterType<IUnitOfWork, UnitOfWorkImpl>();
    }