views:

116

answers:

5

Repository pattern - Switch out the database and switch in XML files.

Hello

I have an asp.net MVC 2.0 project and I have followed the Repository pattern.

Periodically, I am losing access to the database server so I want to have another mechanism in place (XML files) to continue developing. It is not possible to have a local version of the db unfortunately!

I thought this would be relatively easy using the Repository pattern, to switch out the db repositories and switch in XML versions. However, I am having real trouble coming up with a solution.

I have tried LinqToXML but then ran into problems trying to return a List of News items as the LinqToXML ToList returns Generic.List

Should I be mapping the XElement list over to the News list by hand? It just seems a bit clunky compared to LinqToSQL attributes on the News class and then simply doing a Table.....ToList();

Any direction would be appreciated.

Thanks

A: 

Just parse using Linq-To-XML. Something like:

(from a in b
where b.Element("type").Value == "test"
select new c(){d=b.Element("prop").Value}).ToList();
Oskar Kjellin
This does work - but it means I would need to map all of the c properties to the b properties. This is something you don't need to do with LinqToSQL as the c class already has the Linq.Mapping.ColumnAttribute on the properties.I was wondering if LinqToXML had something as nifty or could actually use the Linq attributes already on the class.
glasto red
+1  A: 

You should have something like

public interface IRepo
{
      //methods for data
}
public class DbRepo : IRepo
{
      // IRepo methods for db access
}
public class XmlRepo : IRepo
{
      // IRepo methods for xml access
}

and to rely on interfaces. Then

IRepo repo;
try
{
     repo = new DbRepo();  //this should throw if there is no db connectivity
}
catch 
{
     repo = new XmlRepo();
}
Svetlozar Angelov
A: 

You may be doing some of this already...

I suggest you create an IFooRepository interface. From that you can have a FooRepository (housing your linq to SQL data access) and a FooMockRepository (where you have your linq to XML mocked data access). If you follow POCO (something linq to SQL is not yet great at) then you might use automapper to map your L2S objects to your POCO objects. This then gives you the seperation you need to be able to also use automapper in your FooMockRepository to return POCO objects (via auto mapper) from your linq to XML queries. All that is left is to use StructureMap (or other IoC) to pick and choose the appropriate concrete type for you with syntax like...

IFooRepository fooRepository = ObjectFactory.GetInstance();

Then in your environment configuration scripts you can configure your IoC to get the appropriate repository for dev vs. A more prod like environment.

Andrew Siemer
A: 

Should I be mapping the XElement list over to the News list by hand?

Exactly. The repositories should return a List, not Linq to SQL entities or XElements

Mel Gerats
Thanks Mel - the repositories do return a List. My question is how to create that list. See comment to Oskar below.
glasto red
A: 

Since this is for just your continued development do you really need persistance? Or could you use an in memory repository to accomplish your needs?

public interface IFoo
{
    IList<News> GetNews();
}

public class InProcFoo : IFoo
{

   private static IList<News> news;

   public InProcFoo()
   {
       news = new List<News>();
       news.Add(new News());
   }

   public IList<News> GetNews()
   {
      return news;
   }
}

If you need persistence maybe you could simply use the data contract serializer to get you what you need

public class DataContractFoo :IFoo, IDisposable
{
       private static IList<News> news;
  public DataContractFoo()
  {
    //Load up news from a serialized news list from some xml file.

  }
       public IList<News> GetNews()
       {
          return news;
       }
   public void Dispose()
   {
      //Serialize news via the data contract serializer to your drive.

   }  
}

I dont have a compiler here so this is taken as is, but hopefully can help you continue developing and not having to worry about Linq to Xml

Mark