Okay, so I made a new Save method on my repository like so:
public interface ISchoolRepository
{
void Save(School school); //School of my model not the db school object
School GetSchoolById(int schoolId);
IQueryable<School> GetAll();
IQueryable<Student> GetEnrolledStudentsBy(int schoolId);
}
So then I implemented the new Save method like this:
public void Save(Core.Model.School school){
using (var db = new LearningDataContext())
{
var currentSchool = SchoolMap.GetDbSchoolFrom(school);
var originalSchool = dc.Schools.SingleOrDefault(x => x.SchoolId == school.Id);
if (originalSchool == null)
db.Schools.InsertOnSubmit(currentSchool);
else
db.Schools.Attach(currentSchool, originalSchool);
db.SubmitChanges();
}
}
You should notice two things about the above method. One is that I have a static method called GetDbSchoolFrom(Core.Model.School school), which maps my model school to the db school. The other being that I’m using two datacontexts. One of them is defined on the class, dc, like in this post and the other is local to the method, db. So I still have my db/linq to sql objects as internal, which was one of my main goals and I feel pretty comfortable with this setup and maintainability.
Here is the GetDbSchoolFrom method:On another project, I went ahead and made a lot of DB changes so I could really test my model dependency on the db schema. I was happy to say that I only had to touch my mapper classes. Everything else remained the same.internal static DataAccess.Impl.School GetDbSchoolFrom(School school)
{
var s = new DataAccess.Impl.School();
s.ContactNumber = school.ContactNumber;
s.FullName = school.FullName;
s.SchoolId = school.Id;
s.ShortName = school.ShortName;
s.CountyId = school.County.Id;
return s;
}
As always, please make any comments you feel necessary and definitely let me know if you find any holes or issues with my code. Thanks!
Thank you Jimmy Bogard for telling me about the Windows Live Writer from Visual Studio plugin.
I have a comment. This is impressive, it's just too bad you'll never nut up and start your own thing. What happened to that business you wanted? That idea that was gonna make you money? When working for someone else there is ALWAYS a success ceiling. You're too smart for that. I've been working at being a rock star for over 5 annoying/stressful years. But damnit I'm gonna be one. Don't say you're gonna do it one day, start on it tonight!
ReplyDeleteThank you Adam for the motivation. I do appreciate it and I still intend to beat you to that million dollar marker. I have plans, you just worry about being a rock star!
ReplyDeleteFor those of you that have never heard of Black Magnolia, you should definitely check them out: http://www.myspace.com/blackmagnoliaband
They're awesome.
Hey, is it your own Entity Framework under development thing? I like the idea of separating the class design from the database table design. I always wanted to do something like that. I'm only half way though reading - hope I can contribute after I'm done.
ReplyDeleteBTW, we are working on a web app that would be deployed outside on DMZ servers and this is the FIRST time we are providing services to the general public. They are super cautious about this, adding a middle layer of a web sites that hosts only WCF services to relay the task of database and backend transactions. It sounds an overkill but when it comes to security, nothing is overkill :) I've got to dig in the code to find out how they are doing.
Like your post - Reminded me of my good old days :)
H