2012-11-30 67 views
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並使用「static」方法將DapperRepository修改爲「static」是一個好主意,這樣我就可以在不實例化的情況下調用方法。我想知道這樣做是否會有任何表現收益。

此外,任何輸入,以改善這樣的設計是表示讚賞。

+0

你提到*控制器*,你在ASP.MVC應用程序工作嗎? – jwaliszko

+0

是的,它是一個MVC應用程序。由於它更多的是設計問題,我沒有詳細說明。 – Sunny

回答

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

我希望它給你另外看看這個話題。

+0

感謝系列文章。看起來這是一個完全不同的設計。按照現在,我將不得不堅持這一點,因爲我幾乎完成了應用程序。我問這個問題的代碼重構。 – Sunny