ASP.NET MVCシリーズフレームワーク構築(三)のサービス層の構築

18600 ワード

邯鄲学歩


私は知らない菜鳥ですが、バックグラウンドのプログラマーから小さなアーキテクチャ師になる夢もあります.アーキテクチャ師は考えていないことをよく知っています.
私はもうしばらく働いたことがあります.私は本当に卒業してから1年ぐらい働いて、その数ten thousandの給料をもらうことができると妄想しています.噴き出すな!
 
私たちが育った環境は違っていて、誰かが栽培しているかもしれないし、一人で黙々と奮闘するしかないかもしれません.いずれにしても、
私たちはまず車輪を作ることを学ばなければなりませんが、私は4つの高品質の車輪しか作れません.私の車をしっかり走らせなさい.
これらの計画を実現することをよく知っていて、私は普通の人が払うことができない努力を払わなければなりません!
 
これらのものは,できたら言うに値しないもので,高貴なものではない.
読者に新しい認識と知識をもたらすことを望んでいます.
皆さん、弟を応援してください.最後に、ソース+データベースを視聴者にアップロードします.
続々と更新する.あなたができるまで更新します!!

前編の続き


編二では一つの工場を通じて一つのスレッド内で唯一IDBContactを継承するDBCContactを作成する.
別の工場を通じて、ObjextContextを継承するModelContainerがDBContactと書かれた属性をスレッド内で一意に作成します.
コンテキストのSavechange()をDBContactにカプセル化
SQLを実行してサービス層で論理を制御させます.もちろん倉庫層を外したModelContainerのSavechange()
各倉庫を対応する属性に書きます. 
目的:ビジネスロジック(ここではサービス層と呼ぶ)は、このクラスのみを介してデータ層(ここでは倉庫層と呼ぶ)にアクセスすることである.

①DBContact.cs

using LKBS.CommunicationPlatform.IDAL;
using System.Data.Objects;//        
using LKBS.CommunicationPlatform.Factory;
using System.Data.SqlClient;

namespace LKBS.CommunicationPlatform.DAL
{
 public class DBContact : IDBContact
 {

  public ObjectContext currentEFContext { get; set; }
  ISingleEFCntextFactory singleEFCntextFactory = new SingleEFCntextFactory();//SingleEFCntextFactory    DBContact
  public DBContact()
  {
   currentEFContext = singleEFCntextFactory.GetCurrentEFContext();//
  }

  public int excuteSQL(string cmdText, SqlParameter[] parammeters)
  {
   return currentEFContext.ExecuteStoreCommand(cmdText, parammeters);
  }
  //    BaseRespository   savechang ,             
  //            ,            ,               。      。          
  public int saveChange()
  {
   return this.currentEFContext.SaveChanges();
  }
  public IUserInforRespository userinforResponsity { get; set; }
 }
}

②IDBContact.cs

using System.Data.Objects;
using System.Data.SqlClient;//       

namespace LKBS.CommunicationPlatform.IDAL
{
  public interface IDBContact
   {
    //       {}
    ObjectContext currentEFContext { get; set; }
    int excuteSQL(string cmdText, SqlParameter[] parammeters);
     int saveChange();
    IUserInforRespository userinforResponsity { get; set; }
    //...        , T4    OK
   }
}

重点表示


 
⑥BaseService.csにおける倉庫層のインスタンスを取得する論理.
インスタンスを取得するためにBaseServiceでインスタンスを取得する抽象的な方法を書きます.
サブクラス④でUserInforServiceがこのメソッドを実装しなければならないことをクラスから継承し、保証するために、BaseServiceを抽象クラスに書きました.書かなければなりません!
いつ実行しますか?サブクラスがインスタンス化されると、ベースクラス構築メソッドが呼び出されます.したがって,このインスタンスを得る抽象的な方法をベースクラスの構造関数で呼び出す.
 
OKは、このような大通を言って、実は抽象類と抽象方法の実践運用です.また,サブクラスインスタンス化を用いてベースクラスの構造方法を呼び出すことも実践的である.
最後に、この大通は循環です.理解したらそう思う.この方式はサブクラスのベースクラスのベースクラスの調子のサブクラスの循環を実現しました!
 

サービス層の作成


この層の構築の過程で、私は穴があいていて、木にはpublicがあるか、木にはプログラムセットの引用があるか、木にはusingがあるか、インタフェースオブジェクトにはインスタンスインタフェースにすでに備えられている方法が落ちず、いつも警告しています.
非静的変数や属性やメソッドなどを直接付与することはできず,最終的には解決した.最終的なプログラムには警告はありません.すべて通過する.
フレームの組み立ての差は多くない.データベースは実際に作成しましたが、送信されませんでした.
次はフロントに乗り始めます.
この過程で、成長の過程で、私は記録します.これからはこのような間違いを少なくします.たとえ間違いがあっても、私はすぐに修正します.

③IUserInforService.cs

using LKBS.CommunicationPlatform.Model;

namespace LKBS.CommunicationPlatform.IBll
{
 public interface IUserInforService : IBaseService<UserInfor>
  {

  }
}

④UserInforService.cs

using LKBS.CommunicationPlatform.IBll;
using LKBS.CommunicationPlatform.Model;
using LKBS.CommunicationPlatform.IDAL;
using LKBS.CommunicationPlatform.DAL;


namespace LKBS.CommunicationPlatform.Bll
{
  public class UserInforService:BaseService<UserInfor>, IUserInforService
   {
     public override void setCurrentRespository()
     {//        dbContact         public
       this.currentRespository = this.dbContact.userinforResponsity;
     }
   }
}

⑤IBaseService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LKBS.CommunicationPlatform.IBll
{
 public interface IBaseService<T>
  {
  bool AddEntity(T entity);
  bool DeleteEntity(T entity);
  bool UpdateEntity(T entity);
  T SelectOneEntity(Func<T, bool> wherelamda);
  IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda);
  IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount);
  }
}

⑥BaseService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LKBS.CommunicationPlatform.Model;
using LKBS.CommunicationPlatform.IDAL;
using LKBS.CommunicationPlatform.DAL;
using LKBS.CommunicationPlatform.Factory;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;


namespace LKBS.CommunicationPlatform.Bll
{
 public abstract class BaseService<T> where T : class,new()
 {
  //      :using;   public         
  ISingleDBContactFactory singleDBContactFactory = new SingleDBContactFactory();
  public IDBContact dbContact;
  public BaseService()
  {
   dbContact = singleDBContactFactory.CreateDBCotactUseEF();
   //                !!!!!         ,      。
   //       ,         ,           ,               。            
   //                            。!!
   setCurrentRespository();
  }
  public abstract void setCurrentRespository();
  //      2      “T”                  
  //       where T:class,new()
  public IBaseRespository<T> currentRespository;

  public bool AddEntity(T entity)
  {
   currentRespository.AddEntity(entity);
   if (dbContact.saveChange() > 0)  //   saveChange,        ,    0.0
   {
    return true;
   }
   else
   {
    return false;
   }
   
  }

  public bool DeleteEntity(T entity)
  {

   currentRespository.DeleteEntity(entity);
   
   if (dbContact.saveChange()>0)
   {
    return true;
   }
   else
   {
    return false;
   }
 
  }

  public bool UpdateEntity(T entity)
  {
   currentRespository.UpdateEntity(entity);
   if (dbContact.saveChange()>0)
   {
    return true;
   }
   else
   {
    return false;
   }
  
  }
  /// <summary>
  ///     
  /// </summary>
  /// <param name="wherelamda"></param>
  /// <returns></returns>
  public T SelectOneEntity(Func<T, bool> wherelamda)
  {
   return currentRespository.SelectOneEntity(wherelamda);
  }
  /// <summary>
  ///              
  /// </summary>
  /// <returns></returns>
  public IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda)
  {
   return currentRespository.SelectAllEntityList(wherelamda);
  }
  int rowcount = 0;
  //     <S>         ,     ,“    ”<S>.     <S>,          :          <S>   
  public IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount)
  {//   pageCount,    
   return currentRespository.SelectPageEntityList<S>(orderLamda,whereLamda, pageIndex, pageSize, isAsc, out rowcount);
   }
  }

 }

つぎの続き


データベースはすでに建てました.もちろん、データベースを改善する必要があります.結局、需要の分析は変化します.
最後に、アーキテクチャがデータベースを使用できないため、私は送信します.最後に官親が自動的にデバッグをするのを見たとき、私は自分で奉仕します.これは小さな「干物」だろう.自励する
T 4テンプレートの使用、データベーステーブルの生成、完全とデータベーステーブルのすべてのマッピング.
次の編ではフロントに乗り始めました.これは数日かかると思います.