views:

176

answers:

4

I have 2 tables, for now as an example, ClientOrder and Products Table. Using Linq, I have been able to write all the queries that I want to run 1. Search by Client order 2. Search by Client name 3. Search by Product name 4. Search by Product ID

I want to create methods for each of the above queries. The ? is, what pattern is appropriate here? Factory Pattern does not seem to fit the bill as I know that each of my object will be using the same data context.

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

Note: I am 5 months in with the programming world and a newbie

+2  A: 

Hi,

If you don't know what design pattern you should use, you'd better don't use one! In this very simple case I can't think of any useful addition any desing pattern could provide. Perhaps you just want to know how you could implement functions to control those four queries and their results?

Bas,I can implement the static methods/classes. I wanted to get a better understanding from the community about what pattern to use and why. Perhaps I should have added that I am 5 months in with the programming world and a newbie
+1 - No 'pattern' is better than trying to implement one in this circumstance, just plain ol' OO design. Create a class with the methods you want. Refactor once it gets too big/complicated.
SnOrfus
A: 

I would probably go for a simple class (perhaps a singleton) with the methods as instance methods and call it a data access layer. Instantiate against the database required and just LINQ what i need together in the appropriate methods.

Kris
+1  A: 

I suggest to extend 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 assume you use LINQ to SQL or LINQ to Entity, so you can simply extend the generated partial calsses. If you return collections or instance, and if you choose IEnumerable, IQueryable, IList, List, or whatever depends on your needs.

Daniel Brückner
+2  A: 

I found Martin Fowler's Patterns of Enterprise Application Architecture helpful in learning how people structure access to database tables. Some of these patterns are listed here.

For your simpler task, a single class with four static methods sounds perfectly reasonable. But you should consider Fowler's Table Data Gateway pattern, where you package all the access to each table in its own class of static methods (and use a standard naming convention).

Jim Ferrans