unity-container

Dependency injection with EF DbContext that implements 2 interfaces

白昼怎懂夜的黑 提交于 2019-12-22 18:24:03
问题 Given a DbContext that implements 2 interfaces like so: public interface IQueryEntities { IQueryable<User> Users { get; } IQueryable<Computer> Computers { get; } // other IQueryable<T> get properties } public interface IUnitOfWork { int SaveChanges(); } public class MyContext : DbContext, IQueryEntities, IUnitOfWork { // implement interfaces using EF } First question, is it a bad idea to separate out the query aspects of DbContext (IDbSets) from the command aspects (SaveChanges)? I am

When migrating to AutoMapper 4.2/5.0, should I store an IMapper instance or the MapperConfiguration instance in my dependency injection container?

情到浓时终转凉″ 提交于 2019-12-22 12:40:47
问题 I am migrating to the newer configuration of AutoMapper. I was looking at examples on the AutoMapper GitHub Wiki and am a little confused on how to complete the configuration. The Wiki says in one place you can store the MapperConfiguration instance in your D.I. container (or store it statically), but the next paragraph says you can store the Mapper instance statically. In a nutshell, I am not sure if I should be doing var config = new MapperConfiguration(cfg => { cfg.CreateMap<Foo, Bar>()

PerThreadLifetimeManager in Unity

懵懂的女人 提交于 2019-12-22 12:37:40
问题 In the Unity PerThreadLifetimeManager documentation, I read that: " This lifetime manager does not dispose the instances it holds ". Ref.: http://msdn.microsoft.com/en-us/library/ff647854.aspx So, if I am using a ThreadPool, does it mean that objects resolved using Unity on a Thread of the ThreadPool will not get disposed at the end of the work done in that thread before being returned to the pool? Any pattern or ideas how I can ensure that the objects do get disposed & I get a clean thread

Intercept Unity 2.0 HandlerAttribute without an interface

流过昼夜 提交于 2019-12-22 12:17:16
问题 I'm a first-time user of the AOP features of Unity 2.0 and would like some advice. My goal is to be able to log method calls in an ASPX page, like so: public partial class Page2 : Page { protected void Page_Load(object sender, EventArgs e) { } [Log] private void Testing() { } } Here is the code for the LogAttribute : public class LogAttribute : HandlerAttribute { public override ICallHandler CreateHandler(IUnityContainer container) { return new LogHandler(Order); } } Now the LogHandler :

Unity 2.0 IOC Configuration about Generic class

不想你离开。 提交于 2019-12-22 11:19:13
问题 I want some Repository class extend one common generic class to perform some common operation, problem is: how to config a UserExRepository type in config file. public class UserExRepository : Repository<User>, IUserEx { public UserExRepository(Context context):base(context){ } } public abstract class Repository<TObject> : IRepository<TObject> where TObject : class { protected Context Context = null; public Repository(Context context) { Context = context; } // do some common operation about

How to register multiple types that implement the same interface

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 10:49:28
问题 I have a single interface and this is being used by 2 classes. I am using unity configuration to identify the instance based on the interface. Now I want to know how should i register these types so that i can call the appropriate implementation based on the single interface itself. 回答1: This is how I do it: var container = new UnityContainer().RegisterType<IAmImplementedMoreThanOnce, Implementation1>("Implementation1") .RegisterType<IAmImplementedMoreThanOnce, Implementation2>(

WebApi Filter Unity

Deadly 提交于 2019-12-22 10:45:24
问题 Environement : Unity and ASP.NET MVC WEBAPI I follow the sample from the webpage http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection And after to do some modifications, I find a way to inject Filter for Controler. I used this code in the boostrapper.cs var container = new UnityContainer(); container.RegisterInstance<IFilterProvider>("FilterProvider", new FilterProvider(container)); container.RegisterInstance<IActionFilter>("LogActionFilter", new TraceActionFilter(

How to prevent Unity overwriting existing mappings with AutoRegistration

非 Y 不嫁゛ 提交于 2019-12-22 10:39:30
问题 Unity 3 offers new capabilities for AutoRegistration (Registration by Convention) such as: container.RegisterTypes( AllClasses.FromLoadedAssemblies(), //uses reflection WithMappings.FromMatchingInterface, //Matches Interfaces to implementations by name WithName.Default); This code will register all types that implement their similarly named interfaces, against those interfaces. For example, class MyService : IMyService will be registered automatically as though you had written the following:

DbContext is Disposed When Using Unity Dependency Injection on WebApi project

佐手、 提交于 2019-12-22 09:25:33
问题 I'm fairly new at using dependency injection and I think I must be overlooking something really simple. I have a Web API project where I'm registering generic repositories. The repositories take a dbContext as a parameter in their constructor. The behavior I find strange is that I can make one successfull call to the service but any subsequent calls tell me that the dbcontext has been disposed. I do have a using statement in there but that shouldn't be a problem since DI is supposed to be

How to use DI when spawning new Windows Forms downstream?

帅比萌擦擦* 提交于 2019-12-22 08:57:17
问题 I have the Unity DI container working initially with my Windows Forms application. In Program.cs I have the following: static void Main() { var container = BuildUnityContainer(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(container.Resolve<MainForm>()); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<ITest, MyTestClass>(); container.RegisterType<ISomeOtherTest,