Saturday, 23 March 2019

Insight DB Onion Autofac FInal


var builder = new ContainerBuilder();

            //Register the Log Interceptor, which will log out to the console of the application. Anything registered with 'log-calls' will be intercepted
            builder.Register(c => new CallLogger())
                .Named<IInterceptor>("log-calls");

            // Register dependencies in controllers
            builder.RegisterControllers(typeof(MvcApplication).Assembly);

            builder.RegisterType<HomeController>()
                  .EnableClassInterceptors();
              

            // Register dependencies in filter attributes
            builder.RegisterFilterProvider();


            // Register our Data dependencies
            builder.RegisterModule(new
                IOC.DatabaseIOC("Data Source=IND4PPFQ12;Initial Catalog=NEXUS_DB;Integrated Security=True;"));

            builder.RegisterModule(new IOC.ServiceIOC());


            var container = builder.Build();

            // Set MVC DI resolver to use our Autofac container
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));


public class CallLogger : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            try
            {
                string serializedInput = "";
                try
                {
                    serializedInput = JsonConvert.SerializeObject(invocation.Arguments);
                }
                catch
                {
                }

                Debug.WriteLine(
                    "{0} | Entering method [{1}] with paramters: {2}",
                    invocation.TargetType.FullName,
                    invocation.Method.Name,
                  //  string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())
                  serializedInput
                   );

                invocation.Proceed();
            }
            catch (Exception e)
            {
                Debug.WriteLine(
                    "{0} | {1}...Logging an exception has occurred: "
                    + e.Message + e.InnerException + e.StackTrace + e.Data
                    , invocation.TargetType.FullName, invocation.Method.Name);
               
            }
            finally
            {
                string serializedReturnData = string.Empty;
                serializedReturnData = JsonConvert.SerializeObject(invocation.ReturnValue);
              
                Debug.WriteLine(
                      "{0} | Leaving method [{1}] with return value {2}",
                     invocation.TargetType.FullName,
                    invocation.Method.Name,
                    serializedReturnData.ToString());
            }
        }
    }




  public interface IRepos : IDbConnection
    {
        [Sql("SELECT  [FBI_XML_FILENAME] FROM [NEXUS_DB].[dbo].[HMRC_RETURNS_HISTORY]")]
        List<string> GetDatas();
    }

public interface IRepo2
    {
        [Sql("SELECT  SUBMISSION_DATE FROM [NEXUS_DB].[dbo].[HMRC_RETURNS_HISTORY]")]
        List<DateTime> GetDatas1();
    }



    public class Service1 : IService1
    {
        private readonly IRepos _IRepos;

        public Service1(IRepos iRepos)
        {
            this._IRepos = iRepos;
        }

        public List<string> GetDataa()
        {
            return this._IRepos.GetDatas();
        }
    }

    public class Service2 : IService2
    {
        private readonly IRepo2 _IRepos;

        public Service2(IRepo2 iRepos)
        {
            this._IRepos = iRepos;
        }

        public List<DateTime> GetDataa1(Tuple<List<string>, string> tupleAuthor, int a)
        {
            return this._IRepos.GetDatas1();
        }
    }


  public interface IService1
    {
        List<string> GetDataa();
    }

    //the intercept can be on the interface of the class
    [Intercept("log-calls")]
    public interface IService2
    {
        List<DateTime> GetDataa1(Tuple<List<string>, string> tupleAuthor, int a);
    }



namespace IOC
{

    public class ServiceIOC : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<Service1>().As<IService1>().InstancePerRequest();
            builder.RegisterType<Service2>().As<IService2>().InstancePerRequest().EnableInterfaceInterceptors();
            base.Load(builder);
        }
    }

    public class DatabaseIOC : Module
    {
        private readonly DbConnection _sqlConnection;

        public DatabaseIOC(string sqlConnectionString)
        {
            this._sqlConnection = new SqlConnection(sqlConnectionString);
        }

        protected override void Load(ContainerBuilder builder)
        {
            SqlInsightDbProvider.RegisterProvider();
            builder.Register(b => this._sqlConnection.AsParallel<IRepos>());
            builder.Register(b => this._sqlConnection.AsParallel<IRepo2>());
            base.Load(builder);
        }
    }
}



      public HomeController(IService1 iService1, IService2 iService2)
        {
            _IService1 = iService1;
            _IService2 = iService2;
        }

       
        public ActionResult Index()
        {
           var res =  _IService1.GetDataa();

            Tuple<List<string>, string> tupleAuthor = Tuple.Create(res, "hELLO");

           var VVV = _IService2.GetDataa1(tupleAuthor, 5000);

            //throw new Exception();

            return View();
        }



1 comment: