EF Learning Notes-general additions and deletions scheme

Source: Internet
Author: User

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
    1. Public Interface IEntity
    2. {
    3. long _id { get;}
    4. }

[CSharp]View Plain copy
  1. Public interface icommon<t> where T: class,ientity
  2. {
  3. T ADD (t model);
  4. T Update (t model);
  5. void Delete (T model);
  6. //Press primary key to delete, KeyValues is primary key value
  7. void Delete (params object[] keyvalues);
  8. //keyvalues is the primary key value
  9. T Find (params object[] keyvalues);
  10. List<t> FindAll ();
  11. }

2. DAL

General additions and deletions to change the code:

[CSharp]View Plain copy
  1. Public Abstract class basecommon<t>: interface.icommon<t> where T: class,interface.ientity
  2. {
  3. DbContext DB;
  4. Public Basecommon (DbContext context)
  5. {
  6. this.db = context;
  7. }
  8. Public DbContext Context
  9. {
  10. Get
  11. {
  12. return DB;
  13. }
  14. }
  15. #region icommon<t>
  16. Public T ADD (t model)
  17. {
  18. Db. Set<t> (). ADD (model);
  19. Db. SaveChanges ();
  20. return model;
  21. }
  22. Public T Update (t model)
  23. {
  24. if (db. Entry<t> (model). state = = entitystate.modified)
  25. {
  26. Db. SaveChanges ();
  27. }
  28. Else if (db. Entry<t> (model). state = = entitystate.detached)
  29. {
  30. Try
  31. {
  32. Db. Set<t> (). Attach (model);
  33. Db. Entry<t> (model). state = entitystate.modified;
  34. }
  35. catch (InvalidOperationException)
  36. {
  37. T old = Find (model._id);
  38. Db. Entry (old). Currentvalues.setvalues (model);
  39. }
  40. Db. SaveChanges ();
  41. }
  42. return model;
  43. }
  44. public void Delete (T model)
  45. {
  46. Db. Set<t> (). Remove (model);
  47. Db. SaveChanges ();
  48. }
  49. public void Delete (params object[] keyvalues)
  50. {
  51. T model = Find (keyvalues);
  52. if (model = null)
  53. {
  54. Db. Set<t> (). Remove (model);
  55. Db. SaveChanges ();
  56. }
  57. }
  58. Public T Find (params object[] keyvalues)
  59. {
  60. return DB. Set<t> ().  Find (KeyValues);
  61. }
  62. Public list<t> FindAll ()
  63. {
  64. return DB. Set<t> ().  ToList ();
  65. }
  66. #endregion
  67. }


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
    1. 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
    1. 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
  1. Public Abstract class Common<tdal, tmodel>: interface.icommon<tmodel>
  2. Where Tdal: class,new ()
  3. Where TModel: class, Interface.ientity
  4. {
  5. protected Tdal dal;
  6. private interface.icommon<tmodel> Common;
  7. Public Common ()
  8. {
  9. Dal = new Tdal ();
  10. Common = (interface.icommon<tmodel>) dal;
  11. }
  12. #region icommon<tmodel>
  13. Public TModel ADD (TModel model)
  14. {
  15. return common.  ADD (model);
  16. }
  17. Public TModel Update (TModel model)
  18. {
  19. return common.  Update (model);
  20. }
  21. public void Delete (TModel model)
  22. {
  23. Common. Delete (model);
  24. }
  25. public void Delete (params object[] keyvalues)
  26. {
  27. Common. Delete (KeyValues);
  28. }
  29. Public TModel Find (params object[] keyvalues)
  30. {
  31. return common.  Find (KeyValues);
  32. }
  33. Public list<tmodel> FindAll ()
  34. {
  35. return common.  FindAll ();
  36. }
  37. #endregion
  38. }


Similar to the DAL, the TableA object is here:

[CSharp]View Plain copy
    1. 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

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.