• 1403/03/24

بررسی متد IsDevelopment :

با سلام

اگه بخوایم در کلاس زیر وضعیت متد IsDevelopment رو بررسی کنیم که در برنامه در حالت توسعه قرار داره یا نه، چطور باید اینکار رو انجام داد؟

مثلاً ایمیل رو به صورت لوکال و روی کامپیوتر بفرستیم.

public class SendEmail

{

   public static void Send(string to, string subject, string body)

   {

       MailMessage mail = new MailMessage();

       SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

       mail.From = new MailAddress("TopLearn.com@gmail.com", "تاپ لرن");

       mail.To.Add(to);

       mail.Subject = subject;

       mail.Body = body;

       mail.IsBodyHtml = true;

 

       //System.Net.Mail.Attachment attachment;

       // attachment = new System.Net.Mail.Attachment("c:/textfile.txt");

       // mail.Attachments.Add(attachment);

 

       SmtpServer.Port = 587;

       SmtpServer.Credentials = new System.Net.NetworkCredential("TopLearn.com@gmail.com", "****");

       SmtpServer.EnableSsl = true;

       SmtpServer.Send(mail);

 

   }

}

  • 1403/03/26
  • ساعت 09:56

سلام دوست عزیز وقت بخیر

شما میتونی این مورد رو در بخش IOC کنترل کنی یعنی برای سرویس ایمیل دو پیاده سازی داشته باشی و بعد با استفاده از IsDevelopment سرویس ها inject کنی


  • 1403/03/26
  • ساعت 10:28

این متد رندرشه:

using System;

using System.IO;

using System.Threading.Tasks;

using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Mvc.Abstractions;

using Microsoft.AspNetCore.Mvc.ModelBinding;

using Microsoft.AspNetCore.Mvc.Razor;

using Microsoft.AspNetCore.Mvc.Rendering;

using Microsoft.AspNetCore.Mvc.ViewFeatures;

using Microsoft.AspNetCore.Routing;

 

namespace Stone.Core.Convertors

{

   public interface IViewRenderService

   {

       string RenderToStringAsync(string viewName, object model);

   }

   public class RenderViewToString : IViewRenderService

   {

       private readonly IRazorViewEngine _razorViewEngine;

       private readonly ITempDataProvider _tempDataProvider;

       private readonly IServiceProvider _serviceProvider;

 

       public RenderViewToString(IRazorViewEngine razorViewEngine,

           ITempDataProvider tempDataProvider,

           IServiceProvider serviceProvider)

       {

           _razorViewEngine = razorViewEngine;

           _tempDataProvider = tempDataProvider;

           _serviceProvider = serviceProvider;

       }

 

       public  string RenderToStringAsync(string viewName, object model)

       {

           var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };

           var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());

 

           using (var sw = new StringWriter())

           {

               var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);

 

               if (viewResult.View == null)

               {

                   throw new ArgumentNullException($"{viewName} does not match any available view");

               }

 

               var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())

               {

                   Model = model

               };

 

               var viewContext = new ViewContext(

                   actionContext,

                   viewResult.View,

                   viewDictionary,

                   new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),

                   sw,

                   new HtmlHelperOptions()

               );

 

                viewResult.View.RenderAsync(viewContext);

               return sw.ToString();

           }

       }

   }

}

 

این کد کنترلر Account هست:

using Microsoft.AspNetCore.Authentication;

using Microsoft.AspNetCore.Authentication.Cookies;

using Microsoft.AspNetCore.Authorization;

using Microsoft.AspNetCore.Mvc;

using Stone.Core.Convertors;

using Stone.Core.DTOs;

using Stone.Core.Generator;

using Stone.Core.Security;

using Stone.Core.Senders;

using Stone.Core.Services.Interfaces;

using System.Security.Claims;

 

namespace Stone.Web.Controllers

{

   public class AccountController : Controller

   {

       private readonly IUserService _userService;

       private readonly IViewRenderService _viewRenderService;

       

       public AccountController(IUserService userService,

           IViewRenderService viewRenderService)

       {

           _userService = userService;

           _viewRenderService = viewRenderService;

           }

 

       #region Register

       [Route("register")]

       public IActionResult Register()

       {

           return View();

       }

 

       [HttpPost]

       [Route("Register")]

       public async Task<IActionResult> Register(RegisterViewModel register)

       {

           if (!ModelState.IsValid)

           {

               return View(register);

           }

 

           if (_userService.IsExistUserName(register.UserName))

           {

               ModelState.AddModelError("UserName", "نام کاربری معتبر نمی باشد");

               return View(register);

           }

 

           if (_userService.IsExistEmail(register.Email))

           {

               ModelState.AddModelError("Email", "ایمیل معتبر نمی باشد");

               return View(register);

           }

 

           DataLayer.Entities.User.User user = new DataLayer.Entities.User.User()

           {

               ActiveCode = NameGenerator.GenerateUniqCode(),

               Email = FixedText.FixEmail(register.Email),

               IsActive = false,

               Password = PasswordHelper.EncodePasswordMd5(register.Password),

               RegisterDate = DateTime.Now,

               UserAvatar = "Default.jpg",

               UserName = register.UserName

           };

           _userService.AddUser(user);

 

           #region Send Activation Email

           string body = _viewRenderService.RenderToStringAsync("_ActiveEmail", user);

           SendEmail.Send(user.Email,"ایمیل فعالسازی",body);

           #endregion

 

           return View("SuccessRegister", user);

       }

       #endregion

 

       #region Login

       [Route("Login")]

       public IActionResult Login()

       {

           return View();

       }

 

       [HttpPost]

       [Route("Login")]

       public ActionResult Login(LoginViewModel login)

       {

           if (!ModelState.IsValid)

           {

               return View(login);

           }

 

           var user = _userService.LoginUser(login);

           if (user != null)

           {

               if (user.IsActive)

               {

                   var claims = new List<Claim>()

                   {

                       new Claim(ClaimTypes.NameIdentifier,user.UserId.ToString()),

                       new Claim(ClaimTypes.Name,user.UserName)

                   };

                   var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

                   var principal = new ClaimsPrincipal(identity);

 

                   var properties = new AuthenticationProperties

                   {

                       IsPersistent = login.RememberMe

                   };

                   HttpContext.SignInAsync(principal, properties);

 

                   ViewBag.IsSuccess = true;

                   return View();

               }

               else

               {

                   ModelState.AddModelError("Email", "حساب کاربری شما فعال نمی باشد");

               }

           }

           ModelState.AddModelError("Email", "کاربری با مشخصات وارد شده یافت نشد");

           return View(login);

       }

 

 

       #endregion

 

       #region Active Account

       public IActionResult ActiveAccount(string id)

       {

           ViewBag.IsActive = _userService.ActiveAccount(id);

           return View();

       }

       #endregion

 

       #region Logout

 

       [Route("Logout")]

       public IActionResult Logout()

       {

           HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

           return Redirect("/Login");

       }

       #endregion

       [Authorize]

       public IActionResult test()

       {

           return View();

       }

   }

}

 

ضمناً در فایل Program هم تعریف شده.

حالا من می تونم در لایه web به متد IsDevelopment دسترسی داشته باشم ولی در لایه های پایین تر نه. چه کدی رو برای این کار باید نوشت؟


  • 1403/03/30
  • ساعت 11:42

دوست عزیز ببین ما یک interface داریم مثلا برای ارسال ایمیل استفاده میشه

ما برای این اینترفیس دو تا پیاده سازی قرار میدیم، یکی برای حال Develop و یکی production حالا با استفاده از این IsDevelopment میتونیم یک if بزاریم که یا پیاده سازی Develop به عنوان وابستگی تزریق بشه یا Production


  • 1403/03/31
  • ساعت 01:09

میشه کمی ریزتر توضیح بدید؟این توضیحات رو در پیام قبلی تون هم به صورت خلاصه نوشتید. 

لطفا با کد راهنمایی بفرمایید. 


  • 1403/04/20
  • ساعت 10:20

سلام دوست عزیزم وقت بخیر

مثال زیر رو من برای سرویس ایمیل توضیح میدم:

فرض کن یک سیستم داری که قرار ارسال ایمیل داشته باشه، شما یک interface برای ارسال ایمیل میسازی و در داخل interface هم میای یک متد برای ارسال ایمیل قرار میدی

حالا دوتا پیاده سازی از این اینترفیس میسازی یکی GamailService و یکی هم YahooService

حالا با استفاده از if و متد گفته شده ینی IsDevelopment میای چک میکنی که کدوم یکی از این پیاده سازی هارو برای interface خودت Register کنی


  • 1403/04/20
  • ساعت 20:19

دو نوع پیاده سازی که گفتید هردو به صورت آنلاین هستند و در زمان توسعه می خوام ایمیل ها به صورت لوکال و روی مسیر فیزیکی کامپیوتر و بدون نیاز به اینترنت پیاده سازی بشه.

چطور باید این کار رو انجام داد؟


logo-samandehi