Here is Simple Demo for Linq to Sql dml with Mvp(Model View Presenter pattern ).This will explain you about Insert update and delete and selection of linq to sql using MVP implication.Here I have create very simple database which will contain 2 table which have one to one relation ship Here I have display diagram view of my simple database.
now as per display below image database contain 2 table
1. Pet
2. Owner
let me explain related to MVP pattern what I understand about it. mvp is nothing but just project Architecture for big length and complex project. so using this type of method you can easily manage and provided more security and easily test your code. create separate project for each layer.
let me explain how I have implicated mvp in my project
Here is list of project required to create.
1.Module (contain dbml file for database)
2.Entity Access (required to create class of all entity in our case Pet and Owner class )
3.Task Project (contain inter face for process and task class for pet and owner entity)
4. Pesenter (contain presenter interface and presenter class which will pass data from view to task or vice versa
5.Main project of view
Here is screen shot for solution
now let me explain all process step by step project from bottom to top
Create One Class Library Project add app.config file in project
create one dbml file like Pet.dbml and drag and drop both table from server explorer give relationship between 2 tables
like I have display in first diagramed. if you notice that app.config file contain connection related information.
Create one class which name given same as dbml file. now make this class as partial class of context class and overwrite connection string Here is my code for overwrite class
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Configuration;
6:
7: namespace TestModule
8: {
9: public partial class PetDataContext
10: {
11: public PetDataContext()
12: : base(ConfigurationManager.ConnectionStrings["TestModule.Properties.Settings.PetsConnectionString"].ConnectionString, mappingSource)
13: {
14: OnCreated();
15: }
16:
17: partial void OnCreated()
18: {
19: this.CommandTimeout = 3600;
20: }
21:
22: }
23: }
now from dbml designer required to remove same constructer which we have created in partial class.
1:
2: [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="Pets")]
3: public partial class PetDataContext : System.Data.Linq.DataContext
4: {
5:
6: private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
7:
8: #region Extensibility Method Definitions
9: partial void OnCreated();
10: partial void InsertPet(Pet instance);
11: partial void UpdatePet(Pet instance);
12: partial void DeletePet(Pet instance);
13: partial void InsertOwner(Owner instance);
14: partial void UpdateOwner(Owner instance);
15: partial void DeleteOwner(Owner instance);
16: #endregion
17:
18: //public PetDataContext() :
19: // base(global::TestModule.Properties.Settings.Default.PetsConnectionString, mappingSource)
20: //{
21: // OnCreated();
22: //}
23:
24: public PetDataContext(string connection) :
25: base(connection, mappingSource)
26: {
27: OnCreated();
28: }
Now Required to create Entity Project as Class Library which contain simple property of all table
Here is both class
Owner.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace Test.EntityAccess
7: {
8: public class Owner
9: {
10: public int Id { get; set; }
11: public string Name { get; set; }
12: public int Age { get; set; }
13: }
14: }
Pet.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace Test.EntityAccess
7: {
8: public class Pet
9: {
10: public int id { get; set; }
11: public string PetName { get; set; }
12: public string Species { get; set; }
13: public DateTime BirthDay { get; set; }
14: public int OwnerId { get; set; }
15: }
16: }
Now Required to create Task Project as Class Library where we need to given reference to both project Model and Entity
Now Need to add Interface first where inter face contain Method which will required to override in class
here is 2 interface so more idea you will get
ItaskOwner.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.EntityAccess;
6:
7: namespace Test.Task.InterFace
8: {
9: public interface ITaskOwner
10: {
11: Owner GetOwner(Owner Owner);
12: IList<Owner> GetListOfOwner();
13:
14: bool AddOwner(Owner owner);
15: }
16: }
Itaskpet.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.EntityAccess;
6:
7: namespace Test.Task.InterFace
8: {
9: public interface ITaskPet
10: {
11: List<Pet> GetListOfPets();
12: Pet GetCurrentPet(Pet P);
13: bool AddPet(Pet P);
14:
15: }
16: }
Now Required to create Task Class Where we need to Implementation of all method
TaskOwner.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.Task.InterFace;
6: using TestModule;
7:
8: namespace Test.Task
9: {
10: public class TaskOwner : ITaskOwner
11: {
12:
13: public EntityAccess.Owner GetOwner(EntityAccess.Owner Owner)
14: {
15: throw new NotImplementedException();
16: }
17:
18: public IList<EntityAccess.Owner> GetListOfOwner()
19: {
20: List<EntityAccess.Owner> ListofOwner;
21: using (PetDataContext db = new PetDataContext())
22: {
23: ListofOwner = (from obj in db.Owners
24: select new EntityAccess.Owner()
25: {
26: Age=Convert.ToInt32(obj.Age.Value),
27: Id=Convert.ToInt32(obj.Id),
28: Name=obj.Name
29: }).ToList();
30:
31:
32: }
33: return ListofOwner;
34: }
35:
36: public bool AddOwner(EntityAccess.Owner _owner)
37: {
38: try
39: {
40: using (PetDataContext db = new PetDataContext())
41: {
42: Owner owner = new Owner();
43: owner.Age = _owner.Age;
44: owner.Name = _owner.Name;
45: db.Owners.InsertOnSubmit(owner);
46: db.SubmitChanges();
47: }
48: return true;
49:
50: }
51: catch (Exception)
52: {
53: return false;
54: }
55: }
56: }
57: }
TaskPet.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.Task.InterFace;
6: using Test.EntityAccess;
7: using TestModule;
8:
9: namespace Test.Task
10: {
11: public class TaskPet : ITaskPet
12: {
13:
14: #region ITaskPet Members
15:
16: public List<EntityAccess.Pet> GetListOfPets()
17: {
18: List<EntityAccess.Pet> ListOfPets;
19: using (PetDataContext db = new PetDataContext())
20: {
21: ListOfPets = (from c in db.Pets
22: select new EntityAccess.Pet {
23: BirthDay = c.Birthday.HasValue ? c.Birthday.Value:new DateTime() ,
24: id=c.id,
25: OwnerId=Convert.ToInt32(c.OwnerId),
26: PetName=c.PetName,
27: Species=c.Species
28: }).ToList();
29: }
30: return ListOfPets;
31: }
32:
33: public EntityAccess.Pet GetCurrentPet(EntityAccess.Pet P)
34: {
35: EntityAccess.Pet Pet = new EntityAccess.Pet();
36: using (PetDataContext db = new PetDataContext())
37: {
38: Pet = (from obj in db.Pets
39: where obj.PetName == P.PetName
40: select new EntityAccess.Pet
41: {
42: id=obj.id,
43: BirthDay=obj.Birthday.HasValue ? obj.Birthday.Value:new DateTime(),
44: OwnerId=Convert.ToInt32(obj.OwnerId),
45: PetName=obj.PetName,
46: Species=obj.Species
47: }).FirstOrDefault();
48: }
49: return Pet;
50: }
51:
52: bool ITaskPet.AddPet(EntityAccess.Pet P)
53: {
54: try
55: {
56: using (PetDataContext db = new PetDataContext())
57: {
58: TestModule.Pet pet = new TestModule.Pet();
59: pet.Birthday = P.BirthDay;
60: pet.OwnerId = P.OwnerId;
61: pet.PetName = P.PetName;
62: pet.Species = P.Species;
63: db.Pets.InsertOnSubmit(pet);
64: db.SubmitChanges();
65: }
66: return true;
67: }
68: catch (Exception)
69: {
70: return false;
71: }
72: }
73:
74: #endregion
75: }
76: }
Now we have ready structure for communication between .net and database
now required to create pool which will pass user input to task project and get result from task project and send to aspx page.
Create Presenter project same Class Library but here just method signature is available and pass interface from one level to other level
Presenter Project Contain ViewInterFace Folder where property will define
See below result so more idea you will get
IViewOwner.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.EntityAccess;
6:
7: namespace Presenter.Test.ViewInterFace
8: {
9: public interface IViewOwner
10: {
11: IList<Owner> ListOfOwner{get;set;}
12: Owner CurrentOwner { get; set; }
13: //Owner GetCurrentOwner(Owner Owner);
14: }
15: }
IViewPet.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.EntityAccess;
6:
7: namespace Presenter.Test.ViewInterFace
8: {
9: public interface IViewPet
10: {
11: Pet Pet { get; set; }
12: IList<Pet> ListOfPet { get; set; }
13: }
14: }
now required create class which will communicated with aspx page and task project look and see how implication is done
PresenterOwner.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.Task;
6: using Test.Task.InterFace;
7: using Test.EntityAccess;
8: using Presenter.Test.ViewInterFace;
9:
10: namespace Presenter.Test
11: {
12: public class PresenterOwner
13: {
14: private readonly IViewOwner _viewOwner;
15: private readonly ITaskOwner _taskOwner;
16:
17: public PresenterOwner(IViewOwner viewOwner):this(viewOwner,new TaskOwner()){}
18:
19: public PresenterOwner(IViewOwner viewOwner, ITaskOwner taskOwner)
20: {
21: _viewOwner = viewOwner;
22: _taskOwner = taskOwner;
23: }
24:
25: public void GetLisofOwner()
26: {
27: _viewOwner.ListOfOwner = _taskOwner.GetListOfOwner();
28: }
29: public bool AddOwner(Owner Owner)
30: {
31: return _taskOwner.AddOwner(Owner);
32: }
33: }
34: }
PresenterPet.cs
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Test.Task;
6: using Test.Task.InterFace;
7: using Presenter.Test.ViewInterFace;
8: using Test.EntityAccess;
9: namespace Presenter.Test
10: {
11: public class PresenterPet
12: {
13: private readonly ITaskPet _taskpet;
14: private readonly IViewPet _viewpet;
15:
16: public PresenterPet(IViewPet viewpet) : this(viewpet, new TaskPet()) { }
17:
18: public PresenterPet(IViewPet viewpet,ITaskPet taskpet)
19: {
20: _taskpet=taskpet;
21: _viewpet = viewpet;
22: }
23:
24: public void AddPet(Pet P)
25: {
26: _taskpet.AddPet(P);
27: }
28:
29: }
30: }
Now if you notice that presenter class constructer we need to pass interface with one parameter and it will also call other constructer with 2 parameter.
Now look at fine your aspx.page project
create new website here I have create siple page and make call for select , insert,update call using mvp pattern
LinqTest.aspx
1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinqTest.aspx.cs" Inherits="DateControl.LinqTest" %>
2:
3: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4: <html xmlns="http://www.w3.org/1999/xhtml">
5: <head runat="server">
6: <title></title>
7: </head>
8: <body>
9: <form id="form1" runat="server">
10: <div>
11: <asp:TextBox ID="txtOwner" runat="server"></asp:TextBox>
12: <asp:Button ID="btnaddowner" runat="server" Text="AddOwner" OnClick="btnaddowner_Click" />
13:
14: <asp:Button ID="GetList" runat="server" Text="GetList" OnClick="GetList_Click" />
15: </div>
16: <div>
17: <table>
18: <tr>
19: <td>
20: Select Owner
21: </td>
22: <td>
23: <asp:DropDownList ID="dpOwner" runat="server">
24: </asp:DropDownList>
25: </td>
26: </tr>
27: <tr>
28: <td>
29: PetName
30: </td>
31: <td>
32: <asp:TextBox ID="txtpet" runat="server"></asp:TextBox>
33: </td>
34: </tr>
35: <tr>
36: <td colspan="2">
37: <asp:Button ID="cmdaddpet" runat="server" Text="Add Pet"
38: onclick="cmdaddpet_Click" />
39: </td>
40: </tr>
41: </table>
42: </div>
43: </form>
44: </body>
45: </html>
Cs File
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Web;
5: using System.Web.UI;
6: using System.Web.UI.WebControls;
7: using Presenter.Test;
8: using Presenter.Test.ViewInterFace;
9: using Test.EntityAccess;
10:
11: namespace DateControl
12: {
13: public partial class LinqTest : System.Web.UI.Page, IViewOwner, IViewPet
14: {
15: PresenterOwner _owner;
16: PresenterPet _pet;
17:
18: protected void Page_Load(object sender, EventArgs e)
19: {
20:
21: _owner = new PresenterOwner(this);
22: _pet = new PresenterPet(this);
23: if (!IsPostBack)
24: {
25: FillOwner();
26: }
27: // AddTestPet();
28: // List<Pet> Listofpet = GetPetList();
29:
30: // // Listofpet.ForEach(c => UpdatePet(c));
31: // var obj = (from c in Listofpet
32: // select UpdatePet(c)).Count();
33:
34: //// (from c in Listofpet select DeletePet(c)).Count();
35:
36: }
37:
38: protected void btnaddowner_Click(object sender, EventArgs e)
39: {
40: Owner own = new Owner();
41: own.Name = txtOwner.Text;
42: own.Age = 25;
43: _owner.AddOwner(own);
44: FillOwner();
45: }
46: protected void GetList_Click(object sender, EventArgs e)
47: {
48: _owner.GetLisofOwner();
49: }
50:
51: ///// <summary>
52: ///// Insert Pet Records
53: ///// </summary>
54: //private void AddTestPet()
55: //{
56: // using (PetDataContext context = new PetDataContext())
57: // {
58: // var Listofpets = (from obj in context.Pets select obj);
59: // Pet pet = new Pet();
60: // pet.Birthday = DateTime.Now;
61: // pet.PetName = "Test";
62: // pet.Species = "Test";
63:
64:
65: // context.Pets.InsertOnSubmit(pet);
66: // context.SubmitChanges();
67: // }
68: //}
69:
70: ///// <summary>
71: ///// GetList of Pet
72: ///// </summary>
73: ///// <returns></returns>
74: //protected List<Pet> GetPetList()
75: //{
76: // List<Pet> ListOfPets;
77: // using(PetDataContext db=new PetDataContext())
78: // {
79: // ListOfPets =(from c in db.Pets select c).ToList();
80: // }
81: // return ListOfPets;
82: //}
83:
84: ///// <summary>
85: ///// Update Pet Records
86: ///// </summary>
87: ///// <param name="Pet"></param>
88: ///// <returns></returns>
89: //protected bool UpdatePet(Pet Pet)
90: //{
91: // using (PetDataContext db=new PetDataContext())
92: // {
93: // //var pets=(from c in db.Pets
94: // // where c.PetName == Pet.PetName select c ).FirstOrDefault();
95:
96: // Pet.PetName = "Test Update";
97:
98: // db.Pets.Attach(Pet);
99: // db.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, Pet);
100: // db.SubmitChanges();
101: // }
102: // return true;
103:
104:
105: //}
106:
107: ///// <summary>
108: ///// Delete Pet Records
109: ///// </summary>
110: ///// <param name="_Pet"></param>
111: ///// <returns></returns>
112: //protected bool DeletePet(Pet _Pet)
113: //{
114: // using (PetDataContext db = new PetDataContext())
115: // {
116: // var pet=(from c in db.Pets
117: // where c.PetName==_Pet.PetName select c ).FirstOrDefault();
118:
119: // db.Pets.DeleteOnSubmit(pet);
120: // db.SubmitChanges();
121: // }
122: // return true;
123: //}
124:
125:
126:
127:
128: #region IViewOwner Members
129:
130: public IList<Owner> ListOfOwner
131: {
132: get;
133: set;
134: }
135:
136: public Owner CurrentOwner
137: {
138: get;
139: set;
140: }
141:
142: #endregion
143:
144: protected void cmdaddpet_Click(object sender, EventArgs e)
145: {
146:
147: Pet P = new Pet();
148: P.PetName = txtpet.Text;
149: P.OwnerId = Convert.ToInt32(dpOwner.SelectedValue);
150: P.BirthDay = new DateTime(1970,5,23);
151: _pet.AddPet(P);
152: }
153:
154: private void FillOwner()
155: {
156: _owner.GetLisofOwner();
157: dpOwner.DataSource = ListOfOwner;
158: dpOwner.DataTextField = "Name";
159: dpOwner.DataValueField = "Id";
160: dpOwner.DataBind();
161: }
162:
163:
164:
165: #region IViewPet Members
166:
167: public Pet Pet
168: {
169: get;
170: set;
171:
172: }
173:
174: public IList<Pet> ListOfPet
175: {
176: get;
177: set;
178: }
179:
180: #endregion
181: }
182: }
Here we need to inherit presenter interface so we get all property here and we can pass interface from presenter object
so presenter will call task and task communicate with model access sql and fill entity and pass to presenter and from aspx page we get result. nice structure for complex project