Click here to Skip to main content
12,076,925 members (47,689 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads

Tagged as

Stats

16.1K views
164 downloads
9 bookmarked
Posted

TDD Approach to ASP.NET WebForm Development

, 10 Feb 2010 CPOL
An MVC pattern for WebForm development
MVCWebForm.zip
MVCWebFormPrototype
Lib
nunit.framework.dll
MVCWebForm
bin
Debug
MVCWebForm.dll
MVCWebForm.pdb
obj
Debug
MVCWebForm.dll
MVCWebForm.pdb
Refactor
MVCWebForm.dll
TempPE
Properties
MVCWebFormPrototype.4.5.resharper.user
UnitTest
bin
Debug
MVCWebForm.dll
MVCWebForm.pdb
nunit.framework.dll
UnitTest.dll
UnitTest.pdb
Controllers
DataSources
Model
obj
Debug
Refactor
ResolveAssemblyReference.cache
TempPE
UnitTest.dll
UnitTest.pdb
Properties
Web
App_Code
Controllers
DataSources
Model
App_Data
Bin
MVCWebForm.dll
MVCWebForm.pdb
_ReSharper.MVCWebFormPrototype
CachesImage.bin
ProjectModel
ProjectModel.dat
TodoCache
.version
9
13d15da3.dat
WebsiteFileReferences
.version
WordIndex.New
.version
3
2e9c86c8.dat
Xaml
CacheProvider.dat
using System;
using System.Collections.Generic;

namespace MVCWebForm
{
    public abstract class ControllerBase: IController
    {
        private Dictionary<Type, DataSourceBase> _container
            = new Dictionary<Type, DataSourceBase>();

        #region IController Members

        public void AddDataSource(DataSourceBase dataSource)
        {
            Type entityType = GetEntityType(dataSource.GetType().BaseType);

            if (_container.ContainsKey(entityType))
                throw new ArgumentException(string.Format("DataSource already exists in the controller: {0}.", dataSource.GetType().BaseType.ToString()));

            _container[entityType] = dataSource;  
        }

        public void RemoveDataSource(DataSourceBase dataSource)
        {
            Type entityType = GetEntityType(dataSource.GetType().BaseType);

            if (_container.ContainsKey(entityType))
                _container.Remove(dataSource.GetType());
        }

        #endregion

        public TEntity Create<TEntity>() where TEntity: PresentationEntityBase
        {
            return GetDataSource<TEntity>().Create<TEntity>();
        }

        public TEntity Update<TEntity>(TEntity item) where TEntity : PresentationEntityBase
        {
            return GetDataSource<TEntity>().Update<TEntity>(item);
        }

        public void Delete<TEntity>(TEntity item) where TEntity : PresentationEntityBase
        {
            GetDataSource<TEntity>().Delete<TEntity>(item);
        }

        public IList<TEntity> FindAll<TEntity>() where TEntity : PresentationEntityBase
        {
            return GetDataSource<TEntity>().FindAll<TEntity>();
        }

        public IList<TEntity> FindBy<TEntity>(ICriteria criteria) where TEntity : PresentationEntityBase
        {
            return GetDataSource<TEntity>().FindBy<TEntity>(criteria);
        }

        private DataSourceBase GetDataSource<TEntity>() where TEntity : PresentationEntityBase
        {
            if (!_container.ContainsKey(typeof(TEntity)))
                throw new ArgumentException(string.Format("DataSource doesn't exists in the controller for the type: {0}.", typeof(TEntity).ToString()));

            return _container[typeof(TEntity)];
        }

        private Type GetEntityType(Type genericType)
        {
            if (!genericType.IsGenericType)
                throw new ArgumentException(string.Format("The type {0} is not generic.", genericType.ToString()));

            Type[] typeParameters = genericType.GetGenericArguments();
            if (typeParameters.Length == 0)
                throw new ArgumentException(string.Format("The type {0} doesn't contain generic argument.", genericType.ToString()));

            return typeParameters[0];
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Max Civino
Software Developer (Senior) SnowValley Ltd
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160212.1 | Last Updated 10 Feb 2010
Article Copyright 2010 by Max Civino
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid