DI Interception and UIs

I contribute to an open-source project that heavily uses the ctor-injection design pattern. Recently, there was a discussion about using interception for certain things, including injecting UI commands to a view model, and correctly binding the command to the correct value.

As an example, we have a command:

public class FooCommand : CommandBase    // CommandBase implements ICommand
{
    public bool CanExecute(object obj);
    public void Execute(object obj);
}

We have many of these, and we inject them to the view model with Ninject. It may be relevant that in this specific case, the commands are marked with an attribute so we know which subset to inject:

public class FooViewModel : ViewModelBase
{
    public FooViewModel(IEnumerable<CommandBase> commands) {}
}

Now, in the class defined above, we go like this:

public class FooViewModel : ViewModelBase
{
    public FooCommand FooCommand { get; }

    public FooViewModel(IEnumerable<CommandBase> commands)
    {
        FooCommand = commands.SingleOrDefault(command => command is FooCommand);
    }
}

The public instance of FooCommand is bound to the UI elements for certain actions.

Now, when we tried to work with this using intercepted commands, everything broke down. When we intercepted our implementation and injected Ninject’s implementation, we only received a bunch of CommandBase implementations, so we don’t know what command to bind to which value. Essentially, we are relying on a specific implementation, or any derived implementation, to bind to a specific command; Ninject, however, is returning values at one abstraction level higher than we need.

There are two methods I can see to fix this–A) somehow make Ninject intercept the specific class instead of its interface, and B) add an abstract field to CommandBase to use to figure out what is what. I’m not sure how to do A, or even if it can be done–at least, I’ve not had success with it. B, on the other hand, seems to be 1) adding redundant information (the type already provides this value–when we don’t use interception, that is), and 2) adding another possible place for this to break. What are your recommendations for this?

This is a pretty mangled way to do DI. You are basically doing service locator without the service locator… And the reason it doesn’t work is exactly that: you are using a tool to try and do something it was not meant to do.

The solution is simple: you should only inject the actual classes you NEED by requesting them as they are.

What you are doing here is a valid pattern for some cases (inject an unknown number of classes that extend the same interface and that all need to perform some work), but not yours.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật