Class and XML file. Very strange problem.

S

shapper

Hello,

I have a repository as follows:

public class BrandRepository : IBrandRepository {

public String Path { get; set; }
private XDocument context;
public BrandRepository() { } // BrandRepository

public BrandRepository(String path) {
this.Path = path;
this.context = XDocument.Load(this.Path);
} // BrandRepository

public Brand GetBrand(Guid id) {

Brand brand = (from b in context.Root.Elements("Brand")
where b.Element("BrandId").Value == id.ToString()
select new Brand {
Id = new Guid(b.Element("BrandId").Value),
Created = DateTime.Parse(b.Element
("Created").Value),
Name = b.Element("Name").Value,
Updated = DateTime.Parse(b.Element
("Updated").Value)
}).SingleOrDefault();
return brand;

} // GetBrand

}

And is used by ProductBinder which is fired when submitting a Create
Product form:

public class ProductBinder : IModelBinder {

private BrandRepository brandRepository = new BrandRepository
(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data/
Brands.xml"));

public object BindModel(ControllerContext controller,
ModelBindingContext binding) {

Brand b = brandRepository.GetBrand(new Guid((String)
binding.ValueProvider["Brand"].ConvertTo(typeof(String)))),
...
}

On debugging I did the following:

Created 3 Brands using the Brands form. No problem there.
Then I went to the Product form and I selected a Brand. I submitted
the form.

In ProductBinder Here the Brand b is null.
I check the Brands.xml file and the 3 Brands I created are there!
I check the GetBrand method and the Id received is correct but it
returns a null Brand.

Then I stop debugging it and restart it again.
Now I don't have any error!

I am completely lost.

It might to have something to do with the XML loading?

Does anyone has any idea why this happens?

Thanks,
Miguel
 
S

shapper

Hello,

I have a repository as follows:

  public class BrandRepository : IBrandRepository {

    public String Path { get; set; }
    private XDocument context;
    public BrandRepository() { } // BrandRepository

    public BrandRepository(String path) {
      this.Path = path;
      this.context = XDocument.Load(this.Path);
    } // BrandRepository

    public Brand GetBrand(Guid id) {

      Brand brand = (from b in context.Root.Elements("Brand")
                     where b.Element("BrandId").Value == id.ToString()
                     select new Brand {
                       Id = new Guid(b.Element("BrandId").Value),
                       Created = DateTime.Parse(b.Element
("Created").Value),
                       Name = b.Element("Name")..Value,
                       Updated = DateTime.Parse(b.Element
("Updated").Value)
                     }).SingleOrDefault();
      return brand;

    } // GetBrand

}

And is used by ProductBinder which is fired when submitting a Create
Product form:

  public class ProductBinder : IModelBinder {

    private BrandRepository brandRepository = new BrandRepository
(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data/
Brands.xml"));

    public object BindModel(ControllerContext controller,
ModelBindingContext binding) {

    Brand b = brandRepository.GetBrand(new Guid((String)
binding.ValueProvider["Brand"].ConvertTo(typeof(String)))),
  ...

}

On debugging I did the following:

Created 3 Brands using the Brands form. No problem there.
Then I went to the Product form and I selected a Brand. I submitted
the form.

In ProductBinder Here the Brand b is null.
I check the Brands.xml file and the 3 Brands I created are there!
I check the GetBrand method and the Id received is correct but it
returns a null Brand.

Then I stop debugging it and restart it again.
Now I don't have any error!

I am completely lost.

It might to have something to do with the XML loading?

Does anyone has any idea why this happens?

Thanks,
Miguel

I think I found the problem. I should use:

// ProductBinder
public class ProductBinder : IModelBinder {
public object BindModel(ControllerContext controller,
ModelBindingContext binding) {
BrandRepository brandRepository = new BrandRepository
(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data/
Brands.xml"));
...

Basically move the brandRepository from outside of the BindModel to
inside.
Not sure why ..

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top