First short Introduction/Help for Using

DataManager

public static class DataManager
{
    #region Initialize

    public static void Initialize()
    {
        Initializer.Initializer.SetInitializer();

        ObjectFactory.Configure(
           x =>
           {
               x.For<DbContext>().Use<MyContext>();
               x.For<IUnitOfWorkFactory>().Use<DbContextUnitOfWorkFactory>();
               x.Scan(y => y.Assembly("MyAssembly"));
           }
        );

        DbContextUnitOfWorkFactory.SetDbContext(CreateContext);
    }

    private static DbContext CreateContext()
    {
        DbContext context = new MyContext();

        return context;
    }

    #endregion

    #region Repositories

    private static PeopleRepository _peopleRepository;
    private static ProductRepository _productRepository;

    public static PeopleRepository Peoples
    {
        get
        {
            if (DataManager._peopleRepository == null)
            {
                DataManager._peopleRepository = ObjectFactory.GetInstance<PeopleRepository>();
            }

            return DataManager._peopleRepository;
        }
    }    
    
    public static ProductRepository Products
    {
        get
        {
            if (DataManager._productRepository == null)
            {
                DataManager._productRepository = ObjectFactory.GetInstance<ProductRepository>();
            }

            return DataManager._productRepository;
        }
    }

    #endregion
}

PeopleRepository

public class PeopleRepository : DbContextRepository<People>
{
    #region Constructor

    public PeopleRepository()
        : base()
    {

    }

    #endregion
}

ProductRepository

public class ProductRepository : DbContextRepository<Product>
{
    #region Constructor

    public ProductRepository()
        : base()
    {
        this.Created += ProductRepository_Created;
        this.Filter += ProductRepository_Filter;
    }

    #endregion

    #region Events

    private void ProductRepository_Created(object sender, ActionEventArgs<Product> e)
    {
        if (e.Entity != null)
        {
            e.Entity.CreationDateTime = DateTime.Now;
        }
    }

    private void ProductRepository_Filter(object sender, FilterEventArgs<Product> e)
    {
        if (e.Entity.GrossPrice < 0)
        {
            e.Result = false;
        }
    }

    #endregion
}

Program

class Program
{
    static void Main(string[] args)
    {
        IEnumerable<Product> products = DataManager.Products.Get();
    }
}

Last edited Oct 25, 2012 at 8:55 PM by Asesjixx, version 2

Comments

No comments yet.