AliSoftware / Dip

Simple Swift Dependency container. Use protocols to resolve your dependencies and avoid singletons / sharedInstances!
MIT License
977 stars 72 forks source link

Interception for NSObject subclasses #72

Closed ilyapuchka closed 7 years ago

ilyapuchka commented 8 years ago

That actually has two sides: 1. Using Dip from Objective-C code. That will require adding register/resolve apis that will accept type as parameter (#75)

  1. Providing some advanced features that require more flexible runtime and will work only for NSObjects (like interception using NSProxy, anything else?)

If something of that should be implemented it will make sense to do it in a separate repo and maybe also create Dip org and gather there all repos. Also would let us to extract simple app to it's own repo, living only playground here.

ilyapuchka commented 8 years ago

First option (after just removing generic arguments from register/resolve API) looks possible but not very attractive in terms of API.

container.register(Service.self) { ServiceImp() }
let service = try! container.resolve(Service.self) as! Service

Gets worse with runtime arguments:

container.register(Service.self, argumentsType: (Int, Int).self) { args in
  let (a1, a2) = args as! (Int, Int)
  return ServiceImp(a1, a2)   
}

let service = try! container.resolve(Service.self, withArguments: (arg1, arg2)) as! Service

Don't think it makes much sense. Second option should be still investigated. At least it should be possible to implement minimal interception functionality with just getting callback before calling any method.

ilyapuchka commented 8 years ago

To avoid moving feature dependent on Foundation we can use compile directives and simply do not include this code for not Darwin architecture.