标签: unity-container

在运行时从UnityContainer中删除已注册的类型?

我想知道是否有一种简单的方法可以从统一容器中删除已注册的类型,或者至少用另一个替换现有的接口/类型映射.只是将另一个类类型映射到接口并且旧的类型被覆盖了吗?


这不应该经常发生.实际上几乎没有任何时间,但有些情况我想要更换一个服务,实现与另一个接口的一些接口,而不会让其他部分受到干扰.

c# unity-container

4
推荐指数
2
解决办法
6638
查看次数

在Unity配置中,如何将connectionString传递给构造函数?

我在web.config中设置统一配置,我有一个类型,我想传递给它的连接字符串已经存在于同一个web.config文件中.

<connectionStrings>
    <add name="DatabaseConnectionString" connectionString="metadata=res://*/Database.csdl|res://*/Database.ssdl|....." providerName="System.Data.EntityClient" />
  </connectionStrings>
Run Code Online (Sandbox Code Playgroud)

在统一部分有:

<type type="IDatabase" mapTo="Database" >
      <constructor>
          <param name="connectionString" >
             <value value="metadata=res://*/Database.csdl|res://*/Database.ssdl|...."/>
          </param>
      </constructor>
</type>
Run Code Online (Sandbox Code Playgroud)

但是就像我在同一个.config文件中写两次相同的conectionString一样,是否有另一种更好的方法将connectionString的名称传递给类型Database构造函数以避免web.config中的重复?

c# xml ioc-container unity-container

4
推荐指数
1
解决办法
2379
查看次数

Microsoft Unity容器的性能

我正在审查一个使用Microsoft Patterns and Practices Unity Container的项目.

有一个容器有40个注册类型,为每个Web服务调用创建一个容器实例.

我想知道:

  • 由于注册了这么多类型,是否存在性能问题?
  • 可以在Web服务调用之间共享统一容器吗?

Web服务托管在IIS中.

c# performance web-services unity-container

4
推荐指数
2
解决办法
3306
查看次数

Unity拦截基于方法注释的更改调用处理程序

我在类中有一个方法,我想拦截:

[CustomTag1(Order = 0)]
[CustomTag2(Order = 1)]
public virtual DoSomething()
Run Code Online (Sandbox Code Playgroud)

ICallHandler.Order在使用时,如何将订单值注入属性CustomAttributeMatchingRule

我不希望将订单硬编码到处理程序本身或注册时.我希望它是方法注释的Order属性的变量.

c# aop unity-container interceptor

4
推荐指数
1
解决办法
1530
查看次数

如何使用Unity DI加载具有依赖项的程序集,然后注册内部

我有一个asp.net mvc 3 Web应用程序,它具有实现系统相关EFcontexts和服务的插件程序集.

例如,我有一个看起来像下面这样的程序集:

System1Controller : IController // from System.Web.Mvc

ISystem1Service {
    IList<System1Type> GetOperation(string something);
}

System1Service : ISystem1Service 
{
    private ISystem1Entities context;
    public System1Service(ISystem1Entities context)
    {
        this.context = context;
    }
}

ISystem1Entities
{
    IDbSet<System1Type> Operations { get; set; }
}

System1Entities : DbContext, ISystem1Entities
Run Code Online (Sandbox Code Playgroud)

使用Unity Bootstrapper并调用Bootstrapper.Initialize()与以下BuildUnityContainer()实现一起使用

private static IUnityContainer BuildUnityContainer()
{
    var theContainer = new UnityContainer();
    var connectionString = ConfigurationManager.ConnectionStrings["WebAppConnectionString"];
    if (connectionString == null)
    {
        throw new ApplicationException("The ConnectionString is not defined.");
    }

    // register all your components …
Run Code Online (Sandbox Code Playgroud)

.net asp.net-mvc dependency-injection unity-container entity-framework-5

4
推荐指数
1
解决办法
2206
查看次数

Unity的通用依赖注入

我们将现有的日志记录库包装在我们自己的C#应用​​程序中的日志记录服务中,以便使用预定义的方法为特定的日志记录情

public class LoggingBlockLoggingService : ILoggingService
{
    private LogWriter writer;

    public LoggingBlockLoggingService(LogWriter writer)
    {
        this.writer = writer;
    }
    ....//logging convenience methods, LogWarning(), etc...
}
Run Code Online (Sandbox Code Playgroud)

我想修改这个实现,以便它接受实例化它的类的类型(底层记录器,在这种情况下,LogWriter将是一个单例).所以要么使这个实现(和接口ILoggingService)通用:

public class LoggingBlockLoggingService<T> : ILoggingService<T>
{
    ...
    private string typeName = typeof(T).FulName;
    ...
Run Code Online (Sandbox Code Playgroud)

或者添加一个额外的构造函数参数:

public class LoggingBlockLoggingService : ILoggingService
{
    private LogWriter writer;
    private string typeName;

    public LoggingBlockLoggingService(LogWriter writer, Type type)
    {
        this.writer = writer;
        this.typeName = type.FullName;
    }
    ....//Include the typeName in the logs so we know the class that is logging.
}
Run Code Online (Sandbox Code Playgroud)

在注册我们的类型时,有没有办法在Unity中配置 …

c# dependency-injection unity-container

4
推荐指数
1
解决办法
4497
查看次数

Microsoft练习使用Unity Register Type

我有几个场景,我很难映射类型.

场景1:

 public AccountController()
            : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager)
        {
            UserManager = userManager;
        }
Run Code Online (Sandbox Code Playgroud)

类型定义

 public class ApplicationUser : IdentityUser
    {
    }

 // Summary:
    //     Implements IUserStore using EntityFramework where TUser is the entity type
    //     of the user being stored
    //
    // Type parameters:
    //   TUser:
    public class UserStore<TUser> : IUserLoginStore<TUser>, IUserClaimStore<TUser>, IUserRoleStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserStore<TUser>, IDisposable where TUser : global::Microsoft.AspNet.Identity.EntityFramework.IdentityUser
    {
 ....
  }

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
    { …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection unity-container asp.net-mvc-5

4
推荐指数
1
解决办法
5425
查看次数

基于混凝土类型在Unity中注册通用类型

我正在尝试模拟我可以在Ninject中配置的行为,而只使用Unity.

我正在尝试使用Cached Repository Pattern,给定以下类和接口:

public interface IRepository<T>
{
    T Get();
}

public class SqlRepository<T> : IRepository<T>
    where T : new()
{
    public T Get()
    {
        Console.WriteLine("Getting object of type '{0}'!", typeof(T).Name);
        return new T();
    }
}

public class CachedRepository<T> : IRepository<T>
    where T : class
{
    private readonly IRepository<T> repository;

    public CachedRepository(IRepository<T> repository)
    {
        this.repository = repository;
    }

    private T cachedObject;
    public T Get()
    {
        if (cachedObject == null)
        {
            cachedObject = repository.Get();
        }
        else
        {
            Console.WriteLine("Using cached repository to …
Run Code Online (Sandbox Code Playgroud)

c# generics ninject inversion-of-control unity-container

4
推荐指数
1
解决办法
1608
查看次数

在ASP.NET中使用Unity.WebForms

我正在尝试在webforms项目中实现DI,所以我在UI层中安装了Unity.WebForms dll.我一使用UnityWebFormsStart类文件为我创建了一个App_Start文件夹.在这个文件里面有一个方法RegisterDependencies,它要求编辑.

注册依赖项后的下一步是什么?我需要在Global.asax类文件中添加一些内容吗?我如何以及在何处解决网络表单中的类型?我用任何属性装饰它吗?

dependency-injection webforms unity-container

4
推荐指数
1
解决办法
5688
查看次数

Unity.Mvc3与Unity.Mvc

使用Unity.Mvc3和Mvc 3应用程序,我可以注册我IDummyService的如下:

container.RegisterType<IDummyService, DummyService>(new HierarchicalLifetimeManager());
Run Code Online (Sandbox Code Playgroud)

在每个Web请求上,IDummyService都会创建一个新的my实例(如本文所述),但是由于我将Mvc 3升级到Mvc 4并将Unity.Mvc3升级到Unity.Mvc,因此创建了一个实例并在所有Web请求中使用,直到重新启动应用程序.基本上,使用时IDummyService是Mvc 4应用程序中的单例HierarchicalLifetimeManager.对我来说,很难相信这是Unity.Mvc中的新行为.

对此有更好的解释吗?

asp.net-mvc unity-container asp.net-mvc-3

4
推荐指数
1
解决办法
348
查看次数