Share the common three-tier architecture and Analysis Based on EF + WCF

Source: Internet
Author: User
Document directory
  • This project combines EF 4.3 and WCF to implement a classic three-tier architecture, interface-oriented interfaces at each layer, SOA and Repository encapsulation calls for WCF, and WCFContext, dynamic service calls, And a paging instance.
This project combines EF 4.3 and WCF to implement a classic three-tier architecture, interface-oriented interfaces at each layer, SOA and Repository encapsulation calls for WCF, and WCFContext, dynamic service calls, And a paging instance.
1. Project architecture diagram:

 

 

2. Project solution:

 

  • In the traditional three-tier architecture, WcfService (server), WcfClientProxy (client service call), and WcfExtension (some extensions) are added)

 

 

3. Implementation of Wcf Service:

 

  • The factory implements RemoteServiceFactory (for remote calls) and RefServiceFactory (for local reference to call the service layer) to generate a client proxy, both of which must implement IServiceFactory's "IService CreateService ();"
  • RemoteServiceFactory dynamically generates the client proxy IService through ChannelFactory and caches this object.
  • WCFExtension implements WCFContext, which can transmit user login or IP context information and intercept the mechanism of writing logs.

3. data layer Repository implementation:

 

  • The domain model is decoupled from the customer code and data ing layer by using an interface similar to a set used to access domain objects. The specific implementation code is as follows:
View Code

1 public class DaoBase: IRepository, IDisposable
2 {
3 public DbContext context;
4
5 public DaoBase ()
6 {
7 this. context = new EasyEF. DAL. DbContext ();
8}
9
10 public T Update <T> (T entity) where T: class
11 {
12 var set = context. Set <T> ();
13 set. Attach (entity );
14 context. Entry <T> (entity). State = EntityState. Modified;
15 context. SaveChanges ();
16
17 return entity;
18}
19
20 public T Insert <T> (T entity) where T: class
21 {
22 context. Set <T> (). Add (entity );
23 context. SaveChanges ();
24 return entity;
25}
26
27 public void Delete <T> (T entity) where T: class
28 {
29 context. Entry <T> (entity). State = EntityState. Deleted;
30 context. SaveChanges ();
31}
32
33 public T Find <T> (params object [] keyValues) where T: class
34 {
35 return context. Set <T> (). Find (keyValues );
36}
37
38 public List <T> FindAll <T> (Expression <Func <T, bool> conditions = null) where T: class
39 {
40 if (conditions = null)
41 return context. Set <T> (). ToList ();
42 else
43 return context. Set <T> (). Where (conditions). ToList ();
44}
45
46 public PagedList <T> FindAllByPage <T, S> (Expression <Func <T, bool> conditions, Expression <Func <T, S> orderBy, int pageSize, int pageIndex) where T: class
47 {
48 var queryList = conditions = null? Context. Set <T> (): context. Set <T> (). Where (conditions) as IQueryable <T>;
49
50 return queryList. OrderByDescending (orderBy). ToPagedList (pageIndex, pageSize );
51}
52
53 public void Dispose ()
54 {
55 this. context. Dispose ();
56}
4. The data layer is based on Entity Framwork code First:
  • DBContext

    View Code

    1 public class DbContext: System. Data. Entity. DbContext
    2 {
    3 public DbContext ()
    4: base ("MyDbContext ")
    5 {
    6 this. Configuration. ProxyCreationEnabled = false;
    7}
    8
    9 public DbSet <Category> Categories {get; set ;}
    10 public DbSet <Product> Products {get; set ;}
    11}

  • Model Mapping View Code

    1 [Table ("Product")]
    2 public partial class Product
    3 {
    4 public int Id {get; set ;}
    5
    6 [StringLength (50)]
    7 [Required (ErrorMessage = "name cannot be blank")]
    8 public string Name {get; set ;}
    9
    10 public int Size {get; set ;}
    11
    12 [StringLength (300)]
    13 public string PhotoUrl {get; set ;}
    14
    15 public DateTime AddTime {get; set ;}
    16
    17 public int CategoryId {get; set ;}
    18 public virtual Category {get; set ;}
    19}

5. An example of MVC calling Server Page is provided:
  • MVC calls the Wcf client proxy request paging Data Set 1 public ActionResult Index (int pageIndex = 1)
    2 {
    3 var products = this. Service. GetProducts (PageSize, pageIndex );
    4 return View (products );
    5}
  • MVC attaches user Context information to Server 1 protected override void OnActionExecuting (ActionExecutingContext filterContext)
    2 {
    3 base. OnActionExecuting (filterContext );
    4 WCFContext. Current. Operater = new Operater () {Name = "guozili", Time = DateTime. Now, IP = Fetch. UserIp ,};
    5}
  • BLL retrieves Context information and calls data layer 1 public PagedList <Product> GetProducts (int pageSize, int pageIndex, int categoryId = 0)
    2 {
    3 // Test WCFContext
    4 var context = WCFContext. Current. Operater;
    5 return this. dao. FindAllByPage <Product, int> (p => categoryId = 0? True: p. CategoryId = categoryId, p => p. Id, pageSize, pageIndex );
    6}
  • DAL calls the Common Repository interface 1 public PagedList <T> FindAllByPage <T, S> (Expression <Func <T, bool> conditions, Expression <Func <T, s> orderBy, int pageSize, int pageIndex) where T: class
    2 {
    3 var queryList = conditions = null? Context. Set <T> (): context. Set <T> (). Where (conditions) as IQueryable <T>;
    4
    5 return queryList. OrderByDescending (orderBy). ToPagedList (pageIndex, pageSize );
    6}

 

6. Finally, download the source codeHttp://files.cnblogs.com/guozili/EasyEF.rar

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.