Task3_Dependency Injection and Data Management in ASP.NET Core
Task3_Dependency Injection and Data Management in ASP.NET Core
in ASP.NET Core
Dependency Injection
Dependency injection is a design pattern that helps make our components more
modular and easier to use. The main idea that exists under this design is that our
classes do not have to worry about creating their own dependencies, but rather they
are responsible for using those same resources with the security that what they are
using is working somewhere else outside of their functions, which makes it a more
modular and reusable class.
When we talk about a modular class or modularity, we are referring to a class that
does not have to worry about making functions or processes that are unrelated to the
base of the class, for example, a users class does not have to know how to connect
to a database, this can be achieved from other places and we simply use that
dependency on users to perform the operation that we have to perform.
When we talk about a concept such as the ability to test our applications or
testability, dependency injection also offers certain facilities that make this stage not
so complicated to perform.
When we perform tests on our classes, this design offers us the facility that during
these tests we can use "false dependencies", this so that we can test the operation
of our classes without having to worry about external services. In this same way we
can simulate the operation of the dependencies that we have and thus be able to
carry out these unit tests without any unwanted situation.
Data Types and Storage
When we talk about the types of data and the way in which we store the information
we have to take into account what is the most optimal to use for our application, in
case we have a large application that handles a lot of data in real time, if we use the
wrong types of data or in an excessive way we can face serious performance
problems when our application is highly demanded by users.
We can find many types of data in programming but there are details that make for
good practice when writing code. Some examples of these that we can find are the
following:
● We should use the int data for most values with integers only. It is
recommended that these numbers not be very long since there is another way
to handle this data. For longer numbers we can use the Long data type,
which allows us to store a number of up to 64 bits.For floating numbers or
numbers with decimals, it is advisable to use Float, but this is only acceptable
when we do not require exact precision when working with this data. To work
with more precise decimals we can use Decimal
● It is recommended that when we work with characters, if we need to store a
single character we should use the Char type. Many times, due to
carelessness or simply because we think that it makes no difference, we use
the String type for these cases, which entails bad practice due to the waste of
memory that we are causing.
● We must use collections more carefully since they can overload memory. We
must use the appropriate collections for each case, for example if we know
the size of the array we can declare the array in the following code int[]
idUsers = new int[5], or if we need to work with a collection that we do not
know its exact size we can use the List<T> lists, which dynamically adapt as
the application runs.
Once the movie is selected, we proceed to start creating the order. To this we add
that we need to obtain the user's information to be able to fill out the details of the
order. These processes can be carried out asynchronously while the user selects the
number of seats they require, whether they are accompanied or going to the cinema
alone.In this way, these requests are made asynchronously, we can also work on the
final amount and prices of the order that is being created. This can be executed once
the user has already selected the number of seats to purchase.
For all these processes, it is worth noting that we must make constant queries from
different classes of the application (Users, Orders), this is done in a simple way if we
inject the dependency of the database in both classes and in this way we can make
the necessary queries to complete the information.
Once the user selects the seats, he proceeds to fill out the payment information. In
many businesses, what is used in these modules are APIs for processing the
payment to the desired bank. This allows greater ease and a more modular and
simple way of working with our classes, using external services, which gives us a
lighter application. When the payment is processed we must save the order with the
receipt number, the total amount of the bill in our database, here comes our
dependencies to be able to store this information in our database.
Now suppose we want to send the tickets that the customer has just purchased to
their email. For this we can use another dependency, where it can provide us with
the way to send the tickets generated through Gmail, Hotmail and SMS. If we inject
that dependency into our Orders class we can successfully send the tickets to the
client.
If we talk about modularity and performance, we must take into account many
factors, especially dependency injection, which is precisely what we are going to
solve in our applications, while also taking care of all the details of data management
that we will keep in mind during the execution of our application. That said, it will
guarantee us an application that will undoubtedly obtain good results and customers
who will be satisfied with the service they are receiving.