MVC4 mall project 1: Framework Design, mvc4 mall project framework
Code is hosted on https://code.csdn.net/denghao156/ktnmb_mvc4
First, for the design mode, see ddmvc4.codeplex.com.
I. untitled fwork Design Mode http://www.cnblogs.com/zxj159/p/3505457.html The Unit Of Work mode records all the information modified by the object model. When the object model is submitted, it is modified at one time and the results are synchronized to the database. This process is typically encapsulated in transactions. Therefore, the advantage Of using the Unit Of Work mode in DAL is that it can ensure data integrity. If a problem occurs when holding a series Of business objects (the same transaction, you can roll back all the modifications to ensure that the data is always in a valid state without dirty data. 1.1: Kt. DAL. unitOfWork inherits DbContext to add, delete, modify, and query object models. saveChanges () is used to update data, so that the data changed in the cache context is committed to the data once for transactions. 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 );}View Code
1.2: Kt. DAL
The basic storage class references UnitOfWork to implement unified operations on multiple table entities.
Public class Repository <T>: IRepository <T> where T: class {# region Members IQueryableUnitOfWork _ UnitOfWork; # endregion # region Constructor // <summary> // create a warehouse instance // </summary> /// <param name = "unitOfWork"> Associated Unit Of Work </param> public Repository (IQueryableUnitOfWork unitOfWork) {if (unitOfWork = (IUnitOfWork) null) throw new ArgumentNullException ("unitOfWork"); _ UnitOfWork = unitOfWork ;}View Code
1.3: Kt. Respository
User table Storage Class.
Public class UserRepository: Repository <User>, IUserRepository {public UserRepository (UnitOfWork unitOfWork): base (unitOfWork ){}}View Code
1.4: Kt. Service
Implementation of the business layer of the User table class
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 ();}View Code
Ii. Castle Windsor di mvc controller dependency Injection
1.1. Kt. Web
Create an injection Factory
/// <Summary> /// dependency injection factory /// </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 );}}View Code
1.2 Dependency injection container
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 ());}View Code
1.3. Implementation of the controller dependency injection container when the MvcApplication application starts
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 );}}View Code
See the implementation of specific code framework source code: https://code.csdn.net/denghao156/ktnmb_mvc4
Expected tools:
- Visual Studio 2012
- ASP. net mvc 4 with Razor View Engine
- Entity Framework 5.0
- Castle Windsor for DI
- SQL Server 2008/2012
- Bootstrapt & JQuery
- Automapper
For this section, see what I wrote. In the future, I want to create a marketplace myself. The Code will be updated to code.csdn.net ......
NET40 is installed on the remote server, and the new project is developed based on the MVC40 framework. How can I upgrade it?
Mvc4 can run on. net 4.0. If your project does not have a 4.5-based technology, you should be able to run it directly on the server.
Create a framework webpage and click the project in the left framework to display the corresponding content in the right framework.
<Html>
<Head>
<Meta HTTP-EQUIV = "Content-Type" CONTENT = "text/html; charset = gb2312">
<Title> 0000000000 </title>
</Head>
<Frameset rows = "23, *" framespacing = "0" border = "0" frameborder = "0">
<Frame name = "banner" scrolling = "no" noresize target = "contents" src = "Admin_Head.Asp" marginwidth = "1" marginheight = "1">
<Frameset cols = "165, *">
<Frame name = "contents" target = "main" src = "Admin_Left.Asp" marginwidth = "1" marginheight = "1" scrolling = "yes" noresize>
<Frame name = "main" scrolling = "yes" src = "Admin_Main.Asp" noresize target = "_ self">
</Frameset>
<Noframes>
<Body>
<P> </p>
</Body>
</Noframes>
</Frameset>
</Html>