是否对某些类型的实体的公共服务层(使用 EF 4.1)有帮助?
我需要一些关于我的服务层的建议。假设我有这个模型:
public abstract class Entity
{
public Guid Id {get;set;}
}
public abstract class Document: Entity
{
public virtual ICollection<Attachment> Attachments{get;set;}
}
public class Attachment: Entity
{
Guid ParentEntityId {get;set;}
//some props....
}
public class Note: Document
{
//some props
}
public class Comment: Document
{
//some props
}
假设我有注释和评论的存储库。以下是服务层的示例(TDto 表示扁平化实体的 DTO):
public interface IMainService<TDto>
{
TDto Create(TDto dto);
void Update(TDto dto);
void Delete(Guid, Id);
}
public interface IDocumentService
{
AttachmentDto AddNewAttachment(AttachmentDto dto);
}
public abstract DocumentService<TEntity>: IDocumentService
{
private IRepository<TEntity> _repository;
public DocumentService(IRepository<TEntity> repository)
{
_repository = repository
}
AttachmentDto AddNewAttachment(AttachmentDto dto)
{
var entity = _repository.GetById(dto.ParentId);
//attachment code
_repository.Update(entity)
_repository.UoW.Commit();
.....
}
public class NoteService: DocumentService<Note>, IMainServcie<NoteDto>
{
public NoteService(INoteRepository repository): base(repository)
{
.....
}
}
public class CommentService: DocumentService<Comment>, IMainServcie<CommentDto>
{
public NoteService(INoteRepository repository): base(repository)
{
.....
}
}
虽然这工作正常,但我觉得我会在应用程序层中重复代码。因此,如果我使用 Asp.Net MVC,我可能有一个 Comment 控制器和一个 Note 控制器。我必须创建在每个控制器上创建附件的方法。
我正在尝试想出一种方法来分离文档服务,这样我就可以拥有一个文档控制器。唯一需要注意的是,我不想将我的实体暴露给应用程序层。我们的一个想法是使用 TDto 键入文档服务方法,并使用某种工厂来拉入存储库和实体类型。这意味着,根据 DTO 类型,我们将查找相关实体可能是一个 switch 语句,并且启动它的实体类型和存储库。
其他信息: 我在 EF 4.1 中的映射是这样的,有一个 Note_Attachments 和 Comment_Attachments 表。
I need some advice on my Service layer. Let's say I have this model:
public abstract class Entity
{
public Guid Id {get;set;}
}
public abstract class Document: Entity
{
public virtual ICollection<Attachment> Attachments{get;set;}
}
public class Attachment: Entity
{
Guid ParentEntityId {get;set;}
//some props....
}
public class Note: Document
{
//some props
}
public class Comment: Document
{
//some props
}
Let's say I have repositories for Notes and Comments. Here is an example of the service layers (the TDto represents the DTOs that flatten the entities):
public interface IMainService<TDto>
{
TDto Create(TDto dto);
void Update(TDto dto);
void Delete(Guid, Id);
}
public interface IDocumentService
{
AttachmentDto AddNewAttachment(AttachmentDto dto);
}
public abstract DocumentService<TEntity>: IDocumentService
{
private IRepository<TEntity> _repository;
public DocumentService(IRepository<TEntity> repository)
{
_repository = repository
}
AttachmentDto AddNewAttachment(AttachmentDto dto)
{
var entity = _repository.GetById(dto.ParentId);
//attachment code
_repository.Update(entity)
_repository.UoW.Commit();
.....
}
public class NoteService: DocumentService<Note>, IMainServcie<NoteDto>
{
public NoteService(INoteRepository repository): base(repository)
{
.....
}
}
public class CommentService: DocumentService<Comment>, IMainServcie<CommentDto>
{
public NoteService(INoteRepository repository): base(repository)
{
.....
}
}
While this works fine, I feel like I would be duplicating code in my application layer. So, if I were using Asp.Net MVC I may have a Comment controller and a Note controller. I would have to create methods for creating attachments on each controller.
I am trying to think of a way to separate out the Document service so I could have a Document controller. The only caveat is that I do not want to expose my entities to the app layer. One thought we had was to type the Document Service methods with the TDto and use some sort of factory to pull the repository and entity types in. Meaning, based of the DTO type we would look-up the related entity is perhaps a switch statement and fire up the entity type and repository for it.
Additional Information:
My mappings in EF 4.1 are such that there is a table for Note_Attachments and Comment_Attachments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终做的是使用服务工厂来获得我想要的服务。我还在 IDocumentService 中添加了一个通用类型。我正在使用 Unity 容器。像这样的事情:
我还有一些重构要做,但这至少从应用程序中抽象了我的服务层。
What I ended up doing was using a Service factory to get the service I wanted. I also added a genric to my IDocumentService. I am using a Unity container. Something like this:
I have some more refactoring to do, but this, at least, abstracts my service layer from the app a bit.