What design pattern is offered here?

I have 2 tables, for now the ClientOrder and Products table as an example. Using Linq I was able to write all the queries I want to run 1. Search by customer order 2. Search by customer name 3. Search by product name 4. Search by product ID

I want to create methods for each of the above queries.? is there a pattern that fits here? Factory Sample doesn't seem to fit the bill as I know each of my objects will use the same data context.

Is it wiser to just create a static class with 4 static methods?

Note. I am doing 5 month programming and newbie.

0


a source to share


5 answers


If you don't know which design template you should be using, you better not use it! In this very simple case, I cannot think of any useful addition that any template can provide. Perhaps you just want to know how to implement functionality to manage these four queries and their results?



+2


a source


I found Martin Fowler Enterprise Application Architecture Patterns , helped to learn how people structure access to database tables. Some of these patterns are listed here .



For your simple task, one class with four static methods sounds perfectly reasonable. But you should consider the Gateway Data Data Gateway pattern, where you package all access to each table in its own static method class (and use a standard naming convention).

+2


a source


First of all, consider the intent of the Factory Method Pattern:

Define an interface for instantiating the object, but let subclasses which class to instantiate. Factory Method allows a class to defer instantiating subclasses.

Does this relate to your problem? You are more concerned with how you will manage requests than creating domain objects.

IMHO, I would avoid any implementation alternative that includes a static class / method: you cannot inherit from a static class in order to restrict the extensibility of your model. The same applies to static methods in non-static classes: they cannot be overridden in subclasses.

I would not add these query methods to your domain objects. Remember that you are using OOP to simulate the real world, does it make sense to ask the Order to look for other orders?

Let's think about it: in the real world, you use an order to get certain information from it (its date, customer name, or related product). When you want to find an order, you go to where you store it and look for it (say, in a file cabinet). That is, you do not use the order to find other orders.

With that in mind, you need to simulate this situation in your software. You already have objects that model the order and product. What you are missing is an object that models the location that you use to store orders and find them.

In general, such objects (which store or retrieve other objects) are called repositories. In your case, it could be called ClientOrderRepository. What would this object do? Well, you already mentioned this: run the four different queries that you need. Let's take a look at a possible definition of its interface:

public interface IClientOrderRepository {
    ClientOrder FindOrderWithIdMatching(int anOrderId);
    ClientOrder FindOrderWithClientNameMatching(string aClientName);
    ClientOrder FindOrderWithProductNameMatching(string aProductName);
    ClientOrder FindOrderWithProductIdMatching(string aProductId);
}

      

If you only need one instance of a class that will implement this interface, you can use the Singleton pattern. Don't rely on implementation options (such as static methods) that can be difficult to change later.

Finally, even if you find specific patterns that solve your problem, it is good to think about objects and how they interact to get the job done. Use real-life metaphors to help you find missing objects or responsibilities to complete. In the end, it's all about the essence of the object-oriented paradigm.

For more in-depth information on the repository pattern, here are some resources to help you get started:

+2


a source


I suggest extending the classes with static methods. Something like the following.

IEnumerable<Client> Client.GetByName(String Name) { }

IEnumerable<Product> Product.GetByName(String Name) { }

Product Product.GetById(Guid Id) { }

      

I am assuming you are using LINQ to SQL or LINQ for Entity, so you can simply extend the generated partial scores. If you return the collection or instance, and if you choose to IEnumerable

, IQueryable

, IList

, List

or all, depending on your needs.

+1


a source


I would probably go for a simple class (maybe one) with methods as instance methods and call it the data access layer. Create an instance for the required database and just LINQ, which I need together in the appropriate methods.

0


a source







All Articles