-1

I am really new to delegates and I get confused. I have this line of code in my project:

    static Startup()
    {
        UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());
    }

    public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; }

can someone explain to me what that might look like without using delegates and perhaps direct me to a delegates for dummies guide?

r3plica
  • 13,017
  • 23
  • 128
  • 290
  • the getter part would just be a function that `return new UserManager(new ...)` - the setter cannot be done without saving the function somewhere. – Random Dev Sep 09 '14 at 17:12

4 Answers4

1

I'm gonna provide you with a non text book answer , it's the way i would explain it to my self :)

A little background :

In unmanaged (memory) languages like C , there are function pointers that are used in order to reach a function by holding the value of it's address.

In managed languages like C# The way to reach a function is by holding a reference to it. where a reference is like a pointer in which it points to a place in memory but unlike a pointer it can be moved around , by the GC and such .

So how do we reference a function ? we can reference Reference Types. a class is a Reference type and a Delegate is a special kind of CLASS which needs to have a signature for the method it "represents" (or "references").

Action and Func are exactly that , a Delegate with a pre-defined signature

it just saves you the bother of writing this : (From msdn)

public delegate void ProcessBookDelegate(Book book); 

ProcessBookDelegate handler = ProcessBookMethod; // in the scope of some class's method

instead of that you can write this :

Action<Book> handler = ProcessBookMethod;

see about Action and Func

Action and Func

Without a Delegate you can just write :

 public static UserManager<IdentityUser> GetUserManager(UserStore<IdentityUser> userStore)
 {
     return new UserManager<IdentityUser>(userStore)
 }
eran otzap
  • 12,293
  • 20
  • 84
  • 139
0

This question is awfully broad, but I'll do my best to answer anyway. I'm not actually sure why your code needs a delegate at all (going from what tiny bit you gave us). Actually, as it would appear to not do anything special.

If you're looking for how to how to use delegates, you might want to check out this MSDN link, as well as this and this already on SO.

Best of luck.

Community
  • 1
  • 1
omni
  • 580
  • 1
  • 6
  • 16
0

A delegate is a function pointer. Instead of using a delegate, you can just write a function. It could look something like this

static void Startup()
{
    UserManagerFactory = getter;
}

static UserManager<IdentityUser> getter()
{
    return new UserManager<IdentityUser>(new UserStore<IdentityUser>());
}
djv
  • 15,168
  • 7
  • 48
  • 72
-1

This is a delegate/lambda-based factory. This approach can be pretty handy sometimes. Check this for more on the subject. Without using lamda/delegate, you would go with an abstract factory in this case.

Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37