c# - Chain of dependencies using Unity Dependency Injection and Xamarin.Forms -
i'm new dependency injection, , i'm developing app using xamarin.forms, prism , unity. far know, when using di want supply/inject services classes don't have them. resulting in classes have no knowledge of service implementations.
that means using constructor injection instead of using container resolve services. seen here http://structuremap.github.io/quickstart.
my setup:
[assembly: xamarin.forms.dependency(typeof(foo))] public class foo : ifoo { public ibar bar { get; private set; } public foo(ibar bar) { bar = bar; } } [assembly: xamarin.forms.dependency(typeof(bar))] public class bar : ibar { } now if try resolve ifoo exception thrown: system.missingmethodexception: default constructor not found type foo going on here?
i have tried adding empty constructor foo, results in bar being null , forces me resolve iunitycontainer.
roughly put far can tell xamarin forms dependency services(which looks using) doesn't provide constructor injection. if want constructor based injection need use different container service.
if want use built in forms service following changes code should work..
[assembly: xamarin.forms.dependency(typeof(foo))] public class foo : ifoo { public ibar bar { get; set; } public foo(ibar bar) { bar = bar; } } [assembly: xamarin.forms.dependency(typeof(bar))] public class bar : ibar { } then object set:
var myfoo = xamarin.forms.dependency.get<ifoo>(); myfoo.bar = xamarin.forms.dependency.get<ibar>(); otherwise might want di framework.
Comments
Post a Comment