开发者

Not sure if this is smart, cheating, or kinda cool

So I already have everything mapped out in my edmx file I created the following

 public sealed class ACategory:example.org.Data.Categories
{
    public ACategory()
    {

    }

    public int PostingCount
    {
        get;
        set;
    }
}

I then decided to figure out how to do a compiled query, just cause it seems cool as hell to do.

public static Func<MyEntities, IQueryable<ACategory>> GetCategoriesWithPostingCount =
        CompiledQuery.Compile((MyEntities entities) => from category in entities.Categories.Include("Postings_Categories")
                                                               select开发者_开发百科 new ACategory
                                                               {
                                                                   CategoryID =category.CategoryID,
                                                                   ParentCategoryID = category.ParentCategoryID,
                                                                   CategoryName = category.CategoryName,
                                                                   CategoryDescription = category.CategoryDescription,
                                                                   PostingCount = category.Postings_Categories.Count
                                                               });

It freakin works... way cool I can now call this easily from anywhere.

My question now is this? Is this a good way to do this or am I making a mistake on something that could be lurking behind the scenes?


What do you mean by call anywhere? Do you mean you're now able to call across layers that you shouldn't normally be calling your database with?

I would argue that this is bad practice, and all data access should be done within, of course, the DAL. It's commonly considered "good practice" to have your DAL pass back only IEnumerables to enforce the fact that the queries cannot be called "at will" down the line.


If you do this, you have to expose your data context. This is not a problem if you are using the compiled query in your DAL alone, but is something to think about. It is a concise way to create business objects, though!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜