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 に答える