Dependency Inject IRepository<> using Unity

54 views
Skip to first unread message

jrea...@gmail.com

unread,
Jul 2, 2015, 10:57:02 PM7/2/15
to sharpre...@googlegroups.com
Sharp Repository has been great. However, I can't seem to avoid creating custom repositories for dependency injection. How can I do this assuming I am using the SharpRepository.Ioc.Unity Nuget installation? Thanks.

public static class UnityConfig
{
public static void RegisterComponents()
{
var container = BuildUnityContainer();

GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}

private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();

// register all your components with the container here
// it is NOT necessary to register your controllers

DbContext context = new USFP_Data_WarehouseContext() as DbContext;

// Contexts
container.RegisterType<IDatabaseContext, DatabaseContext>(new HierarchicalLifetimeManager());

// Repositories
container.RegisterType<IProgramsRepository, ProgramsRepository>();

return container;
}
}

Jeff Treuting

unread,
Jul 3, 2015, 5:24:21 PM7/3/15
to jrea...@gmail.com, sharpre...@googlegroups.com
I wasn't able to get that working with Unity unfortunately. I don't use Unity myself so I'm sure that someone with more experience could probably figure it out.

You can see the source code here: https://github.com/SharpRepository/SharpRepository/blob/develop/SharpRepository.Ioc.Unity/UnityExtensions.cs

The idea is that you could call container.RegisterSharpRepository() and it would take care of hooking that up for you. There are some notes in the source code speaking to where I got stuck.. I haven't looked at it in a very long time but you could compare some of the other implementations of the Ioc Register extension methods if you wanted to give it a shot in Unity. If you get it working I'd love to get a PR and incorporate it here.

Sorry I couldn't be of more help.

Jeff
________________________________________
From: sharpre...@googlegroups.com <sharpre...@googlegroups.com> on behalf of jrea...@gmail.com <jrea...@gmail.com>
Sent: Thursday, July 2, 2015 7:57 PM
To: sharpre...@googlegroups.com
Subject: [SharpRepository] Dependency Inject IRepository<> using Unity
--
You received this message because you are subscribed to the Google Groups "SharpRepository" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sharpreposito...@googlegroups.com.
To post to this group, send email to sharpre...@googlegroups.com.
Visit this group at http://groups.google.com/group/sharprepository.
To view this discussion on the web visit https://groups.google.com/d/msgid/sharprepository/663ce163-1f9d-4bd8-bf36-2f56eb4c3647%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages