Code your own IOC Container

前端 未结 12 871
眼角桃花
眼角桃花 2020-12-08 00:52

Has anyone out there written their own IOC Container in C#? Or do the vast majority of folks use the various frameworks such as Spring. What are the pro\'s and con\'s of eac

相关标签:
12条回答
  • 2020-12-08 01:32

    I created my own IoC container that makes it easier to debug the creation of the object (even when you have no access to the container code). When the object is created, when pressing Step into (F11) you see the code to create the object. Full code can be seen here.

    0 讨论(0)
  • 2020-12-08 01:35

    James Kovacs presents a dnrTV episode on this subject here. Here also wrote an article. However during the article he mentions that you would probably want to use one of the pre-built ones. Since there are many diverse looks for them. Ninject, StructureMap, Autofac use a fluent interface. Spring, Castle Windsor, and Unity are more XML config driven. Castle Windsor can also use boo as an interface. Many have hooks to other frameworks such as Unity to EntLib or Castle Windsor to Monorail and the rest of the Castle Project.

    So unless you really need or want something that is not provided by the IOC frameworks available, then why not use one of them.

    0 讨论(0)
  • 2020-12-08 01:37

    Autofac is excellent.

    I've written one myself using less than 15 lines. Just two extensionmethods to a dictionary.

    0 讨论(0)
  • 2020-12-08 01:39

    It's a good excercise to write your own but in the end you might want to use an existing container. You could start with this one in 15 lines of code.

    0 讨论(0)
  • 2020-12-08 01:39

    Someone has wrote one in C# : http://ninject.org/.

    It's open source so you can get the code and see how this guy did it.

    0 讨论(0)
  • 2020-12-08 01:40

    The thing is that there are so many IoC and DI libraries which makes you confused. When you develop something with one of them and you grow, you'll tightly couple your product with such tools and you'll need experts in those to continue the development. It's all about the politics of the company and the design complexity.

    I myself planned to do it manually therefore no much hidden code. I know there are bunch of great open source IoC tools but who actually goes through the code and tries to understand?

    Not about reinventing the wheel but sometimes it's good if you can your own custom wheel that fits your product better.

    0 讨论(0)
提交回复
热议问题