Tuesday 15 February 2011

c# - Using Unity DI with a Console Application -


I am trying to work on unity with my console application, however .. all the properties which I have injected dependency Try to set zero to zero.

This is my code:

Program.cs

  Namespace. Presentation. Console {class program {static void Main (string [] Args) {var mainThread = New methathide (); }}}  

MainThread.cs

  name space xxxx.Presentation.Console {public class manfread {public IUnitOfWork UnitOfWork {get; Set; } Public image mapper {receive; Set; } Public Maththread () {Mapper. Registration Manager (); }}}  

App.config

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Configuration & gt; & Lt; ConfigSections & gt; & Lt; Section name = "Integration" type = "Microsoft.Practices.Unity.configuration.UnityConfigurationSection, Microsoft.Practices.Unity.configuration" /> & Lt; / ConfigSections & gt; & Lt; Startup & gt; & Lt; Supported serial version = "v4.0" sku = ".NETFramework, version = v4.5.1" /> & Lt; / Startup & gt; & Lt; Unity xmlns = "http://schemas.microsoft.com/practices/2010/unity" & gt; & Lt; Alias ​​alias = "idataAccess" type = "UniFr framework .adata, unityframework" /> & Lt; Name space name = "unity framework" /> & Lt; Assembly name = "unity framework" /> & Lt; Container & gt; & Lt; Type & gt; & Lt; Type type = "IMapper" mapTo = "xxxx.Core.Parse.ParseMapper" /> & Lt; / Type & gt; & Lt; / Container & gt; & Lt; / Unity & gt; & Lt; / Configuration & gt;  

App.cong is always set to copy

Where Mapper comes back as a null in this case (and I think there is a unitoffwork too )

Do I need to do something else? Add something to app.config? Am I missing something?

Thanks in advance!

Br, Inx

unity only Supply of dependence for components received through resolution or while resolving sub dependence. "Root" components or components must be obtained manually from the container. Using new program will not provide auto dependency because it bypass integration

  Fixed class program entry {static zero main (string [] arg) {var unity = CreateUnityContainerAndRegisterComponents (); // Program = Unity to explicitly resolve the "root" components or components. Resolve & lt; Program & gt; (); Program.Run (); }} Public Class Program {read only Ix _x; // These dependencies will be resolved automatically // (And in this case the constructor is supplied) Use the public program (IMper Mapper, IX X) {// Dependency Mapper. Registrar Mapping (); // Appoint any relevant properties, etc. _ X = x; } // Actual Job Run Public Zero here () {_x.DoStuff (); }}  

I prefer code based registration for most functions.

  • I suggest that using no attributes, though they will 'work' if above it < Code> resolve pattern. "Route" objects must be resolved manually.

    The problem with the attributes is that the dependencies on on unity - so much "inverting"!

    Manufacturer injection (shown) is automatic / default if property injection is preferred.

  • I could probably create a factory (or Func & lt; UoW & gt; ) and call-stack context (i.e. methods of implementing it ) When applicable, there may be many different UOWs in the application during a single run. In this case, you may be interested in creating scope.

  • I probably use a factory to create an earlier IMapper object when it is then resolved instead of using the registermapping.


No comments:

Post a Comment