發(fā)文章
發(fā)文工具
撰寫
網(wǎng)文摘手
文檔
視頻
思維導圖
隨筆
相冊
原創(chuàng)同步助手
其他工具
圖片轉文字
文件清理
AI助手
留言交流
代碼已托管在 https://code.csdn.net/denghao156/ktnmb_mvc4
先上圖,設計模式參考: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
基礎倉儲類對UnitOfWork 的引用來實現(xiàn)多個表實體的統(tǒng)一操作。
public class Repository<T> : IRepository<T> where T : class { #region Members IQueryableUnitOfWork _UnitOfWork; #endregion #region Constructor /// <summary> /// 創(chuàng)建倉儲實例 /// </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 表倉儲類。
public class UserRepository : Repository<User>, IUserRepository { public UserRepository(UnitOfWork unitOfWork) : base(unitOfWork) { } }
1.4:Kt.Service
User 表類的業(yè)務層的實現(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 應用程序啟動時控制器的依賴注入容器的實現(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); } }
具體代碼的實現(xiàn)見框架源代碼:https://code.csdn.net/denghao156/ktnmb_mvc4
預計用的工具有:
本節(jié)參見大神所寫,日后想自己做一個商城出來,代碼持續(xù)更新到 code.csdn.net ……
來自: 昵稱10504424 > 《工作》
0條評論
發(fā)表
請遵守用戶 評論公約
在EntityFramework6中管理DbContext的正確方式
public void RandomServiceMethod(Guid accountId) { // 強制創(chuàng)建一個新范圍(也就是說,我們將使用我們自己 // 的DbContext 實例) using (var dbContextScope = _dbContextScopeFactory.Create(DbCon...
應用程序框架實戰(zhàn)二十二 : DDD分層架構之倉儲(層超類型基礎篇)
Ef { /// <summary> /// 倉儲 /// </summary> /// <typeparam name=''''''''TEntity''''''''>實體類型</typep...
基于NHibernate的UnitOfWork+Repository模式(AutoFac)–Part1
基于NHibernate的UnitOfWork+Repository模式(AutoFac)–Part1.最近寫了一系列的UnitOfWork模式和Repository模式的文章,你可以在這里查...
C#版MVC框架PureMVC的深入分析和改良方案
在PureMVC中,通知(Notification)貫穿整個框架,把觀察者模式發(fā)揮得淋漓盡致。如果把Notification當作是郵件,那么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實用架構設計(三)——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#實現(xiàn)
應網(wǎng)友之邀為大家提供一下WebQQ的MD5加密算法,因為MD5是WebQQ模擬登錄過程中最難的部分,所以在這里不能不提及。要操作MD5,C#中自帶一個MD5類可供選擇,相當方便。先說說WebQQ MD5加密的方式,公式是...
DDD領域驅(qū)動設計初探(2):倉儲Repository(上)
2、站在架構的層面,倉儲解耦了領域?qū)雍蚈RM之間的聯(lián)系,這一點也就是很多人設計倉儲模式的原因,比如我們要更換ORM框架,我們只需要改變...
微信掃碼,在手機上查看選中內(nèi)容