Kumar
Kumar

Reputation: 11349

Unity extension

I inherited some code with a custom DI container ( which needs to be moved to unity ) which is attribute based like MEF e.g.

[SERVICE]
PUBLIC <interface> myVar { get; set ; }

wondering if there already is a unity extension which i could use or maybe any help on building one ?

Upvotes: 3

Views: 592

Answers (2)

Steven
Steven

Reputation: 172646

If that SERVICEAttribute marks a property for being injected by the container, than just do a search replace over the entire code base and replace [SERVICE] for [Dependency].

The DependencyAttribute is Unity's way of marking properties for dependency injection.

I do agree with ErnieL however that the use of attributes is a code smell in the land of dependency injection. Use the attribute in order to allow moving code to Unity, but don't use [Dependency] for any new code you write and remove it when you refactor old code.

Upvotes: 1

ErnieL
ErnieL

Reputation: 5801

Its not clear what behavior [SERVICE] is suppose to enable. So I'll guess:

Out of the box Unity supports Injection Attributes for different forms of dependency injection. You can also do Attribute-Driven Policies for method interception.

If you have the flexibility to change how injection is done: The preferred pattern is to use constructor injection instead of attributes or service locator. It allows you to remove all references to Unity from you classes... which means you can change containers again without code changes. See Service Locator is an Anti-Pattern.

Upvotes: 2

Related Questions