简体   繁体   中英

Sequential Implementation for one call using unity dependency injection

What is the best way to implement sequential implementation in async mode using unity dependency injection. If I call one method to create order, it should create it in two different implementations.

My Controller:

public class OrderController : BaseController
{
    private IOrderRepository orderService;

    public OrderController (IOrderRepository service)
    {
       this.orderService = service;
    }

    public ActionResult Create (OrderOL obj)
    {
       // here I want this to do two implementations
       // first on OrderRepositorySQL 
       // and then after success response, 
       // it should implement in OrderRepositoryMongo as async method
       orderService.Create(obj)
       return view();
    }

}

My Interface:

public interface IOrderRepository {
   int Create (OrderOL obj);
}

My Implementing class 1

public class OrderRepositorySQL : IOrderRepository
{
    public int Create (OrderOL obj)
    {
         // logic to Create order in Data store 1
    }
}

My Implementing class 2

public class OrderRepositoryMongo : IOrderRepository
{
    public int Create (OrderOL obj)
    {
         // logic to Create order in Data store 2
    }
}

Global.asax AppStart event using ASP.NET MVC Unity

container.RegisterType<IOrderRepository, OrderRepositorySQL>();
container.RegisterType<IOrderRepository, OrderRepositoryMongo>();

Application Details

  • C#.NET 4.5
  • ASP.NET MVC 5
  • Unity 3.0

You can't do that, you can only resolve one component at time. Actually, these two lines of code will conflict either at compile time or at runtime when you try to resolve IOrderRepository ...

container.RegisterType<IOrderRepository, OrderRepositorySQL>();
container.RegisterType<IOrderRepository, OrderRepositoryMongo>();

Basically, you will need to use Named Type Registrations or inject a wrapper service to the controller that will resolve these two components (OrderRepositorySQL and OrderRepositoryMongo) and do the processing required.

Example

Say you have an OrderProcessor class that processes orders and implements the IOrderProcessor interface, right?

public interface IOrderProcessor
{
    ProcessResult Process(OrderOL obj);
}

public class OrderProcessor : IOrderProcessor
{
       public ProcessResult Process(OrderOL obj)
       {
            //TODO:
            // 1- resolve OrderRepositorySQL 
            // 2- create order
            // 3- resolve the other repository
            // 4- create order
            // 5- return the operation result
       }
}

Then your controller will be injected the order processor...

public class OrderController : BaseController
{
    private IOrderProcessor ordeProcessor;

    public OrderController (IOrderProcessor processor)
    {
       this.ordeProcessor= processor;
    }

    public ActionResult Create (OrderOL obj)
    {
       var result = processor.Process(obj)
       return view();
    }
}

To register the components...

container.RegisterType<IOrderRepository, OrderRepositorySQL>("sql", null);
container.RegisterType<IOrderRepository, OrderRepositoryMongo>("mongo", null);

to resolve them...

container.Resolve<IOrderRepository>("sql");
container.Resolve<IOrderRepository>("mongo");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM