發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導(dǎo)圖
隨筆
相冊(cè)
原創(chuàng)同步助手
其他工具
圖片轉(zhuǎn)文字
文件清理
AI助手
留言交流
代碼已托管在 https://code.csdn.net/denghao156/ktnmb_mvc4
先上圖,設(shè)計(jì)模式參考:ddmvc4.codeplex.com
public UnitOfWork() : base("name=EntitiesDb") { this.Configuration.ProxyCreationEnabled = true; this.Configuration.LazyLoadingEnabled = true; } public void Commit() { base.SaveChanges(); } public void CommitAndRefreshChanges() { bool saveFailed = false; do { try { base.SaveChanges(); saveFailed = false; } catch (DbUpdateConcurrencyException ex) { saveFailed = true; ex.Entries.ToList() .ForEach(entry => { entry.OriginalValues.SetValues(entry.GetDatabaseValues()); }); } } while (saveFailed); } public void RollbackChanges() { base.ChangeTracker.Entries() .ToList() .ForEach(entry => entry.State = EntityState.Unchanged); }
1.2: Kt.DAL
基礎(chǔ)倉(cāng)儲(chǔ)類對(duì)UnitOfWork 的引用來實(shí)現(xiàn)多個(gè)表實(shí)體的統(tǒng)一操作。
public class Repository<T> : IRepository<T> where T : class { #region Members IQueryableUnitOfWork _UnitOfWork; #endregion #region Constructor /// <summary> /// 創(chuàng)建倉(cāng)儲(chǔ)實(shí)例 /// </summary> /// <param name="unitOfWork">Associated Unit Of Work</param> public Repository(IQueryableUnitOfWork unitOfWork) { if (unitOfWork == (IUnitOfWork)null) throw new ArgumentNullException("unitOfWork"); _UnitOfWork = unitOfWork; }
1.3:Kt.Respository
User 表倉(cāng)儲(chǔ)類。
public class UserRepository : Repository<User>, IUserRepository { public UserRepository(UnitOfWork unitOfWork) : base(unitOfWork) { } }
1.4:Kt.Service
User 表類的業(yè)務(wù)層的實(shí)現(xiàn)
public class UserService : IUserService { private readonly IUserRepository _userRepository; public UserService(IUserRepository userRepository) { this._userRepository = userRepository; } public UserDto GetUserById(int id) { return KtMapper.CreateMap<User, UserDto>(_userRepository.Get(id)); } public void AddUser(UserDto userDto) { User user = KtMapper.CreateMap<UserDto, User>(userDto); _userRepository.Add(user); _userRepository.UnitOfWork.Commit(); }
二、Castle Windsor DI MVC控制器依賴注入
1.1、Kt.Web
創(chuàng)建注入工廠
/// <summary> /// 依賴注入工廠 /// </summary> public class WindsorControllerFactory : DefaultControllerFactory { private readonly IKernel kernel; public WindsorControllerFactory(IKernel kernel) { this.kernel = kernel; } public override void ReleaseController(IController controller) { kernel.ReleaseComponent(controller); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (controllerType == null) { throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path)); } return (IController)kernel.Resolve(controllerType); } }
1.2、 Dependency 注入容器
public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register(Classes.FromThisAssembly() .BasedOn<IController>() .LifestyleTransient()); container.Register( Component.For<IQueryableUnitOfWork, UnitOfWork>().ImplementedBy<UnitOfWork>(), Component.For<IUserRepository, UserRepository>().ImplementedBy<UserRepository>(), Component.For<IUserService>().ImplementedBy<UserService>(), AllTypes.FromThisAssembly().BasedOn<IHttpController>().LifestyleTransient() ) .AddFacility<LoggingFacility>(f => f.UseLog4Net()); LoggerFactory.SetCurrent(new TraceSourceLogFactory()); EntityValidatorFactory.SetCurrent(new DataAnnotationsEntityValidatorFactory()); }
1.3、MvcApplication 應(yīng)用程序啟動(dòng)時(shí)控制器的依賴注入容器的實(shí)現(xiàn)
public class MvcApplication : System.Web.HttpApplication { private readonly IWindsorContainer container; public MvcApplication() { this.container = new WindsorContainer().Install(new Dependency.Dependency()); } public override void Dispose() { this.container.Dispose(); base.Dispose(); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var controllerFactory = new WindsorControllerFactory(container.Kernel); ControllerBuilder.Current.SetControllerFactory(controllerFactory); } }
具體代碼的實(shí)現(xiàn)見框架源代碼:https://code.csdn.net/denghao156/ktnmb_mvc4
預(yù)計(jì)用的工具有:
本節(jié)參見大神所寫,日后想自己做一個(gè)商城出來,代碼持續(xù)更新到 code.csdn.net ……
來自: 昵稱10504424 > 《工作》
0條評(píng)論
發(fā)表
請(qǐng)遵守用戶 評(píng)論公約
在EntityFramework6中管理DbContext的正確方式
public void RandomServiceMethod(Guid accountId) { // 強(qiáng)制創(chuàng)建一個(gè)新范圍(也就是說,我們將使用我們自己 // 的DbContext 實(shí)例) using (var dbContextScope = _dbContextScopeFactory.Create(DbCon...
應(yīng)用程序框架實(shí)戰(zhàn)二十二 : DDD分層架構(gòu)之倉(cāng)儲(chǔ)(層超類型基礎(chǔ)篇)
Ef { /// <summary> /// 倉(cāng)儲(chǔ) /// </summary> /// <typeparam name=''''''''TEntity''''''''>實(shí)體類型</typep...
基于NHibernate的UnitOfWork+Repository模式(AutoFac)–Part1
基于NHibernate的UnitOfWork+Repository模式(AutoFac)–Part1.最近寫了一系列的UnitOfWork模式和Repository模式的文章,你可以在這里查...
C#版MVC框架PureMVC的深入分析和改良方案
在PureMVC中,通知(Notification)貫穿整個(gè)框架,把觀察者模式發(fā)揮得淋漓盡致。如果把Notification當(dāng)作是郵件,那么Name就是收件人,不...
setOnClickListener on TextView
up vote 4 down vote favorite 1.//set up for model selection TextView modelTextview = (TextView)addView.findViewById(R.id.modelEdit);<TextView android:id="@+id/model" android:...
MVC實(shí)用架構(gòu)設(shè)計(jì)(三)——EF-Code First(1):Repository,UnitOfWork,DbContext
Data 2 { 3 /// <summary> 4 /// 數(shù)據(jù)單元操作接口 5 /// </summary> 6 public interface IUnitOfWorkContext : IUnitOfWor...
Model View Presenter (MVP) design pattern close look - Part 1 (Supervising Controller) - VusCode - C
View contains the Presenter instance (view "knows" presenter) Presenter is the only class knowing how to reach to mo...
WebQQ密碼MD5加密算法的C#實(shí)現(xiàn)
應(yīng)網(wǎng)友之邀為大家提供一下WebQQ的MD5加密算法,因?yàn)镸D5是WebQQ模擬登錄過程中最難的部分,所以在這里不能不提及。要操作MD5,C#中自帶一個(gè)MD5類可供選擇,相當(dāng)方便。先說說WebQQ MD5加密的方式,公式是...
DDD領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)初探(2):倉(cāng)儲(chǔ)Repository(上)
2、站在架構(gòu)的層面,倉(cāng)儲(chǔ)解耦了領(lǐng)域?qū)雍蚈RM之間的聯(lián)系,這一點(diǎn)也就是很多人設(shè)計(jì)倉(cāng)儲(chǔ)模式的原因,比如我們要更換ORM框架,我們只需要改變...
微信掃碼,在手機(jī)上查看選中內(nèi)容