谁能给我指出一个示例或简要描述如何使用 Linq to SQL 作为数据访问层但创建 WCF RIA Services DomainService 的自定义实现使用 .dbml 文件(这是因为 Linq to SQL 模型是由自定义工具生成的,高度自定义,并且是具有 50 多个表的相当大的数据库)并且没有 VS2010用于创建 DomainService 的向导(该向导取决于可用的 .dbml 文件)
这是我到目前为止自己尝试过的一个非常简单的 shell:
[EnableClientAccess()]
public class SubscriptionService : DomainService
{
[Query(IsDefault = true)]
public IQueryable<Subscription> GetSubscriptionList()
{
SubscriptionDataContext dc = new SubscriptionDataContext();
var subs = dc.Subscription.Where(x => x.Status == STATUS.Active)
.Select(x =>
new Subscription
{
ID = x.ID,
Name = x.Name
}).ToList();
return subs.AsQueryable();
}
public void InsertSubscription(Subscription sub)
{
if (!sub.ID.IsEmpty())
{
SubscriptionDataContext dc = new SubscriptionDataContext();
Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
if (tmpSub != null)
{
tmpSub.Name = sub.Name;
dc.Save(tmpSub);
}
else
{
tmpSub = new Subscription();
tmpSub.Name = sub.Name;
dc.Save(tmpSub);
}
}
}
public void UpdateSubscription(Subscription sub)
{
if (!sub.ID.IsEmpty())
{
SubscriptionDataContext dc = new SubscriptionDataContext();
Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
if (tmpSub != null)
{
tmpSub.Name = sub.Name;
dc.Save(tmpSub);
}
}
}
public void DeleteSubscription(Subscription sub)
{
if (!sub.ID.IsEmpty())
{
SubscriptionDataContext dc = new SubscriptionDataContext();
Subscription tmpSub = dc.GetByID<Subscription>(sub.ID);
if (tmpSub != null)
{
dc.Delete(tmpSub);
}
}
}
}
到目前为止这似乎有效。有没有人看到我可能遗漏的这种方法有任何问题?如果有人已经尝试过这种方式并发现它存在一些主要问题,我不想在错误的道路上走得太远。
感谢大家的意见。
最佳答案
这样做没有错。
我基本上做过同样的事情。
您将需要为每个查询创建一个返回 IQueryable 的属性,并且您将使用 RIA 服务自动神奇地获取 skip/take/where 内容。
[EnableClientAccess()]
public class SubscriptionService : DomainService
{
[Query(IsDefault = true)]
public IQueryable<Subscription> GetSubscriptionList()
{
using(var dc = new SubscriptionDataContext())
return from x in dc.Subscription
where x.Status == STATUS.Active
select new Subscription { ID = x.ID, Name = x.Name };
// make sure you don't call .ToList().AsQueryable()
// as you will basically load everything into memory,
// which you don't want to do if the client is going to be using
// any of the skip/take/where features of RIA Services.
// If you don't want to allow this,
// simply return an IEnumerable<Subscription>
}
}
我假设 Subscription
是 DTO 而不是 L2S 类,因为您正在显式实例化它。只要确保您的 DTO 具有正确的属性即可。即
public class Subscription
{
[Key]
// you must have a key attribute on one or more properties...
public int ID { get; set; }
}
如果您的 DTO 中有子元素,请使用 Include
和 Association
属性:
public class User
{
[Key]
public int Id { get; set; }
[Include]
[Association("User_Subscriptions", "Id","UserId")]
// 'Id' is this classes's Id property, and 'UserId' is on Subscription
// 'User_Subscriptions' must be unique within your domain service,
// or you will get some odd errors when the client tries to deserialize
// the object graph.
public IEnumerable<Subscription> Subscriptions { get; set; }
}
另请注意,您的 delete 方法不需要完整的对象,这样的方法可以工作,并且可以防止客户端序列化整个对象并在您不需要时将其发回。
public void DeleteSubscription(int id)
{
using(var dc = new SubscriptionDataContext())
{
var sub = dc.GetById<Subscription>(id);
if( sub != null ) dc.Delete(sub);
}
}
关于c# - 使用 Linq to SQL 自定义实现 DomainService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7262720/