如何组织DAL在ASP.NET MVC [英] How to organize DAL in ASP.NET MVC

查看:176
本文介绍了如何组织DAL在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net mvc的项目来组织数据访问层。我读过有关这个有很多不同的文章,所以我还是有一些问题,为了解决这个问题才能完成:


  1. 我应该在数据库或全部或者一个genereal实例创建每个实体存储库的情况下,例如 PostRepository 可以包括像发表,注释标签


  2. 在控制器我要得到一些数据,变换成视图模型,并将其传递到视图。哪里是做到这一点的最佳地点? 服务控制器或其他什么东西?


  3. 如果是服务。我应该如何多的服务创造?还为每个实体传递到控制器3或4的服务,如果它是necessery?或者,也许不喜欢它,我想做到在资料库? (创建一个共同的服务,将包含库的一些计数。 PostService ,像 PostRepository ,<$ C $库C> CommentRepository 和 TagRepository



解决方案

下面是我的看法:


  

我应该建立资料库的实例在数据库中每个实体
  或所有或一种genereal实例,例如PostRepository可以
  包括像邮政的实体,注释和标记?


有一个单一的通用存储库会为你节省大量的维护头痛。你可以实现一个通用信息库,如:

  ///&LT;总结&gt;
///此接口提供访问数据源的抽象。
///&LT; /总结&gt;
公共接口IDataContext:IDisposable接口
{
    IQueryable的&LT; T&GT;查询&LT; T&GT;()其中T:类;    不要再增加&LT; T&GT;(T项目),其中T:类;    INT更新&LT; T&GT;(T项目),其中T:类;    虚空删除&LT; T&GT;(T项目),其中T:类;    ///&LT;总结&gt;
    ///允许库来控制的SaveChanges时()被调用
    ///&LT; /总结&gt;
    INT的SaveChanges();
}

和实现上面单上下文类接口。

有些人实现独立的特定存储库以及


  

在控制器我要得到一些数据,变换成和视图模型
  它传递到视图。哪里是做到这一点的最佳地点?服务,
  控制器或其他什么东西?


定义所有模型(DTO或实体或POCO)班在一个单独的装配从DA,服务和Web访问。服务方法返回模型实例,控制器转换成他们视图模型(使用AutoMapper),并通过查看。同样是在POST方法控制器首先转换成虚拟机模型,然后传递到服务层的持久性或处理。


  

如果是服务。我应该如何多的服务创造?此外,每
  实体和传递到控制器3或4的服务,如果它是necessery?要么
  也许不喜欢它,我想做到在资料库? (创建一个共同的
  服务,将包含库的一些计数。 PostService,
  像PostRepository,CommentRepository和存储库
  TagRepository)


我强烈建议你定义的服务非常具体。使用单一职责原则来定义你的服务。每个服务应提供相关的功能集。例如。 AuthService将会验证用户不能发送电子邮件,这EmailService工作。

我建议的模式提供不同的服务工作,非常漂亮。例如:

 公共类DriverSearchService:IDriverSearchService
{
    私人只读IBlobService _blobService;
    私人只读IDataContext _dataContext;    公共DriverSearchService(IDataContext的DataContext,IBlobService blobService)
     {
         _blobService = blobService;
        _dataContext = DataContext的;
     }    公共无效SaveDriveSearch(INT searchId)
    {
        //获取来自临时存储和清晰的临时存储值
        VAR项目= context.Query&LT; SearchTempStore方式&gt;()单(S = GT; s.SearchId == searchId);        //临时对象,所以最新更新总店
        VAR mainSearch = context.Query&LT;搜索方式&gt;()单(S = GT; s.Id == searchId);
        mainSearch.LastExecutedOn = DateTime.UtcNow;
        mainSearch.DataAsBlob = item.DataAsBlob;
        context.Update(mainSearch);
    }
}

I am trying to organize data access layer in asp.net mvc project. I've read a lot different articles about this, so still I have some questions in order to finish with this problem:

  1. Should I create instances of repository for every entity in database or for all or one genereal instance, for example PostRepository can include entities like Post, Comment and Tag?

  2. In controller I have to get some data, transform in into ViewModel and pass it into view. Where is the best place to do this? Services, Controller or something else?

  3. If it is Service. How many services should I create? Also for every entity and pass into Controller 3 or 4 services if it is necessery? Or maybe do it like I wanted to do it in repository? (Create one common service which would contain some count of repositories. PostService, with repositories like PostRepository, CommentRepository and TagRepository)

解决方案

Here is my take:

Should I create instances of repository for every entity in database or for all or one genereal instance, for example PostRepository can include entities like Post, Comment and Tag?

Having one single generic repository will save you a lot of maintenance headache. You could implement single generic repository like:

/// <summary>
/// This interface provides an abstraction for accessing a data source.
/// </summary>
public interface IDataContext : IDisposable
{
    IQueryable<T> Query<T>() where T : class;

    T Add<T>(T item) where T : class;

    int Update<T>(T item) where T : class;

    void Delete<T>(T item) where T : class;

    /// <summary>
    /// Allow repositories to control when SaveChanges() is called
    /// </summary>
    int SaveChanges();
}

and implement above interface in single context class.

Some people implement separate specific repositories as well.

In controller I have to get some data, transform in into ViewModel and pass it into view. Where is the best place to do this? Services, Controller or something else?

Define all your model (DTO or entities or POCO) classes in a separate assembly accessible from DA, service and Web. Service methods returns model instance, controller convert them into viewmodel (use AutoMapper) and pass to view. Again in post method controller first convert VM into Model and then pass to Service layer for persistance or processing.

If it is Service. How many services should I create? Also for every entity and pass into Controller 3 or 4 services if it is necessery? Or maybe do it like I wanted to do it in repository? (Create one common service which would contain some count of repositories. PostService, with repositories like PostRepository, CommentRepository and TagRepository)

I strongly suggest you define service very specific. Use Single Responsibility Principle to define your services. Each service should provide related set of functions. E.g. AuthService will authenticate user not sending them email, that EmailService job.

The pattern I suggest work very nicely with different services. For example:

public class DriverSearchService : IDriverSearchService
{
    private readonly IBlobService _blobService;
    private readonly IDataContext _dataContext;

    public DriverSearchService(IDataContext dataContext, IBlobService blobService)
     {
         _blobService = blobService;
        _dataContext = dataContext;
     }

    public void SaveDriveSearch(int searchId)
    {
        // Fetch values from temp store and clear temp store
        var item = context.Query<SearchTempStore>().Single(s => s.SearchId == searchId);

        // Temp object is latest so update the main store
        var mainSearch = context.Query<Search>().Single(s => s.Id == searchId);
        mainSearch.LastExecutedOn = DateTime.UtcNow;
        mainSearch.DataAsBlob = item.DataAsBlob;
        context.Update(mainSearch);
    }
}

这篇关于如何组织DAL在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆