I am working on the correct usage of IOCContainer(Autofac) in a project which has mulitple assemblies. I am not able to understand the nested dependency pattern correctly as well as what would be the best exception handling strategy in case of failed resolves.
The post is not concise, as I am afraid I cannot explain my confusion in a brief manner. So apologies..
I am trying to use the 'constructor injection pattern' instead of 'property' or 'method' injection. so a class which looked like this:
privatestaticreadonly IDependency = IOCContainer.Resolve<IDependency>();
is changed to something like this
Class Foo(IDependency dependency)
privatereadonly IDependency = dependency;
however after cleaning up the whole code with the constructor pattern, the code looks like this
public Foo(IDependency dependency)
this.dependency = dependency;
var bar = new Bar(this.dependency);
public Bar(IDependency dependency)
this.dependency = dependency;
Now if the dependency was not available (IOCContainer failed to resolve this dependency), Foo will pass that dependency around, without worrying about the failed resolved call..
I think that given this scenario, the property or method injection methods seem correct as all the classes are directly calling IOCContainer resolves and then they would have a way to check if the dependency was resolved or not directly from the IOCContainer.
However A null check or a try catch can be applied when we access the dependency anyways even with the constructor injection pattern, for e.g.
(dependency != null ? dependency.doSomething : thrownew Exception(DependencyNotAvailable);
Is it correct approach? How should I handle the exceptions from IOCContainer otherwise?
I see some things in your code which I would change, and this should hopefully clear things up for you. Basically, you're mixing up IoC with newing stuff. If I were you, I would change the implementations to something like this:
As you can see, we're relying on injecting any dependencies here (i.e. injecting IBar rather than newing up Bar), and we test in the constructor for nulls. That way, we determine early on whether or not an actual implementation has been passed in.
PrivateSub cmbcustomer_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cmbcustomer.SelectedIndexChanged
txtticketnumber.Text = "9" + Format(cmbcustomer.SelectedValue, "0000")
Catch ex As Exception
Hi to all.This Is Lakshmi Dhivya,Iam doing my Engineering on Information Technology(IT),and I have decided to make an editor with all possible formatting options.
I have selected vb.net as a programming language for my project.
I have started designing the Rich Text editor,but Iam unable to change the font size alone with different font text selected.I have added a font comobox and a size combobox,but i need to change font alone and size alone with different font text selected.
You have not really explained what the problem is. If you select a new font from the font combo then the current font size should be used. And if you select a new height then the current font should be used. Alternatively you could use the FontDialog class[^].
You have to create a new Font object, based on the previous font object, with the new FontSize. That's the bad thing with Fonts: they are immutable, and changing a property of a font can only be done be creating a new one with the different property.
I have found that in wordpad if you select a text content typed in different fonts the font combobox will not show any font and if you change the size, only the size of the selected text changes and not the fonts.
How To do this.
This Is my Problem,I think i have told the exact problem.
Awaiting positive response form you all.
Last Visit: 31-Dec-99 18:00 Last Update: 28-Jul-14 22:52