开发者

MVC3 Basic C# programming question - Dynamic CRUD XML app - Issue

开发者 https://www.devze.com 2023-03-30 09:13 出处:网络
I\'ve created an app with CRUD functions on XML documents with repository pattern. I have 4 models (4 xml files) with each a repository class.

I've created an app with CRUD functions on XML documents with repository pattern. I have 4 models (4 xml files) with each a repository class. Before it was just 4 xml documents that were read into a XDocument object in the constructor.

 itemData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/item1.xml"));

Now I would like to make the xml file dynamic, so it can read unlimited xmls

So whats the best approach? Making a second constructor and passing in a parameter from the url? Something like this:

        public ItemRepository()
            {
            }

            public ItemRepository(string xml)
            {
             itemData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/" + xml + ".xml"));
                 ....
            }

Any other suggestions? Cos i get NullReferenceException with 开发者_JS百科the Model with this.


I do not see anything bad with you approach except that the repository might accept directly the complete path to get its xml file. It would be just a bit cleaner way of writting it.

AS your NullReferenceException, only a thorough debug might help you. If your first statement was working , I do not see why the next one shouldn't, at least from the bits of code you have written here.

Good luck to you,


Easiest workaround is to just out the code from the constructor into all the CRUD methods. Here a link for a little more info (and more issues :P) NullReferenceException while using XElement

0

精彩评论

暂无评论...
验证码 换一张
取 消