2

Is it possible to expose class public properties in different class through IOC. I am creating an instance of Interface but i am not able to access public properties of class. I am using Unity.WebApi for resolving dependencies.

TransactionService Class

public class TransactionService : ITransactionService
{
    private readonly IMRepository _mRepository;
    private readonly IFService _fGateway;

     public TransactionService(IMbaRepository mbaRepository, IFpnService fpnService)
    {
        _mRepository = mRepository;
        _fGateway = fService;
    }

     private List<Transaction> SearchTransacionsByUser(FUser objFUser)
    {

         foreach (var item in something)
         {
              //can't use _fGateway to set properties because Interface 
              //  don't implement them
              _fGateway.OID = objFUser.OID.ToString();
             _fGateway.Amount = objFUser.Amount;

             _fGateway.Search(criteria);

         }

    }



}

FService class

public class FService : IFpService
{

  public string _OID { get; set; }
  public decimal _Amount{ get; set; }

  public TransactionResponse Search(string criteria)
  {

         TransactionOperationInput _input;
            _input = new TransactionOperationInput()
            {
                Criteria = _criteria,
                OID = _OID,
                Amount = _Amount
            };

           // search transactions
     }



 }
user1263981
  • 2,953
  • 8
  • 57
  • 98
  • Are you able to update interface to include missing property? This looks like a design issue. – Nkosi Nov 14 '18 at 18:34
  • Otherwise you would need to cast the interface. This however is a bad design as it tightly couples to implementation concerns which goes against the very reason for injecting abstractions.. – Nkosi Nov 14 '18 at 18:41
  • I don't have any control over _input = new TransactionOperationInput() bcause its part of dll file. – user1263981 Nov 14 '18 at 18:57
  • I was referring to `IFService` interface – Nkosi Nov 14 '18 at 18:59

1 Answers1

1

If you are in control of the services then refactor the interfaces to expose the desired members

public interface IFService {
   TransactionResponse Search(TransactionOperationInput input);
}

Make sure the derived implementation has those members

public class FService : IFpService {

    public TransactionResponse Search(TransactionOperationInput input) { 

           // search transactions
    }
}

And that the dependent class uses the correct abstraction

public class TransactionService : ITransactionService {
    private readonly IMRepository _mRepository;
    private readonly IFService fGateway;

    public TransactionService(IMbaRepository mbaRepository, IFService fService) {
        _mRepository = mRepository;
        fGateway = fService;
    }

    private List<Transaction> SearchTransacionsByUser(FUser objFUser) {    
        foreach (var item in something) {

            TransactionOperationInput input = new TransactionOperationInput() {
                Criteria = _criteria,
                OID =  objFUser.OID.ToString(),
                Amount = objFUser.Amount,
            };

             fGateway.Search(input);

            //...
         }

        //...
    }
}

Finally make sure the register the appropriate abstractions and implementations with the IoC/DI container.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • can IFService expose an object of TransactionOperationInput class which is in dll? I was thinking about having TransactionOperationInput object in TransactionService class where i can set all inputs and then pass it as argument to search method in Fservice. – user1263981 Nov 14 '18 at 19:11
  • @user1263981 yes you can do that. That is a more solid design as well. – Nkosi Nov 14 '18 at 19:16
  • @user1263981 check updated answer based on your comment – Nkosi Nov 14 '18 at 19:21
  • @user1263981 also if you find the answers useful remember to vote them up as well. – Nkosi Nov 14 '18 at 19:23
  • Thanks. Search(TransactionOperationInput input) is a private method in TransactionService. TransanctionService implements two methods and it also has one private method which is being called in implemented method with in the class. – user1263981 Nov 14 '18 at 19:28