Register a decorator using non-named container registrations? - decorator

Is there a way in Autofac to register a decorator for an interface without forcing the original registration to be named? I want to generically register a decorator for ALL registered implementations, unfortunately, I cannot define a name for all of them (some are registered in a library I cannot adjust).

Related

JavaFX using initialized controllers and FXML linked controllers in same application

Regarding this question I would like to know if it is ok to use both methods for different controllers in the same application.
This is, if a controller is not to be accessed externally I would use the fx:controller and if it needs to be accessed externally I would use the setController() method.
No, you can access all the controller methods using either method, provided the methods are public (or any valid appropriate accessor).
Using FXMLLoader::setController() method is useful when your controller has non-default constructor. This is covered in the StackOverflow post you mentioned.
If you have set a controller class via fx:controller, you can retrieve the controller instance via FXMLLoader::getController(), and you can call any method that that controller has using that instance.

Where to place business logic is Symfony 2 model layer?

I'm moving my old codebase to the Symfony 2.2 framework.
In my old code, my Article entity has had a method getUrl(), which was returning a URL for current article.
In Symfony i have to use Router service to generate such an URLs.
I can't access Router from inside the Entity, cause it's a bad practice and not really supported by the framework.
I can call the router from the Twig template itself using Twig helper path() and provide all the arguments (from the Article instance) needed to construct the URL. But this approach is not very good, cause if i'll decide to change URL formatting rules - i will have to find all this calls and rewrite them (not very DRY).
I really want to save the business-logic encapsulation here and not to pull all the guts to the view layer.
How should i proceed in this situation?
Create an ArticleManager class in your service layer, and handle any business logic there. You can pass the router to it through dependency injection.
For your example, ArticleManager would have a getUrl(Article $article) method which would use the router instance (that you either injected through __construct or a separate setter method) to generate the Url based on properties of $article, and return it.
This method will ensure that your business logic doesn't pollute the view or controller layers.
Be sure to read up on the Service Container docs.

Practices to register types with IoC container?

I have a solution with several projects (MyApp.Data, MyApp.BLL, MyApp.Web). I register types in Global.asax file in MyApp.Web (main web application):
builder.RegisterType<SomeService1>().As<ISomeService1>().InstancePerHttpRequest();
builder.RegisterType<SomeService2>().As<ISomeService2>().InstancePerHttpRequest();
//...etc
And I wonder whether it's a bad practice to register types and their scope using attributes in the other assemblies (for example, in MyApp.BLL). See below:
[Dependency(typeof(ISomeService1), ComponentLifeStyle.Transient)]
public class SomeService1 : ISomeService1
{
//methods and properties go here
}
Using local attributes or other ways to indicate wiring for a DI Container tightly couples the service to the DI Container, so I don't think that's a good idea.
Additionally, it may constrain your future options. If, for example, you specify the lifestyle scope, you can't reuse the service with a different scope.
In general, you should compose the application in a Composition Root (global.asax), which gives you a single location with a clearly defined responsibility where all classes are composed.
That would be much more manageable and maintainable that spreading the configuration data all over your classes.
As your question implies, it makes some sense to delegate responsibility for registration to the assembly that knows what needs to be registered. For example, if you
use the SolrNet library, it provides a method that performs component registration, to encapsulate the knowledge of what needs to be registered and to spare the library's consumer from having to learn all about the library before getting started.
However, there is a potential issue with this approach. Would your registration requirements change if you used the dependent assemblies in other applications? For example, would it make sense to register something as ComponentLifeStyle.HttpRequestScoped and then use it in a non-Web application? By delegating registration to the dependency, you are coupling the dependency to its consumer's registration requirements (and to its choice of IoC container).
Autofac (I can't speak for other IoC containers) provides a way round this. It enables you to override registrations so that the most recently registered component is used when a service is resolved. This means that you can call a library's registration method and then register your own services to override the defaults.
There is another problem with your proposed attribute-based registration - it doesn't enable you to specify a lambda expression as a component creator. How would you implement a registration like this with attributes?
builder.Register(c => new A(c.Resolve<B>()));
It might be preferable to define an IRegistrar interface, and then use reflection to search all loaded assemblies for implementations and invoke them. Perhaps something like this:
public interface IRegistrar
{
void RegisterComponents();
}

Using Structuremap how do i inject a property of an MVC Controller into the Constructor of a Service the Controller uses

I'm new to using StructureMap as an IOC container for asp.MVC. One of my controllers takes an IStreamService interface in the constructor.
This is easily linked to a concrete class implementation of StreamService like so
For<IStreamService>().HttpContextScoped().Use<StreamService>();
The problem i'm facing is that the concrete class constuctor takes an IPrincipal parameter, which needs to be injected. I want to pass the User property of the instantiating Controller into the Concrete Service. Could someone please point me in the right direction?
No problem, just add this line to your configuration:
For<IPrincipal>().Use(() => HttpContext.Current.User);
The use of a lambda causes this to be evaluated every time the dependency is requested (as opposed to being a single instance at configuration time.

Anyone can explain the provider model in asp.net 2.0

Preferablly with a simple example.
The spec can be found at: http://msdn.microsoft.com/en-us/library/ms972319.aspx
From http://en.wikipedia.org/wiki/Provider_model
The .NET extensible provider model allows a "component" to have multiple implementations using an abstract factory pattern approach. Providers are a subclass of the ProviderBase class and typically instantiated using a factory method.
An example would be membership providers. At runtime it works out which provider to use based on configuration settings. The provider must adhere to a specification (defined usually by an interface). It creates an instance of the type specified that can fulfill the requirements of the specification, and then calls methods on it to do the work.
This lets you augment and enhance default functionality to provide your own implementation (ie: custom authentication logic) using a standard interface.
Very similar to abstract factory and builder patterns.

Resources