Ninject using "In SCOPE" - ioc-container

I want to implement IoC in my application, I've few queries regarding that
While binding Interfaces to Classes, i want to specify the scope of the object
While resolving the class object, i want it to resolve all the dependencies automatically
While passing the vaue type arguments to my binding, how could i use factory methods to pass the value as i don;t want to use constructor arguments for the same
I am using IoC in my WCF application, if i am doing something wrong please suggest some better approch to get best results
Thanks

First of all, be sure to look at Ninject.Extensions.Wcf including the examples and the fact that you put a custom factory in the .svc file.
Then just issue Bind<>().To<>().InXyzScope().WithConstructrorArgument(...)calls in your Module Load.
You havent asked a structured question though so I doubt anyone else is going to be able to make a better stab at an answer than this, which probably isnt going to make you happy...

Related

Access actor methods in modules?

Probably very simple, but can't figure it out.
How can you use methods from tests/_support/AcceptanceTester.php inside your helper modules?
For example when I'm in tests/_support/Helper/Acceptance.php I cannot reference any of the methods in tests/_support/AcceptanceTester.php
I'm assuming because one is an actor and the other is a module?
I read https://codeception.com/docs/06-ModulesAndHelpers but I still don't understand how this works.
Update
Saw a discussion on github that deals with this situation and it was suggested to use StepObjects
I managed to put both helper methods and actor methods into a StepObject class method, but that still doesn't work. It doesn't allow me to execute both from one place.
After a few hours of trying to find an 'elegant' solution to this problem such as using StepObjects injecting dependencies etc. it's much easier to just pass the $I object to whichever helper class method you're using..
It's more verbose, but it works.
If anyone has a better solution, I'll gladly accept it instead.

Debugging CommonServiceLocator.ActivationExeption in MVVMLight

I'm trying to use Ioc in MVVMLight.
SimpleIoc works fine with my ViewModel, but not the NavigationService.
I've drawn a blank googling the error, but suspect the problem might be in my conversion of various C# snippets to VB.net ?
Not exactly solving your problem, but: You are using service locator technique, which is considered anti-pattern and it is against MVVM principles. Correct way is to get INavigationService instance in view model's constructor parameter. Maybe you will get somewhere by doing it like I am saying.

Implementing .Net DI Compile Time Proxies?

I'm not so much seeking a specific implementation but trying to figure out the proper terms for what I'm trying to do so I can properly research the topic.
I have a bunch of interfaces and those interfaces are implemented by controllers, repositories, services and whatnot. Somewhere in the start up process of the application we're using the Castle.MicroKernel.Registration.Component class to register the classes to use for a particular interface. For instance:
Component.For<IPaginationService>().ImplementedBy<PaginationService>().LifeStyle.Transient
Recently I became interested in creating an audit trail of every class and method call. There's a few hundred of these classes so writing a proxy class for each one by hand isn't very practical. I could use a template to generate the code but I'd rather not blow up our code base with all that.
So I'm curious if there's some kind of on the fly solution. I know nHibernate creates proxy classes at some point which overlay all the entity classes. Can someone give me some guidance on how I might be able to do something similar here?
Something like:
Component.For<IPaginationService>().ImplementedBy<ProxyFor<PaginationService>>().LifeStyle.Transient
Obviously that won't work because I can only use generics to generalize the types of methods but not the methods themselves. Is there some tricky reflection approach I can use to do this?
You are looking for what Castle Windsor calls interceptors. It's an aspect-oriented way to tackle cross-cutting concerns -- auditing is certainly one of them. See documentation, or an article about the approach:
Aspect oriented programming is an approach that effectively “injects” pieces of code before or after an existing operation. This works by defining an Inteceptor wrapping the logic being invoked then registering it to run whenever a particular set/sub-set of methods are called.
If you want to apply it to many registered services, read more about interceptor selection mechanisms: IModelInterceptorsSelector helps there.
Using PostSharp, things like this can be even done at compile time. This can speed the resulting application, but when used correctly, interceptors are not slow.

Passing objects vs. Singleton

As far as I can see there are two main principles how to deal with application-wide role player objects like a root model object (in MVC context):
create the object and pass it through the object tree (e.g. in the constructor)
provide it as a singleton or other global variable technique
The first approach seems to be cleaner because the dependencies are better visible but there is a lot of additional work to do (parameters, class variables,...).
What do you prefer?
Edit: The first technique also uses only one instance but it is provided by passing the object and not by a static function
I prefer run singletons' method getInstance() as constructor parameter - bake two birds with one stone ;)
This is where dependency injection can help out. Having to explicitly pass all the correct dependencies manually to an object whenever you create one can be a pain and perhaps somewhat error prone. A decent dependency injection container can help to automate this process and is actually easier to use than singletons.
The Symfony2 framework is a modern example:
http://symfony.com/doc/current/book/service_container.html
I think passing as parameter is a little more memory-efficient, easier to debug, but need a some additional work.
I prefer to use singletons only when i really need it (like database sessions, write to file etc.).
It really depends on project type, language, budget, size of project etc. There is no "universal" answer.

Creating WCF DataContracts dynamically from code

Given the fact that I have a fully dynamic object model, that is, I have no concrete classes defined anywhere in code, but I still want to be able to create WCF DataContracts for them so I can use them in operations. How can I achieve this?
My concrete class "Entity" implements ICustomTypeDescriptor which is used to present the various properties to the outside world, but my expeimentation with WCF suggests that WCF does not care about ICustomTypeDescriptor. Is this correct or have I missed something?
Is this possible? It cannot be so that the only way to create a DataContract is to actually have a concrete harcoded class, can it?
you may use untyped service and message contract IIRC http://geekswithblogs.net/claeyskurt/archive/2008/09/24/125430.aspx
You might try System.Reflection.Emit.
Its quite tricky, but essentially you will just build a custom run-time type, with decorated data contract attributes. It gets tricky when creating encapsulated properties with PropertyChanged notifications, but in your service layer you can just get away with auto properties which are a lot easier.
This dated, but still very relevant link should get you going in the right direction.
http://drdobbs.com/184416570
Things evolve :-) Thanks to the excellent blog series by Alex D James its very easy to implement this.