I am after some advice on using entity framework in an efficient way with a WCF service called from an MVC application.
我想要一些关于如何有效地使用来自MVC应用程序的WCF服务的建议。
I have a ASP.NET MVC web application as the presentation layer that calls through to a WCF service which does all the data manipulation. The WCF application uses entity framework to perform the operations on the DB.
我有一个ASP。NET MVC web应用程序作为表示层调用WCF服务,该服务执行所有数据操作。WCF应用程序使用实体框架在DB上执行操作。
The problem is that depending on the page that is being rendered the information required from the DB will change, for example if I want to show a list of all the users I might just want their username but the WCF service would return the whole model, this obviously becomes very inefficient when working with large numbers of records.
问题是,根据所呈现的页面所需的信息从数据库将会改变,例如,如果我想给所有用户的列表我可能只是希望自己的用户名,但WCF服务将返回整个模型,这显然非常低效当处理大量的记录。
Is there any ways to make this more efficient, need to somehow be able to specify the values for Include and Select in the Linq statements
是否有任何方法可以使这个过程更有效,需要以某种方式能够在Linq语句中指定Include和Select的值
1 个解决方案
#1
1
If i get you right, you're actually saying that your Models
are not compatible with the needs of your Views
.
如果我说得对,你实际上是在说你的模型与你的观点的需求不一致。
That is (one of the reasons) why people are using Data Transfer Objects (DTO)
as the objects returned from the data service, and not the entities themselves.
这就是为什么人们使用数据传输对象(DTO)作为从数据服务返回的对象,而不是实体本身。
Usually, you will need to project / map
your entities into the DTO object.
通常,您需要将您的实体投射到DTO对象中。
for example if I want to show a list of all the users I might just want their username but the WCF service would return the whole model
例如,如果我想显示所有用户的列表,我可能只想要他们的用户名,但是WCF服务会返回整个模型
For this particular scenario you could try something like this:
对于这个特定的场景,您可以尝试如下方法:
// Your 'User' Entity
public class User : BaseEntity
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
// Base class for all 'Mappable' DTOs
public abstract class Mappable<ENTITY, DTO> where ENTITY : BaseEntity
{
public abstract DTO ToDTO(ENTITY entity);
}
// Your DTO (specific to your grid needs)
public class UsersGridDTO : Mappable<User, UsersGridDTO>
{
public int Id { get; set; }
public string Username { get; set; }
public override UsersGridDTO ToDTO(User entity)
{
return new UsersGridDTO
{
Id = entity.Id,
Username = entity.Username
};
}
}
// In your WCF data service
public IEnumerable<DTO> GetData<DTO>() where DTO : Mappable<Entity, DTO>, new()
{
return efContext.Users.Select(new DTO().ToDTO);
}
Also, When using Asp.Net MVC, the DTOs can be used as your ViewModels
as well (see here).
另外,当使用Asp。Net MVC, dto也可以用作您的视图模型(参见这里)。
You can also use frameworks like AutoMapper
that can handle the Entity to DTO mapping for you.
您还可以使用AutoMapper之类的框架,它可以为您处理从实体到DTO的映射。
#1
1
If i get you right, you're actually saying that your Models
are not compatible with the needs of your Views
.
如果我说得对,你实际上是在说你的模型与你的观点的需求不一致。
That is (one of the reasons) why people are using Data Transfer Objects (DTO)
as the objects returned from the data service, and not the entities themselves.
这就是为什么人们使用数据传输对象(DTO)作为从数据服务返回的对象,而不是实体本身。
Usually, you will need to project / map
your entities into the DTO object.
通常,您需要将您的实体投射到DTO对象中。
for example if I want to show a list of all the users I might just want their username but the WCF service would return the whole model
例如,如果我想显示所有用户的列表,我可能只想要他们的用户名,但是WCF服务会返回整个模型
For this particular scenario you could try something like this:
对于这个特定的场景,您可以尝试如下方法:
// Your 'User' Entity
public class User : BaseEntity
{
public int Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
// Base class for all 'Mappable' DTOs
public abstract class Mappable<ENTITY, DTO> where ENTITY : BaseEntity
{
public abstract DTO ToDTO(ENTITY entity);
}
// Your DTO (specific to your grid needs)
public class UsersGridDTO : Mappable<User, UsersGridDTO>
{
public int Id { get; set; }
public string Username { get; set; }
public override UsersGridDTO ToDTO(User entity)
{
return new UsersGridDTO
{
Id = entity.Id,
Username = entity.Username
};
}
}
// In your WCF data service
public IEnumerable<DTO> GetData<DTO>() where DTO : Mappable<Entity, DTO>, new()
{
return efContext.Users.Select(new DTO().ToDTO);
}
Also, When using Asp.Net MVC, the DTOs can be used as your ViewModels
as well (see here).
另外,当使用Asp。Net MVC, dto也可以用作您的视图模型(参见这里)。
You can also use frameworks like AutoMapper
that can handle the Entity to DTO mapping for you.
您还可以使用AutoMapper之类的框架,它可以为您处理从实体到DTO的映射。