Autofac / C #: generic factory for a specific base class?

Is it possible for autofac to create a generic factory that can only resolve types of a specific base class?

Currently, I see how appropriate it is to modify the C # brownfield project to use autofac. The project requires the creation of a tree of components of different types, but of the same base class. These components require authentication services and databases (among other things), so it is difficult for him to simply create empty constructors.

Thus, the factory will be such that the user can create objects that produce the base class, use autofac to scan these classes, and provide a factory for them. I thought this could simplify the assembly of the component object tree if you do not need to provide factories for each class.

Aside, or is it a sign of poor design? Should the components and the tree do as little as possible, and we pass this tree to other services for processing and rendering?

Something like (where? Mystery factory)

public MyBase { 
    public Add(MyBase x) {..}
}

public DerivedA: MyBase {}

public DerivedB : MyBase {}

public DerivedC : DerivedA {}

public SomethingElse
{
     private ? _myBaseFact;

     public SomethingElse(? myBaseFact) { 
            _myBaseFact = myBaseFact;
     }

     public BuildComponentTree() {
        DerivedA a = _myBaseFact<DerivedA>();
        DerivedB b = _myBaseFact<DerivedB>();
        DerivedC c = _myBaseFact<DerivedC>();
        a.Add(b);
        b.Add(c);
     }
}

edit: , :) , , autofac. factory , factory ? , , , .

FwComponent {
    public FwComponent (DataProvider, AuthManager, OtherModule)
    public Add(FwComponent x);
    public Setup();
    public Process();
    public Render();
}

Page : FwComponent {
    public Page(DataProvider, AuthManager, OtherModule): base(..)
    public string Title;
    ...
}

Input: FwComponent {
    public Input(DataProvider, AuthManager, OtherModule): base(..)
    public string Name;
    public int length;
    ...
}

Button: FwComponent {
    public Button(DataProvider, AuthManager, OtherModule): base(..)
    public string Name;
    ...
}

----

MyCustomButton : Button {
     public MyCustomButton(DataProvider, AuthManager, OtherModule): base(..)
}

MyPersonalPage : FwComponent {

    IContext _container;

    public MyPersonalPage(DataProvider, AuthManager, OtherModule, IContext container): base(..)

    public Setup() {
         var input = _container.Resolve<Input>();
         input.Name = 'xxx';
         input.length = 10;
         Add(input);

         var butt = _container.Resolve<MyCustomButton>();
         butt.name = "xxx";
         Add(butt);    
    }

    public Process() {
        DataProvider.DoStuff();
    }
}
+3
2

, , , , , _myBaseFact<DerivedA>() _myBaseFact<DerivedB>(). , , , .

, : factory, , . factory :

1: - ( ), factory , :

public class MyBaseFactory : IMyBaseFactory
{
    public MyBase CreateMyBase(IUserContext user)
    {
        // Create a MyBase based on a user object.
    }
}

2: factory factory:

public class MyBaseFactory : IMyBaseFactory
{
    private Autofac.IContainer container;

    public MyBase CreateMyBaseForScenarioA()
    {
        return container.Resolve<DerivedA>();
    }

    public MyBase CreateMyBaseForScenarioB()
    {
        return container.Resolve<DerivedB>();
    }
}

, factory MyBase . , .

IMO, , Autofac factory . , , . factory, factory . , .

+2

, , , factory . , ...

1) ,

, , , , , . ( ) . MVC - Twitter factory .

2) factory

AutoFac, - factory, , Unity - Castle Windsor - ... ... . , , - factory - , .

3)

- , , . , ( ), , Add : MyBase, , , , . , SomethingElse, , , node: , node .


, , , :

interface INotificationService { }
class TwitterNotificationService : INotificationService { }
class FacebookNotificationService : INotificationService { }
class CompositeNotificationService : INotificationService
{
    public CompositeNotificationService(IEnumerable<NotificationService> services) { }
    // implement composite pattern
}

class NotificationController : Controller
{
    public NotificationController(INotificationService service)
    {
        if (service === null)
        {
            throw new ArgumentNullException("service");
        }

        // ...
    }
}

, DI, -

var controller = new NotificationController(
        new CompositeNotificationService(new []
        {
            new TwitterNotificationService(),
            new FacebookNotificationService()
        }));

DI - , , ( ), , DI, OO, , DI.

+4

All Articles