使用依赖注入在构造函数外部创建类的新实例

时间:2021-05-03 22:31:39

I am trying a sample application to test out dependency injection. Before using DI, I have the following method in my class:

我正在尝试一个示例应用程序来测试依赖注入。在使用DI之前,我的课程中有以下方法:

public IQueryable<BookDTO> GetBooks()
{
    var books = from b in db.Books
                select new BookDTO()
                {
                    Id = b.Id,
                    Title = b.Title,
                    AuthorName = b.Author.Name
                };

    return books;
}

BookDTO is a data transfer object defined in another project. Now I want to loosely couple my projects together. So I created IDTOBase interface and made BookDTO implement this. I have a unity container where I have made the relevant registering of the BookDTO class to IDTOBase.

BookDTO是另一个项目中定义的数据传输对象。现在我想把我的项目松散地结合在一起。所以我创建了IDTOBase接口并使BookDTO实现了这一点。我有一个统一容器,我已经将BookDTO类的相关注册到IDTOBase。

But how will I rewrite the LINQ query in my original method? What will take the place of "new BookDTO()"?

但是,如何在原始方法中重写LINQ查询?什么将取代“新BookDTO()”?

Thanks

3 个解决方案

#1


You start off with part of the answer I think: 'BookDTO is a data transfer object'. As such there is little value in abstracting it as it belongs in a very specific layer of your application.

你从我想到的部分答案开始:'BookDTO是一个数据传输对象'。因此,抽象它几乎没有价值,因为它属于应用程序的一个非常特定的层。

The BookDTO has the role of being a purely data representation of a book (in some presumably serializable form). This will occur at a low level in your application stack. Any code that requires the use of such data should create a domain object 'Book' that can be used in the code. This decouples the persistence and retrieval of the book data (BookDTO) from it's domain representation (Book).

BookDTO的作用是作为书籍的纯粹数据表示(以某种可能的可序列化形式)。这将在应用程序堆栈中的低级别发生。任何需要使用此类数据的代码都应该创建一个可以在代码中使用的域对象“Book”。这将书籍数据(BookDTO)的持久性和检索与其域表示(Book)分离。

Your interface definition I don't think serves this purpose and so DI doesn't serve a purpose here. Where I feel DI comes into play here is in the retrieval of Book DTOs. A class that loads book data would have such a service injected and use it to retrieve BookDTO instances.

我不认为你的界面定义是为了这个目的,因此DI在这里没有用处。我认为DI在这里发挥作用的是书籍DTO的检索。加载书籍数据的类将注入此类服务并使用它来检索BookDTO实例。

#2


Well, whoever instantiates the object will need to know what actual type to make it.

那么,实例化对象的人将需要知道实际的类型。

What it looks like you need is some sort of repository, which knows about IDTOBase, as well as BookDTO. Your application would know about IDTOBase. You're application would make a call like BookRepo.GetBooks, in which your repository signature would look like IQueryable<IDTOBase> GetBooks.

您需要的是某种存储库,它知道IDTOBase以及BookDTO。您的应用程序将了解IDTOBase。您的应用程序将调用BookRepo.GetBooks,其中您的存储库签名看起来像IQueryable GetBooks。

#3


You need another class that can resolve the dependency for you.

您需要另一个可以为您解决依赖关系的类。

In most cases of DI you say "Give me a 'class' and resolve any mapped Interfaces it has to its mapped class instance", what you need is that 'class.'

在DI的大多数情况下,你会说“给我一个'类'并解决它对映射类实例的任何映射接口”,你需要的是“类”。

I liked some of these examples when I was learning DI. http://www.asp.net/web-api/overview/advanced/dependency-injection

当我学习DI时,我喜欢其中的一些例子。 http://www.asp.net/web-api/overview/advanced/dependency-injection

#1


You start off with part of the answer I think: 'BookDTO is a data transfer object'. As such there is little value in abstracting it as it belongs in a very specific layer of your application.

你从我想到的部分答案开始:'BookDTO是一个数据传输对象'。因此,抽象它几乎没有价值,因为它属于应用程序的一个非常特定的层。

The BookDTO has the role of being a purely data representation of a book (in some presumably serializable form). This will occur at a low level in your application stack. Any code that requires the use of such data should create a domain object 'Book' that can be used in the code. This decouples the persistence and retrieval of the book data (BookDTO) from it's domain representation (Book).

BookDTO的作用是作为书籍的纯粹数据表示(以某种可能的可序列化形式)。这将在应用程序堆栈中的低级别发生。任何需要使用此类数据的代码都应该创建一个可以在代码中使用的域对象“Book”。这将书籍数据(BookDTO)的持久性和检索与其域表示(Book)分离。

Your interface definition I don't think serves this purpose and so DI doesn't serve a purpose here. Where I feel DI comes into play here is in the retrieval of Book DTOs. A class that loads book data would have such a service injected and use it to retrieve BookDTO instances.

我不认为你的界面定义是为了这个目的,因此DI在这里没有用处。我认为DI在这里发挥作用的是书籍DTO的检索。加载书籍数据的类将注入此类服务并使用它来检索BookDTO实例。

#2


Well, whoever instantiates the object will need to know what actual type to make it.

那么,实例化对象的人将需要知道实际的类型。

What it looks like you need is some sort of repository, which knows about IDTOBase, as well as BookDTO. Your application would know about IDTOBase. You're application would make a call like BookRepo.GetBooks, in which your repository signature would look like IQueryable<IDTOBase> GetBooks.

您需要的是某种存储库,它知道IDTOBase以及BookDTO。您的应用程序将了解IDTOBase。您的应用程序将调用BookRepo.GetBooks,其中您的存储库签名看起来像IQueryable GetBooks。

#3


You need another class that can resolve the dependency for you.

您需要另一个可以为您解决依赖关系的类。

In most cases of DI you say "Give me a 'class' and resolve any mapped Interfaces it has to its mapped class instance", what you need is that 'class.'

在DI的大多数情况下,你会说“给我一个'类'并解决它对映射类实例的任何映射接口”,你需要的是“类”。

I liked some of these examples when I was learning DI. http://www.asp.net/web-api/overview/advanced/dependency-injection

当我学习DI时,我喜欢其中的一些例子。 http://www.asp.net/web-api/overview/advanced/dependency-injection