Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4329224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:47:41+00:00 2026-05-21T09:47:41+00:00

I would like to just markup a property with an attribute [DoInjection] and have

  • 0

I would like to just markup a property with an attribute [DoInjection] and have unity do the injection. I don’t want to have to use prop = Unity.Resolve(type). Thats a pain and messy. Does unity provide attributes to do this or do I have to build my own?

Edit: register in App.Main

ISessionFactory sf = new SessionFactory();
            container.RegisterType<IRepository, CustomerRepository>(new InjectionConstructor(sf.CurrentUoW));
            container.RegisterInstance<IUnitOfWork>(sf.CurrentUoW);

Using [Dependancy] on IUnitOfWork propery in ClassX other class but it’s always null. Do I need to build ClassX instance using Unity to get this to work? It looks like I do have to. I don’t like that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:47:42+00:00Added an answer on May 21, 2026 at 9:47 am

    Unity has a DependencyAttribute you can use for this:

    public class MyObject
    {
      private SomeOtherObject _dependentObject;
    
      [Dependency]
      public SomeOtherObject DependentObject 
      {
        get { return _dependentObject; }
        set { _dependentObject = value; }
      }
    }
    

    http://msdn.microsoft.com/en-us/library/ff650198.aspx

    Based on your question, it sounds like you might be trying to use Unity in the wrong spot and your design sense was telling you it didn’t feel right. You should only see Unity where you bootstrap your application. That’s your Main method in a console app or Global.asax in a web or wcf app. The idea is to keep relying on dependencies all the way up the chain until you get to where you bootstrap and resolve just that one top level object using your IoC container. In a console app, I do this:

    class Program
    {
        static void Main(string[] args)
        {
            using (var container = new UnityContainer())
            {
                container
                    .AddExtension(new ConfigureForConsole(args))
                    .Resolve<MyApplication>()
                    .Execute();
            }
        }
    }
    

    http://www.agileatwork.com/console-application-with-ioc/

    In this case, MyApplication is my top level object (it doesn’t need to be an interface here). The ConfigureForConsole is just a one-off custom container extension that has all the RegisterType lines in there. Alternatively you could initialize the container from App.Config here. The idea though is that your Main method has almost nothing in it. Another benefit of this approach is that it makes your code more portable. I find that console apps usually turn into windows services and keeping things clean here makes that transition pretty painless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to upload files just like google mail does. I would want
I would just like your feedback on something. Basically I have a value called
I have an std::vector<unsigned char> with binary data in. I just would like to
I would like copy just file.xml without folder structure using overlays like this: <overlays>
I just would like to know if it is possible (and if yes how)
I am new to coding and would just like to know a bit more
I am pretty sure I cannot, but I would just like to double check
I would like to add a typing speed indicator just below the textarea we
I would like to learn ASP.NET and just wanted some input as to which
I would like to call the event handler just one time, and then detach

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.