LINQ to SQL Mapping的文章。

caotoulei 2009-05-05 10:45:39
我同事写的一篇关于LINQ to SQL Mapping的文章,英文好的看一下。讨论一下哈。给点评论。
...全文
164 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rimtd0314 2010-11-10
  • 打赏
  • 举报
回复
逍遥,你日思夜想的如梦姐来了
caotoulei 2009-05-11
  • 打赏
  • 举报
回复
图片好像贴不出来,谁帮忙贴一下,或者大家直接去他的blog看
caotoulei 2009-05-11
  • 打赏
  • 举报
回复
我把他文章翻译了,高手出来点评一下啊:
LINQ to SQL Mapping...I think it could work
这个代码例子是我从Rob Conery's ASP.NET MVC Storefront的视频教学中得来的灵感。这里是一些原因我用这种框架而不是使用NHibernate在这个项目上。

1.我可以随时修改它。。。这就是接口的编程美感
2.LINQ to SQL 真的是太简单了。。。对,我听过。。有人说LINQtoSQL不行了,但是只是不被支持
3.它与其他框架不同,我还没见过类似的框架被成功实现。
4.我和好奇我的方法能不能在程序员社区中抛砖引玉得到其他人的想法。

首先,我建立了我的基本模型,像这样:

接着,我建立我的LINQ to SQL, 像这样:(当然,假设我已经进行过测试了:) )


所以我建立我的ISchoolRepository 像这样:
public interface ISchoolRepository
{
School GetSchoolById(int schoolId);
IQueryable<school> GetAll();
IQueryable<student> GetEnrolledStudentsBy(int schoolId);
}

OK, 我们现在可以开始有趣的东西了。。。也许可以称为创新的东西。我不能确定,但是我希望是呵呵。首先提醒一下我用了一些SQL profiling, 但是不全是并且我想用更多因为我比较重视performance并且不喜欢用很多数据库连接。现在。。。如果有人已经立即发现了什么漏洞或者需要注意的地方,请让我知道。我最新加上的用来取代我的mappers static classes是我把它们在datacontext上写成extension methods。。。我想这样更有道理。 好, 我们继续。。。

所以, 这是我对ISchoolRepository接口的implementation:
public class SqlSchoolRepository : ISchoolRepository, IDisposable
{
private readonly LearningDataContext dc;
public SqlSchoolRepository()
{
dc = new LearningDataContext();
}

public Core.Model.School GetSchoolById(int schoolId)
{
return dc.GetSchoolById(schoolId); //EXTENSION METHOD
}

public IQueryable<Core.Model.School> GetAll()
{
return dc.GetAllSchools(); //EXTENSION METHOD
}

public IQueryable<Core.Model.School> GetEnrolledStudentsBy(int schoolId)
{
return dc.GetAllStudents() //EXTENSION METHOD
.WhereIsEnrolled(true) //FILTER
.WithSchoolId(schoolId);//FILTER
}

public void Dispose()
{
dc.Dispose();
}
}

Now you see how I have the filters added on too like Rob did in his videos? I think the filters are very cool! Mainly for the readability. Okay, so here are my extension methods on the datacontext:

现在我如何把filter加入就像Rob在他视频里做的一样。我觉得filter很酷。只要是它让代码的可读性变强。好,我的extension methods 在 datacontext上:

internal static class SchoolMap
{
internal static School GetSchoolById(this LearningDataContext dataContext, int schoolId)
{
var s = dataContext.Schools.SingleOrDefault(x => x.SchoolId == schoolId);

return new School()
{
ContactNumber = s.ContactNumber,
County = s.LookupCounty.FullName,
FullName = s.FullName,
ShortName = s.ShortName
};
}

internal static IQueryable<School> GetAllSchools(this LearningDataContext dataContext)
{
var schools = from s in dataContext.Schools
orderby s.FullName
select new School()
{
ContactNumber = s.ContactNumber,
County = s.LookupCounty.FullName,
FullName = s.FullName,
ShortName = s.ShortName
};

return schools;
}
}

有一件事情应该注意到的是mapping是完全EXACTLY和GetSchoolById & GetAllSchools一样。。。不幸的是,我还没发想一个解决这个问题的方法。我觉得这主要是我的LINQ知识有限造成的,而不是这种结构的问题。希望LINQ的专家可以在这点上帮助我。理想上它应该像这样:

private static Core.Model.School getSchoolFrom(Impl.School sqlschool)
{
return new School()
{
ContactNumber = sqlschool.ContactNumber,
County =sqlschool.LookupCounty.FullName,
FullName = sqlschool.FullName,
ShortName = sqlschool.ShortName
};

可以肯定这样是可行的。你最好和上面一样写student mapping,你可以建立这样的filters:
internal static class StudentFilters
{
internal static IQueryable<Student> WhereIsEnrolled(this IQueryable<Student> qry, bool enrolledStatus)
{
return qry
.Where(x => x.IsEnrolled == enrolledStatus);
}

internal static IQueryable<Student> WithSchoolId(this IQueryable<Student> qry, int schoolId)
{
return qry
.Where(x => x.HomeSchool.Id == schoolId);
}
}

现在我肯定你要问我“多对多”要怎么处理,我如何存数据和其他东西。“多对多”是要根据不同的情况,我把attendancedata存在student因为这是一个attendance的app, 并且每一个网页上我都要用到“student”类并也需要attendance的数据。我没有把contacts和student存在一起是因为我不需要它们。所以我在我的student repository建立了GetAllContactsByStudentId 方法去取信息。这样做显然没有NHibernate那样灵活并且不会代替,但是我想这对这种小的不需要很多事务处理的app比较好。

你会注意到我让我的LINQ to SQL objects internal 并且只使用他们在mapping extension methods里。 我注意到Rob并没有让他的代码 “internal”, 我想这样会是strorefront它们应该有的样子跟令人迷惑。我需要下载最新的版本看看他们是怎么回事。关于如何存数据我还没好点子。我现在做的这个app只是一个“read-only”的项目,但是还是有少许地方需要做数据的“updating”和“saving”, 我会把我如何处理的在我 blog里贴出来。

这样,我的UI只需要和我的repositories和model打交道。。。酷吧!(翻者:这种方法做到低耦合真的很酷!)这样的话,如果我想转到NHibernate,它很简单从LINQ to SQL 转到NHibernate。再次提一下我不推荐使用这种方法如果你有很多的数据库读写操作并且需要搞性能的话。

不管怎样,我觉得这种方法真的很酷呵呵所以我愿意分享。我建完之后我会存下来并且贴出来。(翻者:我现在在用他这个小框架,结构很清晰,非常readable,而已易于代码更新维护!!)谢谢希望看到你们的反映!
caotoulei 2009-05-05
  • 打赏
  • 举报
回复
忘了帖地址
http://derans.blogspot.com/2009/04/linq-to-sql-mappingi-think-it-could.html
caotoulei 2009-05-05
  • 打赏
  • 举报
回复
请大家看完了再他的文章后面留点言哈,他跟我说想看看中国的程序员的看法。我自己刚开始用LINQ to SQL,用的就是他的这个框架,结构很清楚。我也没用过NHibernate.有用过的朋友写点体会在他博客上,
wuyq11 2009-05-05
  • 打赏
  • 举报
回复
up
ansiboy 2009-05-05
  • 打赏
  • 举报
回复
好的,看看

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧