Search code examples
c#asp.net-identityasp.net-core-2.0asp.net-core-identity

What is IOptions in AspNetCore Identity 2.0?


I am trying to understand how Identity comes together. I would like to implement Dapper instead of Entity Framework, but I feel in order for me to be able to support in future I need to understand what is going on.

As such I downloaded the 2.0.0 release from Identity Repository here: https://github.com/aspnet/Identity/releases and started looking around.

The part that I am stuck on right now is IOptions<IdentityOptions> that is required to be passed in by UserManager. Looking at the IdentityOptions class itself, I understand that those are configurations for things such as PasswordOptions and etc. So I need to instantiate and set those up and then bind them.

However, the part that I don't understand is IOptions interface. It seems to be a wrapper, but why is it there, what is the purpose of it? Why not just create IdentityOptions on its own, why does it needs to be wrapped inside the IOptions interface?


Solution

  • As I understand it, IOptions is the interface to retrieve any configured object and its underlying properties. For example, you have multiple Options objects under IdentityOptions class. Instead of instantiating them all, one by one, you would plug your IdentityOptions into DI pipeline by adding your options class into service collection object. This way you can call access any of the Options object (from IdentityOptions class) anywhere through constructor injection.

    The point to remember is that all "Options" objects under IdentityOptions are itself classes. So to avoid all manual instantiating, you can use DI pipeline by wrapping IOptions around your Options class.

    hope this helps.