开发者

linq design patterns

I'm building an ASP web application and for the moment I have a namespace called Queries that contains the linq queries that are called from the code behind pages. The whole site will initially contain about 40 queries; more will be added later.

Should I keep all my queries in one large namespace or should I create a namespace for the queries of each page? For instance, QueriesP开发者_C百科ageA, QueriesPageB, QueriesPageC... and end up with about 10 smaller namespaces.

Thanks.


It sounds like you're building a business logic layer.

If you're using LINQ to SQL or Entity Framework, you will already have a collection of entity classes that closely represent your business domain.

I prefer to add my queries to entity classes as static methods. This keeps my queries neatly distributed (so I don't end up with one huge business logic class) and easy to find (a query that retrieves a set of users will live in the User class).

If the query produces an aggregate or report (e.g. quantity of cookies sold grouped by year), then I usually create a new class for the report. That way, the report becomes a model in its own right, which works well in an MVC architecture, if that's something you're considering.


I think splitting them out is a good idea, but it would be more readable and less confusing to create namespaces based on the data they're returning instead of the page they're originally used on, e.g. MyApp.Queries.Customers, MyApp.Queries.Orders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜