MsCorEE

JeffGonzalez : IScalable

Persistance Ignorance

I took the plunge into Persistance Ignorance today while working on this side project that a couple of my coworkers and I have.  I have been reading about O\R Mapping, Dependency Injection, and Domain Driven Design for the past few months, but I really haven't had a chance to implement any of the ideas I have. 

We haven't completely solved the database portion of this project yet, but I wanted to try not worrying about persistence at all.  I think you almost have to take this position if you want to concentrate on the domain.  I am also using this experience to do some TDD.  I don't quite have that down, but I am at least writing unit tests.  My goal for tonight was to rebuild my SiteMapProvider using the domain instead of Data Abstraction objects (which are very coupled to the database at this point).  I haven't quite figured out how to test ProjectSiteMapProvider class yet, so I will have to research that.

Here is some of the code (in pseudo form for brevity) I have been playing with tonight:

Really simple interface for a Repository.  I am pretty sure this won't be my final version, but it is a start.

public interface IRepository<T>
{
        T GetOne(Int32 id);
        List<T> GetAll();
}


This is the concrete implmentation of my repository.  It is located in a Stubs namespace, just to keep it seperate from my real repositories.

public class SiteMapRepository : IRepository<SiteMap>  
{
        #region IRepository<SiteMap> Members

        public SiteMap GetOne(int id)
        {
            //Some code trimmmed for room....
            SiteMap root = new SiteMap(1, "Root", null, 0, null, true, null, String.Empty);
            root.Children.Add(purchases);
            return root;
        }

        public List<SiteMap> GetAll()
        {
            List<SiteMap> list = new List<SiteMap>();
            list.Add(GetOne(1));
            return list;
        }
}


I have a SiteMapProvider class that I have created also which handles getting the SiteMap domain objects from the repository and then turning them into SiteMapNode objects.  I won't post all of the code here, but just enough to get an idea of what is happening.  I cannot for the life of me remember who wrote this ObjectFactory class, but it is dang handy for sure.  It is a wrapper for the meat of the SpringFramework implementation code that I am using.  As you can see, it is a generic class that allows me to get an instance of the repository class that is defined in xml in the web.config.  This is a pretty neat feature, I think, because it allows me to change to a production repository without a recompile.  If you wrote the ObjectFactory class, please speak up so I can give you credit.  I really like it!

Edit:  I located the author of the Factory<> class.  Scott Bellware is the man!

public override SiteMapNode BuildSiteMap()
{
            lock (_SyncRoot)
            {
                if (null != Root)
                    return Root;

 

                List<Domain.SiteMap> map = ObjectFactory<SiteMapRepository>.Create().GetAll() ;
                Domain.SiteMap rootSiteMap = null;

                if (map.Count > 0)
                {
                   
                    rootSiteMap = map[0] as Domain.SiteMap;
                    Root = CreateNodeFromDomain(map[0]);

                    foreach (Domain.SiteMap siteMap in rootSiteMap.Children)
                    {
                        SiteMapNode node = CreateNodeFromDomain(siteMap);
                        AddNode(node, CreateNodeFromDomain(siteMap.Parent));
                    }
                }

                return Root;
            }
}


This method is pretty self explanatory, but for clarity, it takes a domain sitemap and turns it into a SiteMapNode.

private SiteMapNode CreateNodeFromDomain(Domain.SiteMap siteMap)
{
    return new SiteMapNode(this, siteMap.Id.ToString(), siteMap.Uri, siteMap.Name, siteMap.Description);
}


Here is the configuration for SpringFramework taken from the web.config file.  As I mentioned up above, I can simply remove the Stubs namespace from the type attribute to have my application using a live database repository.

<spring>
        <context>
            <resource uri="config://spring/objects"/>
        </context>
        <objects xmlns="http://www.springframework.net" >
            <object
                id="SiteMapRepository"
                type="CodeMakerX.Tracking.Infrastructure.Stubs.SiteMapRepository, CodeMakerX.Tracking.Infrastructure"/>
        </objects>
</spring>


So that is my foray into Persistance Ignorance and Domain Driven Design so far.  I am really digging it.

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)