将依赖从“上帝”中拉出来DI 框架中的对象

发布于 2024-11-30 05:49:58 字数 1049 浏览 0 评论 0原文

我有一个 Servlet,其中有很多现有代码。我正在尝试将依赖项注入添加到其中的一部分。目前我正在手动执行此操作:

public class AdjustBookPriceHandler extends BookRequestHandler {
    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");
        OfferRepository offerRepository = ((BookData) requestState.getData()).getOfferRepository();

        BookPriceAdjuster priceAdjuster = getBookPriceAdjuster();
        priceAdjuster.adjustPrice(newPrice);
    }

    protected BookPriceAdjuster getBookPriceAdjuster(RequestState requestState, RequestData requestData, Book book) {
        return new BookPriceAdjuster(book, offerRepository);
    }
}

这里,书籍和优惠存储库依赖项通过构造函数注入到 BookPriceAdjuster 中。 getBookPriceAdjuster 方法允许继承自AdjustBookPriceHandler 的类提供不同的价格调节器。

我想开始使用像 Guice 这样的 DI 框架来减少复杂示例会引入的一些样板代码。但是,我不确定在这种情况下使用它的最佳方法。

如何编写绑定来从“上帝”对象 RequestState 和 RequestData 中提取相关依赖项?或者在这一点上使用框架会同样复杂和混乱吗?

I have a Servlet that has a lot of existing code. I'm trying to add dependency injection into one part of it. Currently I am doing it manually:

public class AdjustBookPriceHandler extends BookRequestHandler {
    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");
        OfferRepository offerRepository = ((BookData) requestState.getData()).getOfferRepository();

        BookPriceAdjuster priceAdjuster = getBookPriceAdjuster();
        priceAdjuster.adjustPrice(newPrice);
    }

    protected BookPriceAdjuster getBookPriceAdjuster(RequestState requestState, RequestData requestData, Book book) {
        return new BookPriceAdjuster(book, offerRepository);
    }
}

Here the book and offer repository dependencies are injected into the BookPriceAdjuster through the constructor. The getBookPriceAdjuster method is there to allow classes that inherit from the AdjustBookPriceHandler to provide a different price adjuster.

I would like to start using a DI framework like Guice to reduce some of the boilerplate code that complex examples would introduce. However, I'm unsure of the best way to use it in this context.

How can I write bindings that would pull out the relevant dependencies from the "god" objects RequestState and RequestData? Or at this point would using a framework be just as complicated and messy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩动你心 2024-12-07 05:49:58

我不确定我是否正确理解你想要做什么。这本书是请求范围的,而 OfferRepository 是单例范围的?如果是这样,你可以这样做。

public class AdjustBookPriceHandler extends BookRequestHandler {

    // inject concrete instance with setter or constructor
    private BookPriceAdjusterProvider bookPriceAdjusterProvider;

    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");

        BookPriceAdjuster priceAdjuster = bookPriceAdjusterProvider.getBookPriceAdjuster(book);
        priceAdjuster.adjustPrice(newPrice);
    }

}

public interface BookPriceAdjusterProvider {

    BookPriceAdjuster getBookPriceAdjuster(Book book);

}

public class MyBookPriceAdjusterProvider {

    // inject this through setter or constructor
    private OfferRepository offerRepository;

    protected BookPriceAdjuster getBookPriceAdjuster(Book book) {
        return new BookPriceAdjuster(book, offerRepository);
    }
}

这样,你就摆脱了单例范围的 OfferRepository,并可以使用一些 DI 框架来为你进行注入。如果您的子类等需要,您也可以使用 BookPriceAdjusterProvider 的不同实现。这是你想要的还是我完全误解了你的目标?

I'm not sure if I understand correctly what you are trying to do. The book is request scoped and offerRepository is singleton scoped? If so you can do something like this..

public class AdjustBookPriceHandler extends BookRequestHandler {

    // inject concrete instance with setter or constructor
    private BookPriceAdjusterProvider bookPriceAdjusterProvider;

    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");

        BookPriceAdjuster priceAdjuster = bookPriceAdjusterProvider.getBookPriceAdjuster(book);
        priceAdjuster.adjustPrice(newPrice);
    }

}

public interface BookPriceAdjusterProvider {

    BookPriceAdjuster getBookPriceAdjuster(Book book);

}

public class MyBookPriceAdjusterProvider {

    // inject this through setter or constructor
    private OfferRepository offerRepository;

    protected BookPriceAdjuster getBookPriceAdjuster(Book book) {
        return new BookPriceAdjuster(book, offerRepository);
    }
}

This way, you get rid of the singleton scoped OfferRepository and can use some DI framework to do the injection for you. You can as well use different implementation of BookPriceAdjusterProvider if needed in your subclasses or so. Is this kind of what you wanted or have I misunderstood your objective completely?

情栀口红 2024-12-07 05:49:58

如何编写绑定来从“上帝”对象 RequestState 和 RequestData 中提取相关依赖项?

您可以在这里相当轻松地开始使用 Guice 进行依赖项注入,具体取决于您的应用程序的引导方式。假设您有一个 BooksModule。

public class BooksModule extends AbstractModule {
    protected void configure() {
        // Do Nothing
    }

    @Provides @RequestScoped
    BookData provideBookData(RequestState requestState) {
      return (BookData) requestState.getData();
    }

    @Provides @RequestScoped
    OfferRepository provideOfferRepository(BookData bookData) {
      return bookData.getOfferRepository();
    }
}

现在您可以使用 Guice 将依赖项注入到 BookPriceAdjuster 类中。

@RequestScoped
public class BookPriceAdjuster {
    private final Book book;
    private final OfferRepository offerRepository;

    @Injected
    public BookPriceAdjuster(Book book, OfferRepository offerRepository) {
        this.book = book;
        this.offerRepository = offerRepository;
    }

    // whatever methods it has
}

现在您可以在 servlet 内使用 BookPriceAdjuster 的提供程序。

public class AdjustBookPriceHandler extends BookRequestHandler {

    private final Provider<BookPriceAdjuster> bookPriceAdjusterProvider;

    @Injected
    public AdjustBookPriceHandler(Provider<BookPriceAdjuster> bookPriceAdjusterProvider) {
        this.bookPriceAdjusterProvider = bookPriceAdjusterProvider;
    }

    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");

        BookPriceAdjuster priceAdjuster = bookPriceAdjusterProvider.get();
        priceAdjuster.adjustPrice(newPrice);
    }
}

要引导应用程序,您需要使用 Guice 注入器安装 BooksModule。您如何进行此操作取决于您的应用程序当前的引导方式。对于 servlet,我强烈建议您研究 Guice-Servlet 扩展,它可以让您可以在“类型安全、惯用的 Java”而不是 web.xml 文件中定义 servlet 和依赖项。美丽简单。

或者在这一点上使用框架会同样复杂和混乱吗?

正如您在上面所看到的,您可以慢慢开始直接在现有 Web 应用程序中注入绑定(和提供程序),而无需轻松更改界面或调用模式。剩下的就是 boostrapping...只需将 DI 潜入其中并让它慢慢接管:)

How can I write bindings that would pull out the relevant dependencies from the "god" objects RequestState and RequestData?

You could start using Guice for dependency injection fairly easily here, depending on how your application is bootstrapped. Let's say you have a BooksModule.

public class BooksModule extends AbstractModule {
    protected void configure() {
        // Do Nothing
    }

    @Provides @RequestScoped
    BookData provideBookData(RequestState requestState) {
      return (BookData) requestState.getData();
    }

    @Provides @RequestScoped
    OfferRepository provideOfferRepository(BookData bookData) {
      return bookData.getOfferRepository();
    }
}

Now you can use Guice to inject the dependencies into your BookPriceAdjuster class.

@RequestScoped
public class BookPriceAdjuster {
    private final Book book;
    private final OfferRepository offerRepository;

    @Injected
    public BookPriceAdjuster(Book book, OfferRepository offerRepository) {
        this.book = book;
        this.offerRepository = offerRepository;
    }

    // whatever methods it has
}

And now you can use a provider for your BookPriceAdjuster inside your servlet.

public class AdjustBookPriceHandler extends BookRequestHandler {

    private final Provider<BookPriceAdjuster> bookPriceAdjusterProvider;

    @Injected
    public AdjustBookPriceHandler(Provider<BookPriceAdjuster> bookPriceAdjusterProvider) {
        this.bookPriceAdjusterProvider = bookPriceAdjusterProvider;
    }

    @Override
    public void handleRequest(RequestState requestState, RequestData requestData, Object obj) {
        Book book = (Book) obj;
        long newPrice = Long.parseLong(requestData.getQueryParam("price");

        BookPriceAdjuster priceAdjuster = bookPriceAdjusterProvider.get();
        priceAdjuster.adjustPrice(newPrice);
    }
}

To bootstrap the application, you'll need install the BooksModule using the Guice injector. How you go about this depends on how your application is currently bootstrapped. For servlets, I strongly recommend looking into the Guice-Servlet extension which lets you define your servlets and dependencies in "type-safe, idiomatic Java" instead of the web.xml file. Beautifully simple.

Or at this point would using a framework be just as complicated and messy?

As you can see above, you can slowly start injecting bindings (and providers) directly within an existing web app without changing your interfaces or calling patterns with ease. All that's left is the boostrapping... Just sneak the DI in there and let it slowly take over :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文