Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Service and Repository layers in asp.net mvc

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to add Service and Repository layers to asp.net mvc. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The Service layer is mainly our business logic layer, which does not deal with the underlying Database, but deals with the Repository data persistence layer with Database. This article minimizes the coupling of the three layers of Controller,Service,Repository by using StructureMap dependency injection.

This system uses NorthWind open source data, and uses EntityFramework5.0 to realize the Object mapping to the database.

Before we get to the point, let's take a look at the formed frame structure, which we will expand around this screenshot.

Undefined

First of all, let's take a look at the contents of the Project of TYStudioDemo.Models.

There is a collection of objects mapped by our EntityFramwork edmx file and Northwind database table. Instead of using the default way to generate a bunch of .tt files when you set up ADO.Net Entity Data Model, you use the old form. The way to achieve this is to first set up the data model according to the default program, and then delete the .tt file after establishing the data model. Then open the .edmx file, right-click in the space and select Properties (attribute). The screenshot below will appear. At this time, you only need to modify the value of Code Generation Strategy (the Chinese translation does not know what it is, the first one is correct). The default is None, we change it to Default, and then save. Edmx

You should have noticed that there is an extra TYEntities.cs file in the project, which we are the key to implementing Transaction (transaction processing) in our system.

We use the static and [ThreadStatic] attributes to ensure that a thread always gets the same TYEntities (ObjectContext), which solves the problem of Transaction transactions. Please read the comments in the following code in detail if you haven't explained them.

The copy code is as follows:

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Text

Using System.Threading.Tasks

Using System.Web

Namespace TYStudioDemo.Models

{

Public partial class TYEntities

{

# region Fields

/ / define the index name

Const string ContextKey = "TYEntities"

/ / static fields marked ThreadStaticAttribute are not shared between threads.

/ / each thread of execution has a separate instance of the field, and sets and gets the value of the field independently. If you access the field in a different thread, the field will contain different values.

[ThreadStatic]

Private static TYEntities _ current

# endregion

# region Properties

Public bool Disposed {get; set;}

/ / /

/ / when the system works under HttpContext, the delay home technology will be used to return a TYEntities (ObjectContext), and if there is no HttpContext, it will return null

/ / /

/ / No matter where TYEntities is used, the TYEntities.Cleanup () method needs to be called after the request ends.

/ / the best way is to put TYEntities.Cleanup () in the Global.asax.cs file.

/ / void Application_EndRequest (object sender, EventArgs e)

/ {

/ TYStudioDemo.Models.TYEntities.Cleanup ()

/}

/ / /

Private static TYEntities ForWebRequest

{

Get

{

Var context = HttpContext.Current

/ / check whether HttpContext exists

If (context! = null)

{

/ / try to get TYEntities from context

Var result = context.Items [ContextKey] as TYEntities

If (result = = null)

{

/ / create a new datacontext and save it to context

Result = new TYEntities ()

Context.Items [ContextKey] = result

}

Return result

}

Return null

}

}

/ / /

/ this is a public property used to get TYEntities (ObjectContext)

/ / /

/ / if you get TYEntities through HttpContext, no matter where you use TYEntities, you need to call the TYEntities.Cleanup () method at the end of the request

/ / /

/ / if you don't get the TYEntities through HttpContext, you must call the TYEntities.Cleanup () method after using it to clean up the ObjectContext.

/ / /

/ / one thing to note is that no matter which way we use to get TYEntities, we must manually clean up and Dispose TYEntities (ObjectContext).

/ / so never use TYEntities (ObjectContext) in a using block.

/ / /

Public static TYEntities Current

{

Get

{

/ / obtain datacontext from HttpContext

Var result = TYEntities.ForWebRequest

If (result! = null)

Return result

/ / try to get the currently active TYEntities

If (_ current = = null)

_ current = new TYEntities ()

Return _ current

}

}

/ / /

/ / Clean up TYEntities (ObjectContext)

/ / /

Public static void Cleanup ()

{

If (HttpContext.Current! = null)

{

Var result = HttpContext.Current.Items [ContextKey] as TYEntities

If (result! = null)

Result.Dispose ()

HttpContext.Current.Items [ContextKey] = null

}

Else if (_ current! = null)

{

_ current.Dispose ()

_ current = null

}

}

Protected override void Dispose (bool disposing)

{

Bool disposed = Disposed

Disposed = true

If (! disposed)

Cleanup ()

Base.Dispose (disposing)

}

# endregion

}

}

Thank you for reading! This is the end of the article on "how to add Service and Repository layers in asp.net mvc". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report