I have some interfaces and classes in "repository" and "services" and say if I going to use FindBy method, I would call FindBy method in IUserService which would end up in FindBy method in UserRepository. The interfaces and classes look like this :
public interface IUserService
{
User FindBy(string userName);
SimpleResult Insert(User user);
}
public class UserService : IUserService
{
private readonly IUserRepository userRepository;
public UserService(IUserRepository _userRepository)
{
userRepository = _userRepository;
}
public User FindBy(string userName)
{
return userRepository.FindBy(userName);
}
public SimpleResult Insert(User user)
{
return userRepository.Insert(user);
}
}
public interface IUserRepository
{
SimpleResult Delete(User user);
IList<User> FindAll();
User FindBy(long userID);
User FindBy(string userName);
SimpleResult Insert(User user);
SimpleResult Update(User user);
}
public class UserRepository : IUserRepository
{
Entities db = new Entities();
public User FindBy(long userID)
{
return db.Users.Where(x => x.UserID == userID).FirstOrDefault();
}
public SimpleResult Insert(User user)
{
SimpleResult result = new SimpleResult();
try
{
db.Users.Add(user);
db.SaveChanges();
result.SetSuccessStatus("Insert Success!");
}
catch (Exception ex)
{
result.SetErrorStatus(string.Format("Insert Failed! Cause : {0}", ex.Message));
}
return result;
}
//other methods
}
The problem is, I can only get it to work by instantiating it like this in aspx.cs :
public partial class Default : System.Web.UI.Page
{
IUserService userService = new UserService(new UserRepository());
}
IMO, it such a mess. I've seen someone use a much more simpler way to instantiate it like :
public partial class Default : System.Web.UI.Page
{
IUserService userService { get; set; }
}
When I Instantiate it like that, I get a null userService. How can I do it that way?
Not sure if you ever heard about Dependency Injection, it might be helpful. There are a lot different DI Containers which can be used to that (Unity, Ninject, Autofac etc.) Some article about DI and Loose Coupling: http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/
you should inject your dependency on the controller constructor
IPostRepository _repository;
public PostsController(IPostRepository repository)
{
_repository = repository;
}
You can inject your dependency via construnctor with a IOC Container (as Ninject, wich you can download via NUGet package)
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IPostRepository>().To<PostRepository>();
}
EDIT
Web Form version
Inherit Global(asax) from NinjectHttpApplication
and override CreateKernel
method
public class Global : NinjectHttpApplication
protected override IKernel CreateKernel()
{
IKernel kernel = new StandardKernel(new YourModule());
return kernel;
}
public class YourModule: Ninject.Modules.NinjectModule
{
public override void Load()
{
Bind<IPostRepository>().To<PostRepository>();
}
}
Derive your pages from Ninject.Web.PageBase
public partial class Default : PageBase
{
[Inject]
public IPostRepository _postRepository { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
Post post = _postRepository.Get(int postId);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.