Migrating from Unity to StructureMap

315 views
Skip to first unread message

Richard Onorato

unread,
Apr 13, 2014, 5:47:07 PM4/13/14
to structure...@googlegroups.com
I am in the process of migrating from Unity to StructureMap and need some help translating Unity settings to their StructureMap equivalent:

In my Unity Config class I have the following:

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new PerRequestLifetimeManager(), new InjectionConstructor(new ApplicationDbContext()));

What would be the equivalent StructureMap Registry entry?

I also have

container.RegisterType<IDataContextAsync, DbContext>(new PerRequestLifetimeManager());

What would be the equivalent StructureMap Registry entry?

Thank you in advance for your help.

Regards,

Richard O

Luka Cetina

unread,
Apr 14, 2014, 3:07:57 AM4/14/14
to structure...@googlegroups.com
Hi,
It's quite difficult for us to see without the class/constructor declarations.
Can you post the Interfaces/Classes declarations with the class constructors so we can better understand your problem here.
From what you have wrote here I could only assume this:
UserStore has a constructor dependency on ApplicationDbContext.
UserStore must be unique per request as is the ApplicationDbContext
IDataContextAsync is too PerRequest lifecycle.

So based on those assumptions you could write this:
ObjectFactory.Configure(config =>
                                    {
                                        config.For<IDataContextAsync>().Use<DbContext>().LifecycleIs<UniquePerRequestLifecycle>();
                                        config.For<ApplicationDBContext>().Use<ApplicationDBContext>().LifecycleIs<UniquePerRequestLifecycle>();
                                        config.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>().LifecycleIs<UniquePerRequestLifecycle>();
                                    });

This is the best I can do without seeing your interface/class declarations and constructors.

Luka Cetina



--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to structuremap-us...@googlegroups.com.
To post to this group, send email to structure...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/structuremap-users/c6402c79-5462-4d7f-aff5-8cdf6500d562%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Onorato

unread,
Apr 14, 2014, 9:07:23 AM4/14/14
to structure...@googlegroups.com
Luka,

Thank you for the reply.  That is what I was looking for.  I was able to get the constructor part worked out last night using the following line of code in my Registry file:

For(typeof(IUserStore<ApplicationUser>)).Use(typeof(UserStore<ApplicationUser>)) .Ctor<DbContext>("DbContext") .Is(new ApplicationDbContext());

UserStore is an Object defined in Microsoft.AspNet.Identity.EntityFramework namespace.  It contains two constructors (a default one and one that takes a DbContext).  My ApplicationDbContext inherits from IdentityDbContext which also is part of the same namespace as UserStore.  In the end IdentityDbContext inherits from DbContext which is what the UserStore is expecting.

I will now add in the parts that you provided me and that should hopefully complete my migration.  I am sure I will probably have other questions as I start to make more use of StructureMap in other parts of my project.


Thank you again for your assistance.

Richard
Reply all
Reply to author
Forward
0 new messages