Last post Jul 26, 2015 12:58 PM by asimr
Jul 25, 2015 07:31 PM|asimr|LINK
So I have been working on this project for around 3 weeks which is a CMS written in C# using Entity Framework. I just need guidance on a few issues I have come across.
So the first issue is entity validation, I got two classes Menus and MenuItems (MenuItems are all the links, which would link to a menu), problem I have is that when you create a new menu you can also specify the MenuItems as well
Menu ServicesMenu = new Menu
MenuName = "ServicesMenu",
MenuStyle = "Drop",
MenuTemplate = "_Drop.chstml",
DateCreated = DateTime.Now,
MenuItems = new List<MenuItem>()
MenuItemTitle = "Web Development",
MenuItemTarget = "_blank" ,
MenuItemType = "Parent",
MenuItemLink = null,
MenuItemSortOrder = 10,
MenuItemToolTip = "Browse Componenets",
DateCreated = DateTime.Now
The issue I have with this is that when you add and commit this, the validation for MenuItems will be simply skipped. MenuItems does have it's own method called Add, it validates the data before inserting so for example the MenuItemLink
cannot be left out, however on the above code it's null and that content can still be added to database.
What's the best approach to validate this? I mean can I validate the MenuItems separately?
The other issue I have is that we have a lot of classes (DAL) which contain methods to add data. This means I always wrap my context in using statement, is this a ideal? The reason I am asking this is because it will close and open
connections multiple times. For example code like this:
using (var db = new DatabaseModel())
Folder folder = Retrieve(FolderId);
if (folder.ParentFolderId != 0)
The Retrieve(FolderId) will open another connection since it's got a using statement in its method too.
Some advice and guidance on these points will be greatly appreciated.
Jul 26, 2015 12:58 PM|asimr|LINK
I seem to have found a nice source which explains entity validation.