简体   繁体   中英

NancyFx and TinyIoc

I am new to dependency injection.I am having problems understanding how to use the nancyFx bootstrapper and TinyIoc. I want to reference a dependency but can not understatnd how to do it. I can not seem to find the correct info anywhere or I am just misunderstanding it.This is my code:

  public class Startup1
{
    public void Configuration(IAppBuilder app)
    {
        app.UseNancy();
    }
}

My interface:

public interface IPerson
{
    string Getname();
}

My implementation:

public class Person : IPerson
{
    public string Name { get; set; }
    public string Getname()
    {
        return Name;
    }
}

My program

   static void Main(string[] args)
    {
        Person person = new Person();
        person.Name = "Bill";
        string uri = "http://localhost:8080/";
        using (WebApp.Start<Startup1>(uri))
        {
            Console.WriteLine("Started");
            Console.ReadKey();
            Console.WriteLine("Stopping");
        }

    }

My nancy bootstrapper:

public class MyNancyBoot : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        //base.ConfigureApplicationContainer(container);
          container.Register<IPerson>(new Person());

    }
}

My nancyModule:

public class MyNancyModule : NancyModule
{
    public MyNancyModule(IPerson person)
    {
        Get["/"] = _ =>
        {
            var x = TinyIoCContainer.Current.Resolve<IPerson>();
            return "OK";

        };
    }
}

This return an error in MyNancyModule.

 var x = TinyIoCContainer.Current.Resolve<IPerson>();

Ok so how do I get my person into MyNancyModule?

I want to reference a dependency but can not understatnd how to do it.

I think there are a couple of things to look at here.

You are mistakenly thinking that you need to somehow pass in, on the controller constructor, any type you may want to return from your Get["/"] controller operation. This is not the case. If one of the service operations happens to return a Person type, you do not need to inject that person type into the constructor. Rather you could just do something like:

Get["/"] = _ =>
    {
        var x = new List<Person>();
        return Response.AsJson(x); 
    };

Obviously this service operation is of very limited use, as it will always return an empty list of type Person.

So what you need is a dependency which gets Persons. Eg:

interface IGetPersons
{
    List<Person> GetAll();
}

In order for this to be used inside the controller, Tiny Ioc will need to know about it. So that is where you can then register this type with Tiny Ioc.

container.Register<IGetPersons>(); 

This brings us onto the second thing you are struggling with, which is how Tiny Ioc works.

After you add the implicit registration in the nancy bootstrapper, Tiny Ioc will then scan your assemblies and find the implementation of IGetPersons automatically! (This is assuming there is a single implementation. If more than one you will actually need to do more work but that's another story).

This magic then allows you to just modify your controller thus:

public MyNancyModule(IGetPersons personGetter)
{
    Get["/"] = _ =>
    {
        var x = List<Person> personGetter.GetAll(); // Look, don't need to resolve this manually!
        return Response.AsJson(x);
    };
}

Tiny Ioc will take care of resolving the injected type to the IGetPersons implementation, which you are then free to use through the controller.

Lets say a client makes an http post to a win form app which is running a nancy service. How do get the data from the nancy service to the win form.

Have a look at the following: http://weblogs.asp.net/rweigelt/10235654

The author is using the Managed Extensibility Framework (MEF) to make changes to what I assume is some kind of singleton provided by the framework:

container.Register(MefContainer.GetExportedValue<IRemoteAccess>());

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