1

我在应用程序中使用 Dapper ORM。我创建了一个带有 Dapper 方法的接口,以便快速浏览此应用程序中正在使用 Dapper 的哪些功能,并且可以通过实现它轻松地被其他 ORM 替换。

public interface IDapperRepository
{            
    IEnumerable<T> GetAll<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class;
    T GetById<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class;
}   


class DapperRepository : IDapperRepository
{   
    public IEnumerable<T> GetAll<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class
    {
        //implementation
    }

    public T GetById<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class
    {
        //implementation
    }
}

从 DAL 层:

public class UserRep : IUserRep
{
    private readonly IDapperRepository _iDapperRepository;
    public UserRep()
    {
         _iDapperRepository = new DapperRepository();
    }

    public IEnumerable<UserBO> GetAll()
    {
          return _iDapperRepository.GetAll<UserBO>("select * from users");
    }
    //Other methods
}

在用户列表页面中,_iUserRep.GetAll() 被控制器调用。

从上面的代码中,通过调用 _iUserRep.GetAll() 或存储库类中的任何其他方法,DapperRepository 类被实例化。我的问题是,因为我在 DapperRepository 类中只有实用程序方法,所以删除 IDapperRepository 并使用“静态”方法将 DapperRepository 修改为“静态”是个好主意,这样我就可以在不实例化它的情况下调用这些方法。我想知道这样做是否会带来任何性能提升。

此外,感谢任何改进此设计的输入。

4

1 回答 1

0

由于这是关于可取概念的问题,我将介绍我会做什么。首先尝试限制您的抽象和开销复杂性 - 首先简化,最后自动化。您是否有可能更改存储库实现以使用不同的 ORM?如果没有,请不要使用这种方法。这是老式的存储库风格,乍一看似乎还可以,但最终被重新评估和多余 - 至少在我看来。由于它是 ASP.MVC 应用程序,您可以尝试使用命令代替:

public abstract class BaseController : Controller
{
    public IDocumentSession DocumentSession { get; set; }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        DocumentSession = ...OpenSession(); // initialize and open session
    }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.IsChildAction)
        {
            return;
        }

        using (DocumentSession)
        {
            if (filterContext.Exception != null)
            {
                return;
            }

            if (DocumentSession != null)
            {
                DocumentSession.SaveChanges();
            }
        }
    }

    public void ExecuteCommand(Command cmd)
    {
        cmd.DocumentSession = DocumentSession;
        cmd.Execute();
    }

    public TResult ExecuteCommand<TResult>(Command<TResult> cmd)
    {
        ExecuteCommand((Command)cmd);
        return cmd.Result;
    }
}

抽象命令定义:

public abstract class Command
{
    public IDocumentSession DocumentSession { get; set; }

    public abstract void Execute();
}

public abstract class Command<T> : Command
{
    public T Result { get; protected set; }
} 

示例命令实现:

public class GetUsers : Command<IList<User>>
{
    public IList<int> IDs { get; set; }

    public override void Execute()
    {
        return DocumentSession.Query<User>()...;
    }
}

用法 - 执行表单控制器动作:

[HttpGet]
public NJsonResult GetUsers(string ids)
{
    var result = ExecuteCommand(new GetUsers
                                    {
                                        IDs = ids
                                    });
    //...
}

在我看来,这不是一个微不足道的问题。需要相当多的考虑。为了更好地理解它,您可以在空闲时间尝试浏览 Ayende 的博客:

http://ayende.com/blog/4784/architecting-in-the-pit-of-doom-the-evils-of-the-repository-abstraction-layer

连同下面的系列(简短的帖子,真的很快就能看完):

http://ayende.com/blog/153889/limit-your-abstractions-analyzing-a-ddd-application

http://ayende.com/blog/153921/limit-your-abstractions-application-eventsndash-what-about-change

http://ayende.com/blog/153953/limit-your-abstractions-application-eventsndash-proposed-solution-1

http://ayende.com/blog/153985/limiting-your-abstractions-reflections-on-the-interface-segregation-principle

http://ayende.com/blog/154017/limit-your-abstractions-application-eventsndash-proposed-solution-2ndash-cohesion

http://ayende.com/blog/154049/limit-your-abstractions-application-eventsndash-event-processing-and-rx

...从这里开始变得更有趣:

http://ayende.com/blog/154081/limit-your-abstractions-you-only-get-six-to-a-dozen-in-the-entire-app

http://ayende.com/blog/154113/limit-your-abstractions-commands-vs-tasks-did-you-forget-the-workflow

http://ayende.com/blog/154145/limit-your-abstractions-all-cookies-looks-the-same-to-the-cookie-cutter

http://ayende.com/blog/154177/limit-your-abstractions-so-what-is-the-whole-big-deal-about

http://ayende.com/blog/154209/limit-your-abstractions-refactoring-toward-reduced-abstractions

http://ayende.com/blog/154241/limit-your-abstractions-the-key-is-in-the-infrastructurehellip

http://ayende.com/blog/154273/limit-your-abstractions-and-how-do-you-handle-testing

我希望它能让你对这个话题有另一种看法。

于 2012-11-30T16:17:21.333 回答