开发者

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

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 mechan开发者_运维技巧ism 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


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();
}


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();


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.


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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜