http://blog.csdn.net/leftfist/article/details/25005307
I've just been in contact with EF for a long time, and I don't know what the strength is, but it looks like EF provides a general addition and deletion change function. In the past, some ORM methods have been used, although there are code generators, but the amount of code is generally more. This time using EF, I think, can I try to write some common methods, add tables later, entity class, just do a few changes, you can have basic additions and deletions to check the function?
The scheme is as follows:
1, divided into several parts: BLL, DAL, Model, Interface. Where the code generated by EF is placed in model. Because it does not want to operate the model directly from the BLL, it also adds a DAL, which is further encapsulated in the EF method for the BLL invocation. As for the interface, it is due to the following considerations: additions and deletions, both the BLL and the DAL need to be implemented, so they better implement the same interface; Most importantly, because the interface is used, it is convenient to invoke the Dal object in the BLL.
These sections are described as follows:
1, Interface interface
[CSharp]View Plain copy
- Public Interface IEntity
- {
- long _id { get;}
- }
[CSharp]View Plain copy
- Public interface icommon<t> where T: class,ientity
- {
- T ADD (t model);
- T Update (t model);
- void Delete (T model);
- //Press primary key to delete, KeyValues is primary key value
- void Delete (params object[] keyvalues);
- //keyvalues is the primary key value
- T Find (params object[] keyvalues);
- List<t> FindAll ();
- }
2. DAL
General additions and deletions to change the code:
[CSharp]View Plain copy
- Public Abstract class basecommon<t>: interface.icommon<t> where T: class,interface.ientity
- {
- DbContext DB;
- Public Basecommon (DbContext context)
- {
- this.db = context;
- }
- Public DbContext Context
- {
- Get
- {
- return DB;
- }
- }
- #region icommon<t>
- Public T ADD (t model)
- {
- Db. Set<t> (). ADD (model);
- Db. SaveChanges ();
- return model;
- }
- Public T Update (t model)
- {
- if (db. Entry<t> (model). state = = entitystate.modified)
- {
- Db. SaveChanges ();
- }
- Else if (db. Entry<t> (model). state = = entitystate.detached)
- {
- Try
- {
- Db. Set<t> (). Attach (model);
- Db. Entry<t> (model). state = entitystate.modified;
- }
- catch (InvalidOperationException)
- {
- T old = Find (model._id);
- Db. Entry (old). Currentvalues.setvalues (model);
- }
- Db. SaveChanges ();
- }
- return model;
- }
- public void Delete (T model)
- {
- Db. Set<t> (). Remove (model);
- Db. SaveChanges ();
- }
- public void Delete (params object[] keyvalues)
- {
- T model = Find (keyvalues);
- if (model = null)
- {
- Db. Set<t> (). Remove (model);
- Db. SaveChanges ();
- }
- }
- Public T Find (params object[] keyvalues)
- {
- return DB. Set<t> (). Find (KeyValues);
- }
- Public list<t> FindAll ()
- {
- return DB. Set<t> (). ToList ();
- }
- #endregion
- }
In this, has been encapsulated the general method of adding and deleting. and the corresponding database of each table object of those classes, just inherit this Basecommon class, you can have additions and deletions to change the function. Such as:
[CSharp]View Plain copy
- Public partial class Tablea:basecommon<model.tablea> {}
In this way, the TableA object will naturally have the ability to change and delete. If we need to expand its functionality, we can write another TableA partial class.
Later, add a table TableB, in the Dal here, generally only a new sentence:
[CSharp]View Plain copy
- Public partial class Tableb:basecommon<model.tableb> {}
Isn't it convenient?
3. BLL
The BLL is for the UI layer, or the previous layer is called, so it should also have the basic function of the pruning check for each object. Of course, the BLL does not need to be directly implemented by invoking the DAL:
[CSharp]View Plain copy
- Public Abstract class Common<tdal, tmodel>: interface.icommon<tmodel>
- Where Tdal: class,new ()
- Where TModel: class, Interface.ientity
- {
- protected Tdal dal;
- private interface.icommon<tmodel> Common;
- Public Common ()
- {
- Dal = new Tdal ();
- Common = (interface.icommon<tmodel>) dal;
- }
- #region icommon<tmodel>
- Public TModel ADD (TModel model)
- {
- return common. ADD (model);
- }
- Public TModel Update (TModel model)
- {
- return common. Update (model);
- }
- public void Delete (TModel model)
- {
- Common. Delete (model);
- }
- public void Delete (params object[] keyvalues)
- {
- Common. Delete (KeyValues);
- }
- Public TModel Find (params object[] keyvalues)
- {
- return common. Find (KeyValues);
- }
- Public list<tmodel> FindAll ()
- {
- return common. FindAll ();
- }
- #endregion
- }
Similar to the DAL, the TableA object is here:
[CSharp]View Plain copy
- Public partial class Tablea:common<dal. TableA, model.tablea> {}
Similarly, if later added the table Talbeb, here also only adds a sentence.
Another advantage here is that for external calls, there is no need to know generics, which is a TableA table = new Talbea ();
EF Learning Notes-general additions and deletions scheme