CRM framework trivia and additions and deletions to the logic code

Source: Internet
Author: User

CRM: Customer Relationship Management System

The overall framework MVC4 + EF5 + AUTOFAC replaces the relationship between the various layers of the factory layer connection

Basic framework

01 Entity layer 02 Warehousing layer 03 Business Layer 04 Common layer 05UI MVC framework
------------------------------------------
01 Entity Layer EF

01 in the physical layer for easy model annotation

Generate some classes of EF entity classes and labeled classes using the T4 template, respectively

Some classes of EF entity classes

Using System.ComponentModel;
Using System.ComponentModel.DataAnnotations;
Using Crm.model.ModelView;
[Metadatatype (typeof (Sysfunctionview))]
public partial class Sysfunction
{

}

Labeling Classes

Using System.ComponentModel;
Using System.ComponentModel.DataAnnotations;
public partial class Sysmenusview
{
public int MID {get; set;}
public int Mparentid {get; set;}
[DisplayName ("menu name"), Required (errormessage = "menu name is not empty")]
public string Mname {get; set;}
[DisplayName ("Address url"), Required (errormessage = "Address url not empty")]
public string Murl {get; set;}
[DisplayName ("area"), Required (errormessage = "Region non-empty")]
public string MArea {get; set;}
[DisplayName ("Controller"), Required (errormessage = "Controller non-null")]
public string Mcontroller {get; set;}
[DisplayName ("Method"), Required (errormessage = "method NOT NULL")]
public string Maction {get; set;}
[DisplayName ("Sort number"), Required (errormessage = "Sort number not empty")]
public int Msortid {get; set;}
[DisplayName ("status")]
public int Mstatus {get; set;}
[DisplayName ("menu icon"), Required (errormessage = "menu icon not empty")]
public string Mpicname {get; set;}
public int Mlevel {get; set;}
public string MExp1 {get; set;}
Public nullable<int> MEXP2 {get; set;}
public int Mcreatorid {get; set;}
Public System.DateTime mcreatetime {get; set;}
Public nullable<int> Mupdateid {get; set;}
Public System.DateTime mupdatetime {get; set;}
}

Note: When referencing the T4 template, first modify this here.

Const string inputfile = @ ". \CRM.MODEL\JKCRM.EDMX ";

For a better understanding of the use of T4 templates inside generate space naming, class names, fields, properties,

Know how to modify the T4 template to generate classes according to your needs


-----------------------------

02 Storage Tier Two class library DAL and Interface Idal

In the Dal class library:

Generic basedal<tentity> classes and Basedbcontext classes

There are also a series of ' TEntity ' DAL classes generated through the T4 template

Inherit basedal<tentity> and implement Interface I ' TEntity ' DAL

public partial class ' TEntity ' dal:basedal<tentity>,i ' TEntity ' DAL


Basedbcontext class inherits DbContext custom EF Container

Using System.Data.Entity;

public class Basedbcontext:dbcontext
{
Public Basedbcontext ()
: Base ("Name=jkcrmentities")
{

}
}

First there is a generic basedal to implement Interface Idal--"

public class basedal<tentity>:ibasedal<tentity> where Tentity:class

Basedal in writing, deleting, changing, and checking logic code

Defines a dbset<t> private variable that is initialized in the constructor function.

Dbset<tentity> _dbset;

Public Basedal ()
{
_dbset = db. Set<tentity> ();
}

--------increase, delete, change, check logic code

#region 3.0 Query related methods

#region 3.0.1 with Conditional Query method
<summary>
Conditional Query method
</summary>
<param name= "where" ></param>
<returns></returns>
Public iqueryable<tentity> Querywhere (expression<func<tentity, bool>> where)
{
Return _dbset. where (where);
}
#endregion

#region 3.0.2 even table query

Public iqueryable<tentity> Queryjoin (expression<func<tentity, bool>> where, string[] tableNames)
{
1.0 parameter Legality check
if (Tablenames = = NULL | | Tablenames.any () = = False)
{
throw new Exception ("The list method must have at least one table name");
}

2.0 Definition Dbquery<tentity>
dbquery<tentity> query = _dbset;

3.0 Traversal
foreach (var item in Tablenames)
{
query = query. Include (item);
}

4.0 on-band conditional query
return query. where (where);
}

#endregion

#region 3.0.3 Paged Query

Public iqueryable<tentity> querybypage<tkey> (int pageindex
, int pagesize
, out int TotalCount
, Expression<func<tentity, bool>> where
, expression<func<tentity, tkey>> order)
{
1.0 calculating the total number of skipped rows
int skipcount = (pageindex-1) * pagesize;

2.0 calculating the total number of rows
TotalCount = _dbset. Count (where);

3.0 return
Return _dbset. where (where). OrderByDescending (Order). Skip (Skipcount). Take (pagesize);
}

#endregion

#region 3.0.4 A unified approach to executing SQL statements

<summary>
Use EF to execute SQL statements or stored procedures directly
Note If you are performing a stored procedure, you do not need to exec just pass in the name of the stored procedure parameter 1, Parameter 2 ...
Format: usp_getlist
</summary>
<typeparam name= "TElement" ></typeparam>
<param name= "SQL" ></param>
<param name= "PS" ></param>
<returns></returns>
Public list<telement> runsql<telement> (String sql, params object[] PS)
{
Return DB. database.sqlquery<telement> (SQL, PS). ToList ();
}

#endregion

#endregion

#region 4.0 New

public void Add (TEntity model)
{
1.0 Validation of parameter legitimacy
if (model = = NULL)
{
throw new Exception ("entity non-null");
}

2.0 Append to the EF container and modify the state to added
_dbset. ADD (model);
}

#endregion

#region 5.0 on-demand editing (new entity, manually modifying its state to unchanged and then modifying the property's ismodified to True)

public void Edit (TEntity model, string[] properts)
{
1.0 Validation of parameter legitimacy
if (model = = NULL)
{
throw new Exception ("entity non-null");
}
if (Properts = = NULL | | properts. any () = = False)
{
throw new Exception ("The property to be modified must have at least one");
}

2.0 appending the model to the EF container
var entry = db. Entry (model);
Entry. state = System.Data.EntityState.Unchanged;

3.0 Traversal
foreach (var item in Properts)
{
Entry. Property (item). IsModified = true;
}

4.0 Turn off EF entity Property validation
Db. configuration.validateonsaveenabled = false;
}

#endregion

#region 6.0 Physical Deletions

public void Delete (TEntity model, BOOL Isaddedcontext)
{
1.0 Validation of parameter legitimacy
if (model = = NULL)
{
throw new Exception ("entity non-null");
}

2.0 Append if not appended
if (Isaddedcontext = = False)
{
_dbset. Attach (model);
}

3.0 modifying the state to deleted
_dbset. Remove (model);
}

#endregion

#region 7.0 Unified Save
public int SaveChanges ()
{
Return DB. SaveChanges ();
}
#endregion

------

Idal class Library: interface Ibasedal and some of the interface I ' Tentity ' DAL generated through the T4 template

This series of interfaces inherits the interface Ibasedal

Public partial interface I ' Tentity ' dal:ibasedal<tentity>
---------------------------------------------------------------
The relationship between the whole dal into

1, the ' TEntity ' DAL generated by the T4 template inherits the generic class basedal<tentity>

and implement the I ' TEntity ' DAL interface generated through the T4 template

2, the I ' TEntity ' DAL interface generated by the T4 template inherits the Ibasedal<tentity> interface

3,basedal<tentity> Implementing the Ibasedal<tentity> interface

' TEntity ' dal:basedal<tentity>,i ' TEntity ' DAL

I ' Tentity ' dal:ibasedal<tentity>

basedal<tentity>:ibasedal<tentity> where Tentity:class
--------------------------------------------------------------

03 Business Layer

Two class libraries BLL and IBLL

The class library BLL contains basebll<tentity> generic classes and some classes generated through T4 templates ' TEntity ' BLL

Relationship: 1, basebll<tentity> generic class inheritance interface ibasebll<tentity>

public class basebll<tentity>: ibasebll<tentity> where Tentity:class

2, some classes generated by the T4 template inherit the basebll<tentity> generic class

and implement some interfaces generated through the T4 template I ' TEntity ' BLL

public partial class ' TEntity ' bll:basebll<tentity>,i ' TEntity ' BLL

In the basebll<tentity>:

public class basebll<tentity>: ibasebll<tentity> where Tentity:class
{
protected ibasedal<tentity> Basedal;
#region 3.0 Query related methods

#region 3.0.1 with Conditional Query method
<summary>
Conditional Query method
</summary>
<param name= "where" ></param>
<returns></returns>
Public iqueryable<tentity> Querywhere (expression<func<tentity, bool>> where)
{
Return Basedal. Querywhere (where);
}
#endregion

etc....


Directly T4 template generation in some class ' TEntity ' BLL constructors generated by the T4 template

public partial class SYSKEYVALUEBLL:BASEBLL<SYSKEYVALUE>,ISYSKEYVALUEBLL
{
Isyskeyvaluedal dal;
Public syskeyvaluebll (Isyskeyvaluedal dal)
{
This.dal = dal;
Base.basedal = dal;
}

In the IBLL class library

Contains interface Ibasebll and some of the interfaces generated by the T4 template I ' TEntity ' BLL

public partial Interface Isyskeyvaluebll:ibasebll<syskeyvalue>

CRM framework trivia and additions and deletions to the logic code

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.