LightInject and automatic property instantiation The Next CEO of Stack OverflowHidden Features of C#?How do you give a C# Auto-Property a default value?LINQ's Distinct() on a particular propertyHow to get Unity's automatic injection to work on interface injected constructors?Dependency injection and named loggersConstructor Injection Alternatives (Castle Windsor)Castle Windsor won't inject Logger in a property!Passing parameters/properties with MEFWindsor - pulling Transient objects from the containerNLog: Is passing a logger around classes a good practice?

Visit to the USA with ESTA approved before trip to Iran

Only print output after finding pattern

When did Lisp start using symbols for arithmetic?

Rotate a column

What makes a siege story/plot interesting?

MAZDA 3 2006 (UK) - poor acceleration then takes off at 3250 revs

Why is Miller's case titled R (Miller)?

Customer Requests (Sometimes) Drive Me Bonkers!

Is HostGator storing my password in plaintext?

Why do professional authors make "consistency" mistakes? And how to avoid them?

How to write the block matrix in LaTex?

% symbol leads to superlong (forever?) compilations

How can I open an app using Terminal?

Text adventure game code

Grabbing quick drinks

How to be diplomatic in refusing to write code that breaches the privacy of our users

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?

Does the Brexit deal have to be agreed by both Houses?

WOW air has ceased operation, can I get my tickets refunded?

Why Were Madagascar and New Zealand Discovered So Late?

Solution of this Diophantine Equation

Describing a person. What needs to be mentioned?

If I blow insulation everywhere in my attic except the door trap, will heat escape through it?



LightInject and automatic property instantiation



The Next CEO of Stack OverflowHidden Features of C#?How do you give a C# Auto-Property a default value?LINQ's Distinct() on a particular propertyHow to get Unity's automatic injection to work on interface injected constructors?Dependency injection and named loggersConstructor Injection Alternatives (Castle Windsor)Castle Windsor won't inject Logger in a property!Passing parameters/properties with MEFWindsor - pulling Transient objects from the containerNLog: Is passing a logger around classes a good practice?










0















Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:



A C# class library that contains a logger class; the logger is what should be injected.



namespace Common 
public interface ILogger void Log(string msg);

public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);




A C# console app that references the class library. Some things that didn't seem to help are commented out.



namespace TestLightInject 
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());

var worker = new Worker();
worker.DoSomething();



public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");




I guess I could allow public access to the service container, and change the Worker ctor to something like



public Worker() logger = Program.container.GetInstance<ILogger>(); 


but it was simpler when any ILogger property was automatically instantiated.



Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?










share|improve this question






















  • Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

    – madreflection
    Mar 21 at 16:41












  • I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

    – Qodex
    Mar 22 at 14:40











  • So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

    – Qodex
    Mar 22 at 14:46
















0















Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:



A C# class library that contains a logger class; the logger is what should be injected.



namespace Common 
public interface ILogger void Log(string msg);

public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);




A C# console app that references the class library. Some things that didn't seem to help are commented out.



namespace TestLightInject 
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());

var worker = new Worker();
worker.DoSomething();



public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");




I guess I could allow public access to the service container, and change the Worker ctor to something like



public Worker() logger = Program.container.GetInstance<ILogger>(); 


but it was simpler when any ILogger property was automatically instantiated.



Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?










share|improve this question






















  • Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

    – madreflection
    Mar 21 at 16:41












  • I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

    – Qodex
    Mar 22 at 14:40











  • So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

    – Qodex
    Mar 22 at 14:46














0












0








0








Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:



A C# class library that contains a logger class; the logger is what should be injected.



namespace Common 
public interface ILogger void Log(string msg);

public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);




A C# console app that references the class library. Some things that didn't seem to help are commented out.



namespace TestLightInject 
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());

var worker = new Worker();
worker.DoSomething();



public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");




I guess I could allow public access to the service container, and change the Worker ctor to something like



public Worker() logger = Program.container.GetInstance<ILogger>(); 


but it was simpler when any ILogger property was automatically instantiated.



Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?










share|improve this question














Some time ago I worked on a project that I THINK used LightInject. I no longer have access, so I can't just go look for myself. It seemed like once the ServiceContainer was instantiated, something triggered reflection across all assemblies, and any properties of a certain interface type were automatically instantiated. Something like this:



A C# class library that contains a logger class; the logger is what should be injected.



namespace Common 
public interface ILogger void Log(string msg);

public class Logger : ILogger
public Logger()
public void Log(string msg) Console.WriteLine(msg);




A C# console app that references the class library. Some things that didn't seem to help are commented out.



namespace TestLightInject 
class Program
private static ServiceContainer container;
static void Main(string[] args)
container = new ServiceContainer();
//container.EnableAnnotatedPropertyInjection();
container.Register<ILogger, Logger>();
//container.RegisterPropertyDependency<ILogger>((factory, propertyInfo) => new Logger());

var worker = new Worker();
worker.DoSomething();



public class Worker
//[Inject]
ILogger logger get; set; = null; // THIS IS THE PROPERTY THAT NEEDS TO BE SET
public Worker()
public void DoSomething() logger.Log("It works!");




I guess I could allow public access to the service container, and change the Worker ctor to something like



public Worker() logger = Program.container.GetInstance<ILogger>(); 


but it was simpler when any ILogger property was automatically instantiated.



Is there a way to do this with LightInject, or was it some other DI framework that did it? Or am I just imagining it all?







c# .net dependency-injection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 16:30









QodexQodex

1068




1068












  • Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

    – madreflection
    Mar 21 at 16:41












  • I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

    – Qodex
    Mar 22 at 14:40











  • So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

    – Qodex
    Mar 22 at 14:46


















  • Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

    – madreflection
    Mar 21 at 16:41












  • I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

    – Qodex
    Mar 22 at 14:40











  • So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

    – Qodex
    Mar 22 at 14:46

















Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

– madreflection
Mar 21 at 16:41






Be careful with that last line because that's using the service locator pattern, which can appear to be similar to dependency injection but definitely isn't. What you need to do read up on DI best practices and research libraries. SO doesn't do library / external resource recommendations, so do some research, pick a DI library, take a shot at it, and then ask a question if you run into a problem.

– madreflection
Mar 21 at 16:41














I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

– Qodex
Mar 22 at 14:40





I'm always careful. But the whole point of this questions is to NOT do what's in that last line. I'm not doing DI for the sake of doing DI because it's cool, I'm doing it because I need some objects injected in certain places. DI is usually expressed in terms of ctor or method injection, which is often impractical for a project of significant size. Carry a reference down thru 15 call levels just so it can be injected in the object that needs it? Add ctor injectors to 500 or 1000 classes? Let's not be ridiculous. What I need is reflective injection....

– Qodex
Mar 22 at 14:40













So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

– Qodex
Mar 22 at 14:46






So, yes, I did some research, I picked a DI library (LightInject), took a shot at it (multiple shots, actually), ran into a problem, and asked a question. In case the main question wasn't clear, it's this: is there a way to get LightInject to do reflective injection? It appeared that it was doing so in a project I no longer have access to. Extensive googling has not turned up an answer. I'm not looking for a recommendation, just an answer to the question, can library X do Y?

– Qodex
Mar 22 at 14:46













0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285118%2flightinject-and-automatic-property-instantiation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55285118%2flightinject-and-automatic-property-instantiation%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현