Unity framework DependencyAttribute only works for public properties?

Posted by rally25rs on Stack Overflow See other posts from Stack Overflow or by rally25rs
Published on 2009-01-23T16:36:12Z Indexed on 2010/06/09 19:22 UTC
Read the original article Hit count: 572

Filed under:
|

I was trying to clean up some accessability stuff in my code, and inadvertently broke Unity dependency injection. After a while I realized that I marked some public properties that I didn't really want exposed outside my DLLs to internal. Then I started getting exceptions.

So it seems that using the [Dependency] attribute in Unity only works for public properties. I suppose that makes sense since the internal and private props wouldnt be visible to the Unity assembly, but feels really dirty to have a bunch of public properties that you never want anyone to set or be able to set, other than Unity.

Is there a way to let unity set internal or private properties too?

Here is the unit test I'd like to see pass. Currently only the public prop test passes:

    [TestFixture]
public class UnityFixture
{
	[Test]
	public void UnityCanSetPublicDependency()
	{
		UnityContainer container = new UnityContainer();
		container.RegisterType<HasPublicDep, HasPublicDep>();
		container.RegisterType<TheDep, TheDep>();

		var i = container.Resolve<HasPublicDep>();
		Assert.IsNotNull(i);
		Assert.IsNotNull(i.dep);
	}

	[Test]
	public void UnityCanSetInternalDependency()
	{
		UnityContainer container = new UnityContainer();
		container.RegisterType<HasInternalDep, HasInternalDep>();
		container.RegisterType<TheDep, TheDep>();

		var i = container.Resolve<HasInternalDep>();
		Assert.IsNotNull(i);
		Assert.IsNotNull(i.dep);
	}

	[Test]
	public void UnityCanSetPrivateDependency()
	{
		UnityContainer container = new UnityContainer();
		container.RegisterType<HasPrivateDep, HasPrivateDep>();
		container.RegisterType<TheDep, TheDep>();

		var i = container.Resolve<HasPrivateDep>();
		Assert.IsNotNull(i);
		Assert.IsNotNull(i.depExposed);
	}
}

public class HasPublicDep
{
	[Dependency]
	public TheDep dep { get; set; }
}

public class HasInternalDep
{
	[Dependency]
	internal TheDep dep { get; set; }
}

public class HasPrivateDep
{
	[Dependency]
	private TheDep dep { get; set; }

	public TheDep depExposed
	{
		get { return this.dep; }
	}
}

public class TheDep
{
}


Updated:

I noticed the call stack to set the property passed from:

UnityCanSetPublicDependency()
--> Microsoft.Practices.Unity.dll
--> Microsoft.Practices.ObjectBuilder2.dll
--> HasPublicDep.TheDep.set()

So in an attempt to at least make the internal version work, I added these to my assembly's properties:

[assembly: InternalsVisibleTo("Microsoft.Practices.Unity")]
[assembly: InternalsVisibleTo("Microsoft.Practices.Unity.Configuration")]
[assembly: InternalsVisibleTo("Microsoft.Practices.ObjectBuilder2")]

However, no change. Unity/ObjectBuilder still won't set the internal property

© Stack Overflow or respective owner

Related posts about .NET

Related posts about unity