X
X
xXNullXx2020-01-05 01:16:05
WPF
xXNullXx, 2020-01-05 01:16:05

What are containers and component registration for?

Good day!
Began to study the full implementation of the WVVM pattern. During the study, I learned that they often use the "PRISM" library (I began to study it too). During the study of "PRISM" I learned that some containers are being implemented. And here are the first few questions: What is a container in this context and what is it for? Dependencies are also registered in this container. Here is a thread of questions: What is dependency registration and what is it for?
Here's the actual example:

public class Bootstrapper : AutofacBootstrapper
    {
        protected override DependencyObject CreateShell() => Container.Resolve<Shell>();

        protected override void InitializeShell()
        {
            base.InitializeShell();

            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override ContainerBuilder CreateContainerBuilder()
        {
            var builder = new ContainerBuilder();

            // регистрация зависимостей в контейнере
            // должны быть здесь...

            return builder;
        }
    }

To summarize:
  • What are containers?
  • What are containers for?
  • What is dependency registration?
  • Why register dependencies?
  • Additional question (if possible, answer): is there any significant difference between IoC such as: Unity, AUTOFAC, MEF?

Where I am in BASIC (but not all) about all this:
Here is a series of articles
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-01-05
@yarosroman

Dependency store container. The container is used to resolve dependencies, they say which specific implementation to substitute. Your dependency can be an interface - a class is an implementation of an interface, a class is a class successor, just a class. So, when registering, you match the type and its specific implementation.
for example

container.Register<IRegisterServer,DbRegisterService>();
container.Register<RegisterVieModel>();
.......
//Конструстор ViewModel
void RegisterViewModel(IRegisterService service)
{
}

So, when you request RegisterViewModel from the ioc container, it looks at the parameters of the constructor and, in accordance with the types registered in it, creates and substitutes the corresponding types into the constructor. Why is this necessary, you can change the implementation at any time or make several implementations and register them depending on the startup options or registry keys. In general, dependency inversion serves to get rid of code from cohesion. In the example above, the VM does not use a specific class, but a certain type described by the interface, if you need to change the implementation, then you will change only the implementation line, and the VM class will remain unchanged. On small projects, the benefit may not be noticeable, but on large ones, over which several people, even very much.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question