Friday, May 13, 2016

Web Api For Creating,Updating,Deleting the Active Directory groups and Users


At this point of time when i am writing the article i only know that i have create,update,delete users in the active directory.
But i know that requirements would change and i would end up writing and modifying many things and i am prepared for it.

So lets start my creating a new Web Api project and Add entity Framework for this.
I would be using the code first approach. But once my requirements are freeze i may switch to the Db first. Its up to the our decision making.



As i am going with the code first approach and i would know that i need data models and mapping and DataAccess layers i have created the folders for it.
Actually i want to separte it out the layers but i am short of time so i am doing everything in this.

The code first apporach fundamentals are explained at this article. Go through once url:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx



Now i am going to add few models to it.

public class ProjectDetails
    {
        public int ProjectId { get; set; }

        public int Name { get; set; }

        public int AdGroupName { get; set; }

        public int ReferenceId { get; set; }

        public virtual ICollection<OrgGroupNames> OrgGroupNames { get; set; }



    }

 public class OrgGroupNames
    {
        public int Id { get; set; }

        public string GroupName { get; set; }

        public int RefrenceId { get; set; }

        public int ProjectId { get; set; }

        public virtual ICollection<OrgGroupNamesDetails> OrgGroupNamesDetails { get; set; }
    }


public class OrgGroupNamesDetails
    {
        public int Id { get; set; }
           

        public string AdUserId { get; set; }

        public string Role { get; set; }

        public int ProjectId { get; set; }

        public int RefrenceId { get; set; }

        public int OrgGroupNamesId { get; set; }

        public virtual OrgGroupNames OrgGroupNames { get; set; }
    }


Now i will add the Context class and there respective mapping classes, with which i will do the upstream and downstream of data.

First i will create the mapping files which are very important for us to configure the constraints on table and columns with fluent validations.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ActiveDirectoryPractice
{
    public class ProjectDetails
    {
        public int Id { get; set; }

        public int Name { get; set; }

        public string AdGroupName { get; set; }

        public int ReferenceId { get; set; }

        public virtual ICollection<OrgGroupNames> OrgGroupNames { get; set; }



    }
}

namespace ActiveDirectoryPractice
{
    public class OrgGroupNames
    {
        public int Id { get; set; }

        public string GroupName { get; set; }

        public int RefrenceId { get; set; }

        public int ProjectId { get; set; }

        public virtual ICollection<OrgGroupNamesDetails> OrgGroupNamesDetails { get; set; }
    }
}

namespace ActiveDirectoryPractice
{
    public class OrgGroupNamesDetails
    {
        public int Id { get; set; }
           

        public string AdUserId { get; set; }

        public string Role { get; set; }

        public int ProjectId { get; set; }

        public int RefrenceId { get; set; }

        public int OrgGroupNamesId { get; set; }

        public virtual OrgGroupNames OrgGroupNames { get; set; }
    }


}

The Context class will have all  the dataset pointers to the Tables  and their respective mappings about how each entity is configured in the above mapping classes.


 public class OrgStructureContext : DbContext
    {
        public OrgStructureContext() : base("MyConnectionString")
        {

        }

        public DbSet<ProjectDetails> Students { get; set; }
        public DbSet<OrgGroupNames> OrgGroupNames { get; set; }
        public DbSet<OrgGroupNamesDetails> OrgGroupNamesDetails { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Mapping Classes
            modelBuilder.Configurations.Add(new ProjectDetailsMap());
            modelBuilder.Configurations.Add(new OrgGroupNamesMap());
            modelBuilder.Configurations.Add(new OrgGroupNamesDetailsMap());
        }
    }

I would need one more class for the DataBase initilizer to set few of the seed values.


No comments:

Post a Comment